I’m working on a migration to Apollo and want to access userId
in a sub-query by using the context argument, but context is showing up as undefined.
the schema type:
type Meal {
_id: String
name: String
userId: String
submitted: String
photoId: String
servings: Int
published: Boolean
sourceName: String
sourceUrl: String
ingredients: [Ingredient]
steps: [Step]
grams: Int
nutrition: Nutrition
daysWithMeal: [Int]
}
my root query:
async publishedMeals(root, args, context) {
return await PGMeals.findAll( { where: { published: true } } );
},
daysWithMeal
is a ‘synthetic’ field, it doesn’t have a stored value in the database, it is instead produced with its own query using the following resolver:
sub-query for daysWithMeal
field which produces an array of integers:
Meal: {
daysWithMeal: (meal) => db.query(
`
SELECT
d.order as order
FROM days d
JOIN day_meals e on e."dayId"= d._id
WHERE e."mealId" = :mealId and e."userId" = :userId;
`,
{ replacements: { mealId: meal._id, userId: context.userId }, type: db.QueryTypes.SELECT })
.then((data) => {
const arr = [];
console.log("context", context);
data.forEach((item) => {arr.push(item.order)});
return arr;
}),
},
how do I get access to context in the sub query daysWithMeal
?