2017-09-24 56 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如果它準確回答您的問題,請將答案標記爲已接受:) –