使用 GQLObject 作为变异的参数?

Use a GQLObject as arg for a mutation?

我在服务器端(nodeJS)有以下变化(导入 RequiredDataType):

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;

当我使用此代码时出现以下错误:"module initialization error: Error"

如果我将突变中的 RequiredDataType 更改为 GraphQLString,它可以正常工作,但我无法使用我需要的对象:)

最后我将发送并处理以下数据结构:

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

在客户端(带有 apollo-client 的 reactJS)我使用以下 gql-tag 代码:

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

但首先它在服务器上的突变声明上崩溃。那么是不是不能使用 GQLObject 作为突变的参数,或者我的代码错误在哪里?

感谢您的帮助!

最佳,

法比安

很遗憾,类型不能代替输入,输入也不能代替类型。这是设计使然。来自 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.

您可以查看 了解有关 为什么

的更多详细信息

您需要将 RequiredDataType 定义为 GraphQLInputObjectType,而不是 GraphQLObjectType,才能让您的变异工作。如果您也需要它作为 GraphQLObjectType,则需要将它们声明为两种不同的类型——类似于 RequiredDataTypeRequiredDataInput.