简体   繁体   中英

How to execute a mutation in GraphQL?

In GraphQL we have basically two types of operations: queries and mutations. While queries are well described in the documentation and there are many examples of them, I'm having a hard time to understand how to execute a mutation. Mutations obviously are update methods.

I've created very simple Node.js server:

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

In memory database is just in an array of User objects {id, name} :

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

module.exports = {
  inMemoryDatabase
};

Executing query to get user by id looks as follows:

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

How would the mutation changing user name look like?

Hey may completely be missing what you are saying, but the way that I look at a mutation is like this

  • I get some arguments and a field, that is the same thing as params and a path in rest, with those i do something (in your case lookup the user and update the attribute based on the arguments passed in
  • After That, i return something from the resolve function that will fulfill the type you specify in the type of the mutation

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

Then with that as a context, you pass some arguments and request back a user

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

In a normal production schema you would also normally have something like errors that could be returned to convey the different states of the update for failed/not found

@Austio's answer was pretty close, but the proper way is:

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

if we connect directly with MongoDB below will help you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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