繁体   English   中英

如何在 GraphQL 中执行突变?

[英]How to execute a mutation in GraphQL?

在 GraphQL 中,我们基本上有两种类型的操作:查询和突变。 虽然查询在文档中有很好的描述并且有很多示例,但我很难理解如何执行突变。 突变显然是更新方法。

我创建了非常简单的 Node.js 服务器:

var express = require("express");
var graphqlHTTP = require("express-graphql");
var graphql = require("graphql");
var inMemoryDatabase = require("./inMemoryDatabase").inMemoryDatabase;
var _ = require("lodash-node");

var userType = new graphql.GraphQLObjectType({
  name: "User",
  fields: {
    id: { type: graphql.GraphQLString },
    name: { type: graphql.GraphQLString }
  }
});

var queryType = new graphql.GraphQLObjectType({
  name: "Query",
  fields: {
    user: {
      type: userType,
      args: {
        id: { type: graphql.GraphQLString }
      },
      resolve: function(parent, { id }) {
        return _.find(inMemoryDatabase, { id: id });
      }
    }
  }
});

var mutationType = new graphql.GraphQLObjectType({
  name: "Mutation",
  fields: {
    user: {
      type: userType,
      args: {
        id: { type: graphql.GraphQLString },
        name: { type: graphql.GraphQLString }
      },
      resolve: function(parent, { id, name }) {
        var index = _.findIndex(inMemoryDatabase, { id: id });
        inMemoryDatabase.splice(index, 1, { id: id, name: name });
        return _.find(inMemoryDatabase, { id: id });
      }
    }
  }
});

var schema = new graphql.GraphQLSchema({
  query: queryType,
  mutation: mutationType
});

var app = express();
app.use(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    graphiql: true
  })
);

var port = 9000;
if (process.env.PORT) {
  port = process.env.PORT;
}

app.listen(port);
console.log("Running a GraphQL API server at localhost:" + port + "/graphql");

在 memory 数据库中只是一个用户对象数组{id, name}

var inMemoryDatabase = [
  {
    id: "31ce0260-2c23-4be5-ab78-4a5d1603cbc8",
    name: "Mark"
  },
  {
    id: "2fb6fd09-2697-43e2-9404-68c2f1ffbf1b",
    name: "Bill"
  }
];

module.exports = {
  inMemoryDatabase
};

执行查询以通过 id 获取用户如下所示:

{
 user(id: "31ce0260-2c23-4be5-ab78-4a5d1603cbc8"){
  name
 }
}

改变用户名的突变会是什么样子?

嘿,您可能完全错过了您所说的内容,但是我看待突变的方式是这样的

  • 我得到一些参数和一个字段,与params和静止的路径相同,并使用它们做一些事情(在您的情况下,请查找用户并根据传入的参数更新属性。
  • 之后,我从resolve函数返回一些内容,该内容将满足您在突变type中指定的type

 var mutationType = new graphql.GraphQLObjectType({ name: "Mutation", fields: { user: { // You must return something from your resolve function // that will fulfill userType requirements type: userType, // with these arguments, find the user and update them args: { id: { type: graphql.GraphQLString }, name: { type: graphql.GraphQLString } }, // this does the lookup and change of the data // the last step of your result is to return something // that will fulfill the userType interface resolve: function(parent, { id, name }) { // Find the user, Update it // return something that will respond to id and name, probably a user object } } } }); 

然后将其作为上下文,传递一些参数并请求返回用户

mutation updateUser {
  user(id: "1", name: "NewName") {
    id
    name
  }
}

在正常的生产模式中,通常还会有类似errors ,这些errors可能会返回以传达失败/未找到的更新的不同状态

@Austio的答案非常接近,但是正确的方法是:

mutation updateUser {
  user(id: "31ce0260-2c23-4be5-ab78-4a5d1603cbc8", name: "Markus") {
    id
    name
  }
}

如果我们直接与下面的 MongoDB 联系,将会对您有所帮助。

mutation {
  
  taskTrackerCreateOne
  (
    record: 
    {
      id:"63980ae0f019789eeea0cd33", 
      name:"63980c86f019789eeea0cda0"
    }
  )
  {
    recordId
  }
}

暂无
暂无

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

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