首页 文章

如何在GraphQL中执行变异?

提问于
浏览
0

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

在内存数据库中只是一个User对象数组 {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 回答

  • 2

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

    • 我得到了一些参数和一个字段,这与params和一个休息路径是一回事,我做了一些事情(在你的情况下查找用户并根据传入的参数更新属性)

    • 之后,我从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 的内容,可以返回以传达更新的不同状态以查找失败/未找到

  • 0

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

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

相关问题