Der GraphQL-Fehlerfeldtyp muss Eingabetyp sein, hat aber Folgendes erhalten:

Lesezeit: 3 Minuten

Benutzeravatar von lee huang
lee huang

Hier die Mutation:

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

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

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

    },
});

Hier ist das Schema, das im Detailfeld der Mutation verwendet wird:

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

    }),

});

Der Fehler, den ich beim Ausführen dieses Codes bekomme, ist:

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)

Ich habe diese Syntax mit Abfragen verwendet und sie funktionierten korrekt. Aber sie geben einen Fehler mit einer Mutation zurück. Was ist in meinem Code falsch und wie kann ich es korrigieren?

Benutzeravatar von Daniel Rearden
Daniel Rearden

In GraphQL, ein input kann nicht als verwendet werden type und ein type kann nicht als verwendet werden input. Selbst wenn die Felder mit einem vorhandenen Typ identisch erscheinen, müssen Sie leider immer eine separate Eingabe definieren, die als Argument verwendet werden kann. Versuchen Sie so etwas:

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

Bei Verwendung von SDL würde derselbe Typ wie folgt aussehen:

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

Bitte lesen Sie diese Antwort für eine ausführliche Erklärung von warum es ist notwendig, dies zu tun.

  • Es hat viel zu lange gedauert, bis ich das verstanden habe, aber es scheint ziemlich “offensichtlich” zu sein, wenn Sie es verstanden haben!

    – Kendall

    19. Oktober 2017 um 19:00 Uhr

  • Ihre Antwort sollte oben auf ihrer Website blinken

    – Mccc

    21. Februar 2022 um 15:13 Uhr

Für diejenigen, die verwenden graphql-tools und stolpere über diesen Beitrag, Dokumentation ist hier auf der Website von GraphQL.

Mein Beispiel mit graphQL-Tools ist hier unter: (dies hat eine Eingabe innerhalb einer Eingabe AKA an ImageInput innerhalb der SocialPostInput)

//Mutationsdatei

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

//Schedule und SocialPost-Datei

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
}`

Wenn Sie das Schema in der Datei schema.graphql beschreiben, ersetzen Sie einfach type durch input:

Falsch:

type SomeData {
  someField: String!
}

Rechts:

input SomeData {
  someField: String!
}

  • Ich folge deiner Antwort @zemil, und es funktioniert für mich. Danke dir. Meine Frage ist: Ich habe eine große Schemasprache und muss den Typ klonen, um ihn zu einer Eingabe zu machen. Gibt es vorhandene Optionen, die die Redundanz minimieren, da ihr einziger Unterschied der Typ-/Eingabecode ist, die Felder sind alle gleich.

    – Jur P

    23. Dezember 2021 um 7:32 Uhr

  • @JurP check stackoverflow.com/questions/52452982/…

    – job.js.org

    23. Dezember 2021 um 9:17 Uhr


Bitte austauschen type CreateNotebookInput zu input CreateNotebookInput

Hier ist das Schema, das im Detailfeld der Mutation verwendet wird

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

    }),

});

1438470cookie-checkDer GraphQL-Fehlerfeldtyp muss Eingabetyp sein, hat aber Folgendes erhalten:

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy