繁体   English   中英

GraphQL 错误字段类型必须是输入类型,但得到:

[英]GraphQL Error field type must be Input Type but got:

这是突变:

const createNotebook = mutationWithClientMutationId ({
    name: 'CreateNotebook',
    inputFields: {
        token: {
            type: GraphQLString,
        },

        details: {
            type: NotebookDetails,
        },
    },
    outputFields: {

    },
    async mutateCRNotebook(input, context) {
        const data = getJSONFromRelativeURL(input.token);

    },
});

这是突变的详细信息字段中使用的模式:

const NotebookDetails = new GraphQLObjectType({
    name: 'NotebookDetails',
    interfaces: [nodeInterface],

    fields: () => ({
        id: globalIdField('NotebookDetails'),

        description: {
            type: GraphQLString,
            description: '...',
            resolve(obj) {
                return obj.description;
            },
        },

        language: {
            type: GraphQLString,
            description: '...',
            resolve(obj) {
                return obj.language;
            },
        },

    }),

});

我在运行此代码时遇到的错误是:

api_1    | Error: CreateNotebookInput.details field type must be Input Type but got: NotebookDetails.
api_1    |     at invariant (/usr/src/app/node_modules/graphql/jsutils/invariant.js:19:11)
api_1    |     at /usr/src/app/node_modules/graphql/type/definition.js:698:58
api_1    |     at Array.forEach (native)
api_1    |     at GraphQLInputObjectType._defineFieldMap (/usr/src/app/node_modules/graphql/type/definition.js:693:16)
api_1    |     at GraphQLInputObjectType.getFields (/usr/src/app/node_modules/graphql/type/definition.js:682:49)
api_1    |     at typeMapReducer (/usr/src/app/node_modules/graphql/type/schema.js:224:26)
api_1    |     at typeMapReducer (/usr/src/app/node_modules/graphql/type/schema.js:190:12)
api_1    |     at Array.reduce (native)
api_1    |     at /usr/src/app/node_modules/graphql/type/schema.js:217:36
api_1    |     at Array.forEach (native)
api_1    |     at typeMapReducer (/usr/src/app/node_modules/graphql/type/schema.js:210:27)
api_1    |     at Array.reduce (native)
api_1    |     at new GraphQLSchema (/usr/src/app/node_modules/graphql/type/schema.js:98:34)
api_1    |     at Object.<anonymous> (/usr/src/app/src/schema/index.js:39:16)
api_1    |     at Module._compile (module.js:569:30)
api_1    |     at Object.Module._extensions..js (module.js:580:10)

我已将此语法与查询一起使用,并且它们工作正常。 但是,他们正在返回带有突变的错误。 我的代码中有什么不正确的地方,我该如何纠正?

在 GraphQL 中, input不能用作typetype也不能用作input 不幸的是,即使字段看起来与现有类型相同,您也始终必须定义单独的输入以用作参数。 尝试这样的事情:

const NotebookDetailsInput = new GraphQLInputObjectType({
  name: 'NotebookDetailsInput',
  fields: () => ({
    id:          { type: GraphQLID },
    description: { type: GraphQLString },
    language:    { type: GraphQLString }, 
  })
});

如果使用 SDL,相同的类型将如下所示:

input {
  id: ID
  description: String
  language: String
}

请参阅此答案以详细解释为什么有必要这样做。

对于那些使用graphql-tools并偶然发现这篇文章的人, GraphQL的网站上提供了文档。

我使用 graphQL 工具的示例如下:(这在输入 AKA 中有一个输入,在SocialPostInput中有一个ImageInput

//变异文件

extend type Mutation {
    SchedulePost ( 
        socialPost: SocialPostInput,
        schedule: ScheduleInput
     ): ScheduledPost
}`

//时间表和SocialPost文件

type Image {
    id: String
    url: String
}
type SocialPost {
    id: String
    GCID: String
    message: String
    image: Image
}
input ImageInput {
    url: String
}
input SocialPostInput {
    GCID: String
    message: String
    image: ImageInput
}
type Schedule {
    id: String
    month: Int
    date: Int
    hour: Int
    minute: Int
}
input ScheduleInput {
    id: String
    month: Int
    date: Int
    hour: Int
    minute: Int
}`

如果您在 schema.graphql 文件中描述模式,只需将类型替换为输入:

错误的:

type SomeData {
  someField: String!
}

正确的:

input SomeData {
  someField: String!
}

请将type CreateNotebookInput更改为input CreateNotebookInput

这是突变的详细信息字段中使用的模式

const NotebookDetails = new GraphQLObjectType({
    name: 'NotebookDetails',
    fields: () => ({
        id: { type: GraphQLID },
        description: { type: GraphQLString },
        language: { type: GraphQLString },

    }),

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM