75

Here is the mutation:

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?

lee huang
  • 1,209
  • 1
  • 12
  • 23

5 Answers5

99

In GraphQL, an input cannot be used as a type and a type cannot be used as an input. Unfortunately, even if the fields appear 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 }, 
  })
});

If using SDL, the same type would look like this:

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

Please see this answer for an extensive explanation of why it's necessary to do this.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
40

For those who are using graphql-tools and stumble accross this post, documentation is here at GraphQL's Website.

My Example using graphQL tools is here below: (this has an input inside of an input AKA an ImageInput inside the SocialPostInput)

//mutation file

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

//Schedule and SocialPost file

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
}`
Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75
15

If you describe schema within schema.graphql file, just replace type to input:

Wrong:

type SomeData {
  someField: String!
}

Right:

input SomeData {
  someField: String!
}
zemil
  • 3,235
  • 2
  • 24
  • 33
  • I follow your answer @zemil, and it works for me. Thank you. My question is: I have a large schema language, and i have to clone the type to make it an input. Is there existing options that will minimize the redundancy of it because their only difference is the type/input code, the fields are all the same. – Jur P Dec 23 '21 at 07:32
  • @JurP check https://stackoverflow.com/questions/52452982/reusing-input-type-as-fragment-in-graphql – zemil Dec 23 '21 at 09:17
3

Please change type CreateNotebookInput to input CreateNotebookInput

Phuc Trinh
  • 41
  • 1
  • 3
0

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

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

    }),

});
MD SHAYON
  • 7,001
  • 45
  • 38