Currently seeking examples of writing nested mutations. Specifically, I am creating a mutation for a recipe object with the following schema:
const RecipeType = new GraphQLObjectType({
name: "Recipe",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
dateCreated: { type: GraphQLString },
authorID: { type: GraphQLID },
prepTime: { type: PrepTimeType },
cookTime: { type: CookTimeType },
ingredients: { type: new GraphQLList(IngredientType) },
steps: { type: new GraphQLList(StepType) }
})
});
const PrepTimeType = new GraphQLObjectType({
name: "PrepTime",
fields: () => ({
quantity: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
const CookTimeType = new GraphQLObjectType({
name: "CookTime",
fields: () => ({
quantity: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
const IngredientType = new GraphQLObjectType({
name: "Ingredients",
fields: () => ({
name: { type: GraphQLString },
quantity: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
const StepType = new GraphQLObjectType({
name: "Ingredients",
fields: () => ({
details: { type: GraphQLString },
estimatedTime: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
In order to create an entire object for this item, I need to write a mutation like the one below:
createRecipe: {
type: RecipeType,
args: {
// Required Args
name: { type: new GraphQLNonNull(GraphQLString) },
authorID: { type: new GraphQLNonNull(GraphQLID) },
ingredients: { type: new GraphQLList(IngredientType) },
steps: { type: new GraphQLList(StepType) },
// Not required args
prepTime: { type: PrepTimeType },
cookTime: { type: CookTimeType },
},
resolve(parent, args) {
let recipe = new Recipe({
name: args.name,
dateCreated: new Date().getTime(),
authorID: args.authorID,
ingredients: args.ingredients,
steps: args.steps
});
// Handle optional arguments
args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null;
args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null;
return recipe.save();
}
}
Struggling with creating a single mutation that handles the complete object creation, and updating will be an additional challenge. Any guidance, examples, or documentation links would be helpful as it seems GraphQL lacks clear instructions on this topic.
Encountering the following errors currently:
{
"errors": [
{
"message": "The type of Mutation.createRecipe(ingredients:) must be Input Type but got: [Ingredients]."
},
{
"message": "The type of Mutation.createRecipe(steps:) must be Input Type but got: [Steps]."
},
{
"message": "The type of Mutation.createRecipe(prepTime:) must be Input Type but got: PrepTime."
},
{
"message": "The type of Mutation.createRecipe(cookTime:) must be Input Type but got: CookTime."
}
]
}
Appreciate any assistance provided.
Thanks,