Darryl
07/04/2018, 4:50 PMlawjolla
07/04/2018, 4:54 PMDarryl
07/04/2018, 4:56 PMlawjolla
07/04/2018, 4:56 PMDarryl
07/04/2018, 4:57 PMlawjolla
07/04/2018, 4:58 PMDarryl
07/04/2018, 4:58 PMlawjolla
07/04/2018, 4:58 PMDarryl
07/04/2018, 4:59 PMlawjolla
07/04/2018, 5:03 PMDarryl
07/04/2018, 5:04 PMDarryl
07/04/2018, 5:04 PMDarryl
07/04/2018, 5:04 PMlawjolla
07/04/2018, 5:05 PMcreate
vs connect
just means if the type already exists. So if there's a "salt" ingredient already existing, you connect to it rather than create it.lawjolla
07/04/2018, 5:07 PMDarryl
07/04/2018, 5:07 PMlawjolla
07/04/2018, 5:07 PMDarryl
07/04/2018, 5:08 PMDarryl
07/04/2018, 5:08 PMlawjolla
07/04/2018, 5:09 PMDarryl
07/04/2018, 5:09 PMDarryl
07/04/2018, 5:09 PMcreateRecipe: async (
_,
{ ingredients, name, instructions, categories, imageUrl },
context: Context,
info
) => {
const ingredientsNutrition = await context.prisma.query.ingredients(
{
where: { name: ingredients.name }
},
`
{
nutrition {
carbohydrates
energy
fat
protein
}
}
`
);
const nutrition: Nutrition = ingredientsNutrition
.map(ingredient => ingredient.nutrition)
.reduce((a, b) => {
return {
energy: a.energy + b.energy,
fat: a.fat + b.fat,
carbohydrates: a.carbohydrates + b.carbohydrates,
protein: a.protein + b.protein
};
});
return context.prisma.mutation.createRecipe(
{
data: {
name,
instructions: {
set: instructions
},
categories: {
connect: [...categories]
},
imageUrl: imageUrl,
nutrition: {
create: nutrition
}
}
},
info
);
}
lawjolla
07/04/2018, 5:10 PMlawjolla
07/04/2018, 5:10 PMDarryl
07/04/2018, 5:10 PMDarryl
07/04/2018, 5:11 PMlawjolla
07/04/2018, 5:12 PMcreateRecipe
, you don't have any IngredientInRecipe
Darryl
07/04/2018, 5:12 PMlawjolla
07/04/2018, 5:12 PMlawjolla
07/04/2018, 5:15 PMreturn context.prisma.mutation.createRecipe(
{
data: {
...
ingredients: {
create: {
quantity: x,
recipe: {
connect: { id: recipeId }
},
ingredients: {
connect: [{id: id1}, {id: id2}...]
}
// or if there's only one ingredient per type
ingredient: { connect: { id: ingredientId }}
}
}
...
}
},
info
);
}
lawjolla
07/04/2018, 5:15 PMDarryl
07/04/2018, 5:15 PMDarryl
07/04/2018, 5:15 PMlawjolla
07/04/2018, 5:18 PMDarryl
07/04/2018, 5:21 PM[ { quantity: 1, name: 'Rolled Oats' },
{ quantity: 3, name: 'Oat Milk' } ]
It seems like I somehow need to loop over these and create and object in ingredients for each.Darryl
07/04/2018, 5:22 PMlawjolla
07/04/2018, 5:23 PMDarryl
07/04/2018, 5:23 PMDarryl
07/04/2018, 5:25 PM{
"data": {
"recipes": [
{
"name": "Recipe 1",
"ingredients": [
{
"quantity": 1,
"ingredient": {
"name": "Rolled Oats",
... // more about that ingredient if I want it
}
},
{
"quantity": 3,
"ingredient": {
"name": "Oat Milk",
... // more about that ingredient if I want it
}
}
],
"nutrition": {
"fat": 7.5,
"carbohydrates": 75.7,
"protein": 14,
"energy": 427
}
}
]
}
}
Darryl
07/04/2018, 5:26 PMlawjolla
07/04/2018, 5:28 PMIngredientInRecipe
had ingredients
plural, as a list. So that's how I wrote the mutation. Your example return here is as a single object, which makes more senselawjolla
07/04/2018, 5:30 PMlawjolla
07/04/2018, 5:31 PMcreateRecipe
mutation above will give you the exact output you wantDarryl
07/04/2018, 5:33 PMlawjolla
07/04/2018, 5:36 PMDarryl
07/04/2018, 5:40 PMingredients: {
create: {
quantity: x, ← 1
recipe: {
connect: { id: recipeId }
},
ingredients: {
connect: [
{id: id1}, ← 2
{id: id2} ← 3
]
}
}
}
1) I can’t hard-code a single quantity here. This create essentially needs to be run for each item in the args.ingredients. I don’t know how it works, hence the question. 🙂lawjolla
07/04/2018, 5:40 PMingredients: [ ... someArray(x => ({
create: {
quantity: x.quantity, recipe: { connect: { id: recipeId }}, ingredient: { connect: { x.id }}
}
}))]
Darryl
07/04/2018, 5:40 PMDarryl
07/04/2018, 5:41 PMlawjolla
07/04/2018, 5:41 PMDarryl
07/04/2018, 5:41 PMDarryl
07/04/2018, 5:42 PMlawjolla
07/04/2018, 5:42 PMDarryl
07/04/2018, 5:43 PMDarryl
07/04/2018, 5:43 PMlawjolla
07/04/2018, 5:43 PMDarryl
07/04/2018, 5:43 PMlawjolla
07/04/2018, 5:44 PMlawjolla
07/04/2018, 5:44 PMDarryl
07/04/2018, 5:44 PMlawjolla
07/04/2018, 5:45 PMDarryl
07/04/2018, 5:46 PMDarryl
07/04/2018, 5:47 PMlawjolla
07/04/2018, 5:47 PMDarryl
07/04/2018, 5:47 PMDarryl
07/04/2018, 5:48 PMlawjolla
07/04/2018, 5:48 PMlawjolla
07/04/2018, 5:48 PMDarryl
07/04/2018, 5:48 PMDarryl
07/04/2018, 5:49 PMlawjolla
07/04/2018, 5:49 PMnilan
07/05/2018, 8:58 AMnilan
07/05/2018, 8:58 AMDarryl
07/05/2018, 12:28 PMnilan
07/05/2018, 12:51 PMnilan
07/14/2018, 11:27 AM