简体   繁体   English

我在 graphql javascript 中遇到突变查询问题

[英]I have an issues with mutation query in graphql javascript

I try this type of mutation but I didn't get a proper response please help me to solve that.我尝试了这种类型的突变,但没有得到正确的响应,请帮我解决这个问题。

mutation: new GraphQLObjectType({
  name: "Mutation",
  fields: {
    createProduct: {
      type: new GraphQLNonNull(productType),
      args: {
        products: { type: new GraphQLNonNull(productInput) }
      },
      resolve: (_, products) => {
        let pro = products;
        console.log("pro", pro);

        return products.push(pro);
      }
    }
  }
});

You probably want to keep the products in memory inside of an array of products.您可能希望将 memory 中的产品保留在产品数组中。 There are two things wrong in your code.您的代码中有两处错误。 First, the second argument of the resolver returns an object of arguments because a field can have multiple arguments.首先,解析器的第二个参数返回 arguments 的 object,因为一个字段可以有多个 arguments。 You can either destructure the arguments or use the normal object access syntax.您可以解构 arguments 或使用正常的 object 访问语法。 Second, Array.prototype.push returns the new lenght of the array .其次, Array.prototype.push 返回数组的新长度 Therefore, you can't return the result of this call (your product type most likely expects a product as a root value).因此,您无法返回此调用的结果(您的产品类型很可能需要一个产品作为根值)。 Return product instead.而是退回product Also you are pushing into the argument.你也在推动争论。 Maybe you have a name conflict there.也许你在那里有名字冲突。 I would recommend to use singular for arguments and variables that are not arrays, lists, or sets.我建议对 arguments 和不是 arrays、列表或集合的变量使用单数。

let products = [];

const schema = new GraphQLSchema({
  query: _,
  mutation: new GraphQLObjectType({
    name: "Mutation",
    fields: {
      createProduct: {
        type: new GraphQLNonNull(productType),
        args: {
          product: { type: new GraphQLNonNull(productInput) }
        },
        resolve: (_, args) => {
          console.log("pro", args.product);

          products.push(args.product);
          return args.product;
        }
      }
    }
  }),
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM