【问题标题】:Writing Nested GraphQL Mutations编写嵌套的 GraphQL 突变
【发布时间】:2019-04-10 01:07:13
【问题描述】:

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

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

我们将不胜感激。

干杯,

【问题讨论】:

  • 我想通了。我必须为所有子文档创建新的输入类型。从那里我将它们添加到突变中并且效果很好:

标签: javascript mongodb graphql


【解决方案1】:

我想通了。我需要为每个子文档创建输入类型。我已经有了对象类型,但是对于突变,我必须添加新的。从那里我将它放入突变中。

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

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 2020-04-24
    • 2018-08-24
    • 2021-04-09
    • 2017-07-28
    • 2016-02-07
    • 2018-08-22
    • 2020-04-05
    相关资源
    最近更新 更多