2017-09-24 27 views
0

我有以下突变在服务器端(的NodeJS)(RequiredDataType进口):使用GQLObject作为arg进行突变?

mutationA: { 
     type: MutationResponseType, 
     args: { 
     id: { 
      type: new GraphQLNonNull(GraphQLString) 
     }, 
     name: { 
      type: new GraphQLNonNull(GraphQLString) 
     }, 
     requiredData: { 
      type: new GraphQLNonNull(new GraphQLList(RequiredDataType)) 
     } 
     }, 
     async resolve(parentValue, { 
     id, 
     name, 
     requiredData 
     }, req) { 
     // Some Magic Code 
     } 
    }, 

的RequiredDataType编码如下(所有GraphQL东西都是进口的:)):

const RequiredDataType = new GraphQLObjectType({ 
    name: 'RequiredDataType', 
    fields: { 
    name: { 
     type: GraphQLString 
    }, 
    value: { 
     type: GraphQLString 
    }, 
    required: { 
     type: GraphQLBoolean 
    } 
    } 
}); 
module.exports = RequiredDataType; 

当我使用此代码我得到以下错误:“模块初始化错误:错误”

如果我将突变中的RequiredDataType更改为GraphQLString它没有任何错误,但我不能使用我需要的对象:)

最后我会发送和处理数据结构如下:

{ 
"name": "Hallo" 
"id": "a54de3d0-a0a6-11e7-bf70-7b64ae72d2b6", 
"requiredData": [ 
{ 
"name": "givenName", 
"value": null, 
"required": true 
}, 
{ 
"name": "familyName", 
"value": null, 
"required": false 
} 
] 
} 

在客户端(reactJS与阿波罗的客户端)我用下面的GQL标签代码:

export default gql` 
    mutation MutationA($id: String!, $name: String!, $requiredData: [RequiredDataType]!){ 
    mutationA(id: $id, name: $name, requiredData: $requiredData) { 
      id, 
      somethingElse 
     } 
    } 
`; 

但首先它在服务器上的突变声明上崩溃。那么是不是可以使用GQLObject作为变量的参数,或者在代码中出现错误?

谢谢你的帮助!

最佳,

的Fabian

回答

1

不幸的是,类型不能代替输入的使用,以及输入可以不到位的类型的使用。这是设计。从official specification

Fields can define arguments that the client passes up with the query, to configure their behavior. These inputs can be Strings or Enums, but they sometimes need to be more complex than this.

The Object type defined above is inappropriate for re‐use here, because Objects can contain fields that express circular references or references to interfaces and unions, neither of which is appropriate for use as an input argument. For this reason, input objects have a separate type in the system.

您可以检查this answer更多的细节到为什么

你需要定义RequiredDataType作为GraphQLInputObjectType,不是GraphQLObjectType,让你的工作突变。如果您也需要它作为GraphQLObjectType,则需要将它们声明为两种不同的类型 - 类似RequiredDataTypeRequiredDataInput

+0

嗨,感谢你的工作! – Developer94

+0

@ Developer94如果它准确回答您的问题,请将答案标记为已接受:) –

相关问题