突变中的单个JSON参数

Tho*_*ggi 1 json graphql graphql-js

下面我试图example用一个object-arg 设置一个突变credentials。我以前曾进行过此工作,然后突然停止在JSON部分上工作失败。为什么我无法通过发送json credentials

import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLInputObjectType,
  GraphQLNonNull,
  graphql
} from 'graphql'


let requestType = new GraphQLInputObjectType({
  name: 'Request',
  fields: {
    name: {type: GraphQLString},
  }
})

let responseType = new GraphQLObjectType({
  name: 'Response',
  fields: {
    name: {type: GraphQLString},
    age: {type: GraphQLInt}
  }
})

let schema = new GraphQLSchema({
  query:  new GraphQLObjectType({
    name: 'Query',
    fields: {
      author: {
        type: GraphQLString,
        resolve: (source, args, context, info) => {
          return 'Thomas Reggi'
        }
      }
    }
  }),
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      example: {
        type: responseType,
        args: {
          credentials: {
            name: 'credentials',
            type: requestType
          }
        },
        resolve: (source, args, context, info) => {
          return {
            'name': 'Thomas Reggi',
            'age': 26
          }
        }
      }
    }
  })
})

let credentials = { name: "Thomas Reggi" }

let requestString = `
mutation {
  example(credentials: ${JSON.stringify(credentials)}) {
    name,
    age
  }
}`

graphql(schema, requestString).then(result => {
  console.log(result)
})
Run Code Online (Sandbox Code Playgroud)

这是错误:

{ errors: 
   [ Syntax Error GraphQL request (3:25) Expected Name, found String "name: "

     2: mutation {
     3:   example(credentials: {"name":"Thomas Reggi"}) {
                                ^
     4:     name,
      ] }
Run Code Online (Sandbox Code Playgroud)

参考Name来自何处?为什么这会引发错误?

Tho*_*ggi 5

刚发现困难的方法。您不能{"name": "Thomas Reggi"}因为名称用引号引起来。

该查询有效。

mutation {
  example(credentials: {name: "Thomas Reggi"}) {
    name,
    age
  }
}
Run Code Online (Sandbox Code Playgroud)