编写嵌套的GraphQL突变

时间:2018-11-06 15:47:43

标签: javascript mongodb graphql

我正在寻找编写嵌套突变的示例。我正在对配方对象进行突变,其架构如下所示:

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 }
  })
});

我正在寻找写一个变异来创建该物品的整个对象。突变如下所示:

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
    });

    // Check for optional args and set to recipe if they exist
    args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null;
    args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null;

    return recipe.save();
  }
}

我不确定如何创建一个可以创建整个对象的突变。然后更新将是进一步的挑战。是否有人有任何示例或指向支持此操作的文档的链接?据我所知,GraphQL并未以有用的方式进行介绍。

我目前遇到以下错误:

{
  "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."
    }
  ]
}

任何支持将不胜感激。

干杯

1 个答案:

答案 0 :(得分:0)

我知道了。我需要为每个子文档创建输入类型。我已经有了对象类型,但是对于突变,我不得不添加新的对象类型。从那里,我将其放入突变体中。

createRecipe: {
  type: RecipeType,
  args: {
    // Required Args
    name: { type: new GraphQLNonNull(GraphQLString) },
    authorID: { type: new GraphQLNonNull(GraphQLID) },
    ingredients: { type: new GraphQLList(IngredientInputType) },
    steps: { type: new GraphQLList(StepInputType) },
    // Not required args
    prepTime: { type: PrepTimeInputType },
    cookTime: { type: CookTimeInputType },
  },
  resolve(parent, args) {
    let recipe = new Recipe({
      name: args.name,
      dateCreated: new Date().getTime(),
      authorID: args.authorID,
      ingredients: args.ingredients,
      steps: args.steps
    });

    // Check for optional args and set to recipe if they exist
    args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null ;
    args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null ;

    return recipe.save();
  }
},