簡體   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