Search code examples
javascriptnode.jsexpressgraphqlexpress-graphql

Graphql input type of array of objects


I have this schema:

type Preference{
    _id: ID!
    uID: ID!
    preferences: [Category!]!
}

type Category{
    name: String!
    subcategories: [String]!
}

type RootMutation{
    createPreference(perferenceInput: PerferenceInput) : Preference
}

input PerferenceInput{
    uID: String!
    preferences: [Category!]!
}

schema {    
    query: RootQuery
    mutation: RootMutation
}

type RootQuery {
    preferences: [Preference!]!
    category: [Category!]!
}

But Graphql gives me an error that states: message": "The type of PerferenceInput.preferences must be Input Type but got: [Category].", which I have traced back to be related to graphql not being able to parse the [Category] array.

I need to have this array as a nested array inside the Preference object, but somehow graphql is not able to parse this.. so, how can I pass the Category array to as an input type ?


Solution

  • This is well-known GraphQL limitation.

    You can't use regular GraphQLObjectTypes as the type for an GraphQLInputObjectType field – you must use another GraphQLInputObjectType.

    You can't mix input and output types in your schema. Input and output types have different type scope.