代码之家  ›  专栏  ›  技术社区  ›  Arkadiusz Kałkus

如何在GraphQL中执行突变?

  •  0
  • Arkadiusz Kałkus  · 技术社区  · 7 年前

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

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

    内存中的数据库只是一个用户对象数组 {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
     }
    }
    

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

    2 回复  |  直到 7 年前
        1
  •  2
  •   Austio    7 年前

    嘿,我可能完全不知道你在说什么,但我看待突变的方式是这样的

    • 之后,我从resolve函数返回一些东西,它将满足您在 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 它可以返回,以传递失败/未找到的更新的不同状态

        2
  •  0
  •   Arkadiusz Kałkus    7 年前

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