简体   繁体   English

如何从GraphQL突变访问variableValues?

[英]How do I access variableValues from GraphQL mutation?

Below I have a query requestString and a mutation requestString . 下面我有一个query requestString和一个mutation requestString

Both queries are run and I'm logging both of the resolve methods. 这两个查询都在运行,并且我正在记录两个resolve方法。

Questions: 问题:

  1. How do I access {name: 'Thomas'} the variableValues from the resolve handler for the mutation ? 如何从resolve处理程序访问{name: 'Thomas'} variableValues来进行mutation
  2. For some reason the second argument passed into the resolve hander for the mutation is require itself. 由于某种原因,传递给该突变的resolve的第二个参数是require本身。 Why is that? 这是为什么?

Code: 码:

import Promise from 'bluebird'
import axios from 'axios'
import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString
} from 'graphql'

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

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      user: {
        type: userType,
        args: {
          id: { type: GraphQLString }
        },
        resolve: function () {
          console.log(arguments)
          return Promise.resolve({"name": 'meow'})
        }
      }
    }
  }),
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      createUser: {
        type: userType,
        args: {
          name: {
            name: 'name',
            type: GraphQLString
          }
        },
        resolve: () => {
          console.log(arguments)
          return Promise.resolve({"name": 'meow'})
        }
      }
    }
  })
})

var mutationRequest = `
mutation basic($name: String!) {
  createUser(name: $name) {
    name
  }
}
`

graphql(schema, mutationRequest, null, null, {name: 'Thomas'}).then(result => {

  console.log(result)

})

var queryRequest = `
query basic($id: String!) {
  user(id: $id) {
    name
  }
}
`

graphql(schema, queryRequest, null, null, {id: '1'}).then(result => {

  console.log(result)

})

Results: 结果:

thomasreggi@zx:super-octopus$ babel-node graphql-test.js 
{ '0': null,
  '1': { id: '1' },
  '2': null,
  '3': 
   { fieldName: 'user',
     fieldASTs: [ [Object] ],
     returnType: 
      GraphQLObjectType {
        name: 'User',
        description: undefined,
        isTypeOf: undefined,
        _typeConfig: [Object],
        _interfaces: [],
        _fields: [Object] },
     parentType: 
      GraphQLObjectType {
        name: 'Query',
        description: undefined,
        isTypeOf: undefined,
        _typeConfig: [Object],
        _interfaces: [],
        _fields: [Object] },
     path: [ 'user' ],
     schema: 
      GraphQLSchema {
        _queryType: [Object],
        _mutationType: [Object],
        _subscriptionType: undefined,
        _directives: [Object],
        _typeMap: [Object],
        _implementations: {} },
     fragments: {},
     rootValue: null,
     operation: 
      { kind: 'OperationDefinition',
        operation: 'query',
        name: [Object],
        variableDefinitions: [Object],
        directives: [],
        selectionSet: [Object],
        loc: [Object] },
     variableValues: { id: '1' } } }
{ '0': {},
  '1': 
   { [Function: require]
     resolve: [Function: resolve],
     main: 
      Module {
        id: '.',
        exports: {},
        parent: null,
        filename: '/Users/thomasreggi/Desktop/super-octopus/graphql-test.js',
        loaded: true,
        children: [Object],
        paths: [Object] },
     extensions: 
      { '.js': [Function],
        '.json': [Function],
        '.node': [Function],
        '.jsx': [Function],
        '.es6': [Function],
        '.es': [Function] },
     cache: {...requireCache}
    }
  '2': 
   Module {
     id: '.',
     exports: {},
     parent: null,
     filename: '/Users/thomasreggi/Desktop/super-octopus/graphql-test.js',
     loaded: true,
     children: [ [Object], [Object], [Object] ],
     paths: 
      [ '/Users/thomasreggi/Desktop/super-octopus/node_modules',
        '/Users/thomasreggi/Desktop/node_modules',
        '/Users/thomasreggi/node_modules',
        '/Users/node_modules' ] },
  '3': '/Users/thomasreggi/Desktop/super-octopus/graphql-test.js',
  '4': '/Users/thomasreggi/Desktop/super-octopus' }
{ data: { createUser: { name: 'meow' } } }
{ data: { user: { name: 'meow' } } }

It looks like the Node environment, or your execution context, is providing an arguments object that does not represent the arguments of your resolve method. 看来节点环境或您的执行上下文正在提供一个arguments对象,该对象不代表您的resolve方法的参数。

If you use named arguments it works just fine: 如果使用命名参数,则可以正常工作:

resolve: function(source, args, context, info) {
  // all arguments have the correct values
}

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

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