2016-07-19 72 views
0

下面我有一個queryrequestString和一個mutationrequestString如何從GraphQL變量訪問variableValues?

這兩個查詢都運行,我正在記錄兩個resolve方法。

問題:

  1. 我如何從resolve處理程序mutation訪問{name: 'Thomas'}variableValues
  2. 由於某種原因,傳遞給resolve突變體的第二個參數本身就是require。這是爲什麼?

代碼:

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) 

}) 

結果:

[email protected]: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' } } } 

回答

1

它看起來像節點環境,或者你的執行上下文,是提供了一個arguments對象,並不代表你的resolve的論據方法。

如果使用命名參數它工作得很好:

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