GraphQL错误字段类型必须是输入类型,但得到: [英] GraphQL Error field type must be Input Type but got:

查看:473
本文介绍了GraphQL错误字段类型必须是输入类型,但得到:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是突变:

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

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

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

    },
});

以下是变异详细信息字段中使用的模式:

Here is the schema used in the details field of the mutation:

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;
            },
        },

    }),

});

我在运行此代码时出错:

Error I am getting on running this code is :

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)

我用过这种语法与查询和他们正常工作。但是,他们正在返回带有突变的错误。
我的代码中有什么不正确以及如何更正?

I have used this syntax with queries and they worked correctly. But, they are returning error with a mutation. What is incorrect in my code and how do I correct it?

推荐答案

在GraphQL中,输入不能用作类型类型不能用作输入。不幸的是,即使结构与现有类型相同,您也必须定义一个单独的输入以用作参数。尝试这样的事情:

In GraphQL, an input cannot be used as a type and a type cannot be used as an input. Unfortunately, even if the structure is identical to an existing type, you always have to define a separate input to use as an argument. Try something like this:

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

这篇关于GraphQL错误字段类型必须是输入类型,但得到:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆