I'm having a hard time figuring out why my graphQL & DataLoader setup isn't working and could use some help.
I have a User and a Orchestra type, and I would like to transform a User to populate its createdOrchestras field and do the same thing with Orchestra and an owner field.
EDITED.
The following code causes an infinite loop:
Here are the DataLoaders, which are passed to the resolvers via context:
const DataLoader = require('dataloader');
const Orchestra = require('../models/orchestras');
const User = require('../models/users');
const batchOrchestras = async ids => {
const orchestras = await Orchestra.find({ _id: { $in: ids } });
const orchestraMap = {};
orchestras.forEach(o => {
orchestraMap[o.id] = o;
});
return ids.map(id => orchestraMap[id] || new Error(`No result for ${id}`));
}
const batchUsers = async ids => {
const users = await User.find({ _id: { $in: ids } });
const userMap = {};
users.forEach(u => {
userMap[u.id] = u;
});
return ids.map(id => userMap[id] || new Error(`No result for ${id}`));
};
module.exports = () => new DataLoader(batchUsers)
module.exports = () => new DataLoader(batchOrchestras);
Here are the transform functions which should be capable of fetching data for nested fields via data loaders and modify sensitive fields like the user password.
async function transformUser(userId, loaders) {
const user = await loaders.userLoader.load(userId.toString());
return {
...user._doc,
createdOrchestras: await Promise.all(
user._doc.createdOrchestras.map(orchestra =>
transformOrchestra(orchestra, loaders)
)
)
}
}
async function transformOrchestra(orchestraId, loaders) {
const orchestra = await loaders.orchestraLoader.load(orchestraId.toString());
return {
...orchestra._doc,
owner: transformUser(orchestra._doc.owner, loaders)
}
}
module.exports = {
transformUser,
transformOrchestra
}
How should I restructure the code to prevent an infinite loop but keeping the transform functions as the final providers of data for a particular field ?
Related
I don't understand why I get this error. This is my controller:
export const createProductReview = async (req, res) => {
const { rating, comment } = req.body;
const product = await Product.findById(req.params.id);
if (product) {
const alreadyReviewed = product.reviews.find(
r => r.user.toString() === req.user.userId.toString()
);
if (alreadyReviewed) {
throw new NotFoundError('Product already reviewed');
}
const review = {
user: req.user.userId,
name: req.user.username,
rating: Number(rating),
comment,
};
product.reviews.push(review);
product.numOfReviews = product.reviews.length;
product.rating =
product.reviews.reduce((acc, item) => item.rating + acc, 0) /
product.reviews.length;
await product.save();
res.status(StatusCodes.OK).json({ message: 'Review added', review });
} else {
throw new NotFoundError('Product not found');
}
};
This is mine productPage where i dispatch addProductReview and passing product id from params and review object:
const [rating, setRating] = useState(0);
const [comment, setComment] = useState('');
const submitHandler = e => {
e.preventDefault();
dispatch(
addProductReview(id, {
rating,
comment,
})
);
};
And this is my productSlice:
export const addProductReview = createAsyncThunk(
'product/review',
async (id, { review }, thunkAPI) => {
try {
const { data } = await axios.post(
`/api/v1/products/${id}/reviews`,
review
);
return data;
} catch (error) {
const message = error.response.data.msg;
return thunkAPI.rejectWithValue(message);
}
}
);
I have no clue why i got error Path comment is required. i pass review object to route.
The issue is with the parameters used in your Thunk payloadCreator. From the documentation...
The payloadCreator function will be called with two arguments:
arg: a single value, containing the first parameter that was passed to the thunk action creator when it was dispatched. This is useful for passing in values like item IDs that may be needed as part of the request. If you need to pass in multiple values, pass them together in an object when you dispatch the thunk, like dispatch(fetchUsers({status: 'active', sortBy: 'name'})).
thunkAPI: an object containing all of the parameters that are normally passed to a Redux thunk function, as well as additional options
Your payloadCreator has three arguments which is incorrect.
Try this instead
export const addProductReview = createAsyncThunk(
'product/review',
async ({ id, ...review }, thunkAPI) => {
try {
const { data } = await axios.post(
`/api/v1/products/${id}/reviews`,
review
);
return data;
} catch (error) {
const message = error.response.data.msg;
return thunkAPI.rejectWithValue(message);
}
}
);
and dispatch it like this
dispatch(addProductReview({ id, rating, comment }));
here in for loop with other operations ie: searching product, i have added for of loop and mapping product with store in mapping table.
data size is in thousands.
for (let store of storesList) {
storeProductMappingRepository
.findOne({
relations: ["store", "product"],
where: {
store: store,
product: product,
},
})
.then((isMappingAvailable) => {
if (!isMappingAvailable) {
let storeProductMapping = new StoreProductMapping();
storeProductMapping.price = item.Item_Price;
storeProductMapping.store = store;
storeProductMapping.product = product;
storeProductMappingRepository.save(storeProductMapping);
}
});
}
You must correctly "await" each promise (async code), otherwise the function may (most of the time) return before processing all stores.
For more information, take a look at Promise from MDN.
Two solutions are available:
await on each iteration
for (let store of storesList) {
// await here
const isMappingAvailable = (await storeProductMappingRepository
.findOne({
relations: ["store", "product"],
where: {
store: store,
product: product,
},
})) !== undefined;
if (!isMappingAvailable) {
const storeProductMapping = new StoreProductMapping();
storeProductMapping.price = item.Item_Price;
storeProductMapping.store = store;
storeProductMapping.product = product;
// await here
await storeProductMappingRepository.save(storeProductMapping);
}
}
// Executed
Single await with Promise.all(...)
Requires a little bit more code but can run multiple Promise(s) in parallel.
// Create an array of Promise(s)
const promises: { (): Promise<void>} [] = [];
for (let store of storesList) {
// Add a promise for each store
promises.push(async () => {
// await here
const isMappingAvailable = (await storeProductMappingRepository
.findOne({
relations: ["store", "product"],
where: {
store: store,
product: product,
},
})) !== undefined;
if (!isMappingAvailable) {
const storeProductMapping = new StoreProductMapping();
storeProductMapping.price = item.Item_Price;
storeProductMapping.store = store;
storeProductMapping.product = product;
// await here
await storeProductMappingRepository.save(storeProductMapping);
}
return Promise.resolve();
});
}
// Resolve/Execute all
await Promise.all(promises);
// Executed
The module calls three tables(students, subjects, grades)I call three sql queries and somehow managed to call one by one as explained below. the queries are independent of each other. However the select queries are executed one by one, first student, then subjects, then grades using await.
studentmodel.calls are only for executing select quesries from the database and is in one module. Other functions are defined in a separate module
The logic can execute the three selct queries(database calls) in parallel, then aggregate and process all the data together. Please let me know how to modify so that the database calls can execute independent, then move to process all data together
processing module -main start call
const mainstart = async () => {
let students = 0;
const getStudentData = await getAllStudents();
/** checking a condition if getEmployeeData responce is not empty */
if (getStudentData.length > 0) {
const studentData = await processData(getStudentData);
return 1;
} else {
return 0;
}
};
same file secondcall to the function getAllStudents
const getAllStudents = async () => {
try {
return await studentmodel.getAllStudents();//database call 1
} catch (err) {
// console.log("processing", err)
}
};
const processData = async (getStudentData) => {
try {
let studentData = [];
const subjectsData = await studentModel.getStudentSubjects();//database call 2
const gradesData = await studentModel.getStudentGrades();//database call 3
await Promise.all(getStudentData.map(async (singleObject) => {
let subjects = await processSubjects(subjectsData, singleObject.student_log);
let grades = await processGrades(gradesData, singleObject.student_log);
//Some processing on sigleobject, subjects and grades to populate studentData array
}));
return studentData;
} catch (err) {
console.log("processing", err)
}
};
const processSubjects = async (result, Name) => {
let subjectsArr = [];
const processArray = result.filter(ele => ele.student_log == Name)
processArray.map((singleObject) => {
subjectsArr.push({
name: singleObject.name,
startDate: singleObject.startDate,
});
})
return subjectsArr;
}
const processGrades = async (result, Name) => {
let gradesArr = [];
const processArray = result.filter(ele => ele.student_log == Name)
processArray.map((singleObject) => {
gradesArr.push({
id: singleObject.id,
name: singleObject.name,
});
})
return gradesArr;
database calls module/studentModel
const getAllStudents = async () => {
try {
/** Populating all students */
const sqlQuery = `SELECT * FROM STUDENTS`;
let [result] = await bigQuery.query({
query: sqlQuery,
location: 'US'
});
return result;
} catch (err) {
return false;
}
};
const getStudentSubjects = async () => {
try {
/** Populating all skills */
const sqlQuery = `SELECT * FROM Subjects`;
let [result] = await bigQuery.query({
query: sqlQuery,
location: 'US'
});
return result;
} catch (err) {
return false;
}
};
const getStudentGrades = async () => {
try {
/** Populating all students */
const sqlQuery = `SELECT * FROM GRADES`;
let [result] = await bigQuery.query({
query: sqlQuery,
location: 'US'
});
return result;
} catch (err) {
return false;
}
};
While I didn't probably fully understand what your question is, I had a go with your code.
I simulated your studentmodel functions with setTimeout and made the code like so that it first fetches all students. After fetching all students, it fetches the subjects and the grades "in parallel" by utilising Promise.all. After we have fetched our students, subjects and grades, we pass all of those to processData function where you can process all of the data however you want.
In case you would also like to fetch the students "in parallel" with the subjects and grades, just change the Promise.all part like so:
const [studentData, studentSubjects, studentGrades] = await Promise.all(
[
getAllStudents(),
getStudentSubjects(),
getStudentGrades()
]);
And remove the const studentData = await getAllStudents(); line and the if-clause. Because you had the if(studentData.length > 0) in your code, I assumed that we only want to fetch subjects and grades if there are students and therefore that needs to be done first, separately.
Note that if you want to do all three in parallel, you cannot use studentData when calling getStudentSubjects or getStudentGrades.
// STUDENTMODEL
const getAllStudents = async () => {
// Simulate SQL query
console.log("Fetching students");
return new Promise(resolve =>
setTimeout(() => {
resolve(["Student 1", "Student 2"])
}, 1000));
};
const getStudentSubjects = async () => {
// Simulate SQL query
console.log("Fetching subjects");
return new Promise(resolve =>
setTimeout(() => {
resolve(["Subject 1", "Subject 2"])
}, 1500));
};
const getStudentGrades = async () => {
// Simulate SQL query
console.log("Fetching grades");
return new Promise(resolve =>
setTimeout(() => {
resolve(["Grade 1", "Grade 2"])
}, 1500));
};
const mainstart = async () => {
// Fetch student data from database
const studentData = await getAllStudents();
if (studentData.length > 0) {
// Use Promise.all to wait until both student subjects and
// student grades have been fetched
// The results are destructured into
// studentSubjects and studentGrades variables
const [studentSubjects, studentGrades] = await Promise.all(
[
getStudentSubjects(studentData),
getStudentGrades(studentData)
]);
// Everything is fetched, process it all
processData([studentData, studentSubjects, studentGrades]);
return 1;
} else {
return 0;
}
};
const processData = (allData) => {
console.log("Processing all data");
console.log(allData);
// Process data somehow
};
(async () => {
console.log('start');
await mainstart();
console.log('end');
})();
I have a page that displays user contacts. it retrieves an array of _ids from a specific user and the should iterate over these arrays to get corresponding contact information. However I get stuck with my async operations. In the current situation it resolves after the firs contactinformation was pushed to the invitationsFromFriendsData array. If I put the resolve outside the forEach loop, it resolves instantly without adding any contactinformation to the array. What am I doing wrong?
exports.contactBoard = async (req, res) => {
const username = req.params.username;
const doc = await User.findOne({'username': username}).exec();
const friendFiller = async () => {
return new Promise((resolve, reject)=> {
doc.invitationsFromFriends.forEach (async element => {
const doc1 = await User.findById(element).exec()
doc.invitationsFromFriendsData.push(doc1.username)
resolve('gelukt')
});
})};
const send = async () => {
return new Promise((resolve, reject)=> {
res.status(200).json({
message:"retrieved user contacts",
contacts: doc
})
console.log("Nu verzenden")
resolve ('gelukt')
})
};
const verzend = async() => {
let first = await friendFiller();
console.log('Ik wacht op het vullen')
let second = await send();
}
verzend();
}
You can use Promise.all to await an array of promises.
const friendFiller = async () => {
const friends = await Promise.all(doc.invitationsFromFriends.map((id) => User.findById(element).exec()));
friends.forEach(() => {
doc.invitationsFromFriendsData.push(doc1.username);
})
}
Having said that I think Mongoose can perform "joins" on your behalf with populate which I expect is more efficient as it'll issue a single DB query for all the friends.
I have Card model and I have an API where I'm looking for a document by ID.
app.post("/api/postcomment", async (req,res) => {
const data = req.body
const reqUrl = req.headers.referer
const re = new RegExp('([a-zA-Z0-9]*$)', 'i')
const fixedUrl = reqUrl.match(re)
try {
await Card.update({_id: fixedUrl}, {$push:{'comments': data}})
const card = await Card.findById(fixedUrl)
return res.json(card)
} catch (err) {
throw err
}
})
It works fine. But now I have few more models. All should work the same way to them. But how can I make this code reusable for every model?
Or maybe there is a way to pass a name of my model to API? and then use it like this:
app.post("/api/postcomment", async (req,res, modelName) => {
const data = req.body
const reqUrl = req.headers.referer
const re = new RegExp('([a-zA-Z0-9]*$)', 'i')
const fixedUrl = reqUrl.match(re)
try {
await modelName.update({_id: fixedUrl}, {$push:{'comments': data}})
const item = await modelName.findById(fixedUrl)
return res.json(item )
} catch (err) {
throw err
}
})
Solution1: You can create two helper functions and call the from the router. Both function accept the model object:
let updateDocument = (model, fixedUrl, data) => {
return model.update({ _id: fixedUrl }, { $push: { comments: data }})
}
let getDocument = (model, fixedUrl) => {
return model.findById(fixedUrl)
}
app.post("/api/postcomment", async (req, res, modelName) => {
const data = req.body
const reqUrl = req.headers.referer
const re = new RegExp('([a-zA-Z0-9]*$)', 'i')
const fixedUrl = reqUrl.match(re)
try {
await updateDocument(Card, fixedUrl, data)
const item = await getDocument(Card, fixedUrl)
return res.json(item )
} catch (err) {
throw err
}
})
Solution2: The much better solution is to create a base class (service), with the common functionality. And extend it for each model:
class BaseService {
constructor(model) {
this.model = model;
}
getDocument(data) {
return this.model.findOne(...);
}
updateDocument(data) {
return this.model.update(...);
}
}
class CardService extends BaseService {
constuctor() {
super(Card);
}
}