2016-08-09 5 views
0

以下私はexampleという1つのオブジェクトを持つ突然変異credentialsを設定しようとしています。私は以前この作業をしていたので、突然JSONの部分で作業が停止しました。 credentialsでjsonを送信できないのはなぜですか?ここで突然変異の単一のJSON引き数

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

は誤りです:

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

    2: mutation { 
    3: example(credentials: {"name":"Thomas Reggi"}) { 
           ^
    4:  name, 
     ] } 

Nameへの参照はどこから来るのでしょうか?なぜこれはエラーを投げているのですか?

+0

FYI、あなたの 'requestString'に、あなたがここに正しく引用符で囲まれた文字列を持っていませんでした:'例(資格情報:{ "名: "トーマスReggi"})' –

+0

を私はタイプミスがあったが、私はまだすることができます – ThomasReggi

答えて

2

ただ難しい方法を見つけました。名前は引用符で囲まれているため、{"name": "Thomas Reggi"}は使用できません。

このクエリが機能します。

mutation { 
    example(credentials: {name: "Thomas Reggi"}) { 
    name, 
    age 
    } 
} 
+0

Apollo GraphQLのクライアントコードと複雑なパラメータの突然変異を見ていると本当に苦痛です。JSONを文字列化してフィールド名の引用符を削除すると効果があります。 –

関連する問題