Apollo / GraphQl – Type must be Input type

From the spec:

Fields may accept arguments to configure their behavior. These inputs are often scalars or enums, but they sometimes need to represent more complex values.

A GraphQL Input Object defines a set of input fields; the input fields are either scalars, enums, or other input objects. This allows arguments to accept arbitrarily complex structs.

In other words, you can’t use regular GraphQLObjectTypes as the type for an GraphQLInputObjectType field — you must use another GraphQLInputObjectType.

When you write out your schema using SDL, it may seem redundant to have to create a Load type and a LoadInput input, especially if they have the same fields. However, under the hood, the types and inputs you define are turned into very different classes of object, each with different properties and methods. There is functionality that is specific to a GraphQLObjectType (like accepting arguments) that doesn’t exist on an GraphQLInputObjectType — and vice versa.

Trying to use in place of another is kind of like trying to put a square peg in a round hole. “I don’t know why I need a circle. I have a square. They both have a diameter. Why do I need both?”

Outside of that, there’s a good practical reason to keep types and inputs separate. That’s because in plenty of scenarios, you will expose plenty of fields on the type that you won’t expose on the input.

For example, your type might include derived fields that are actually a combination of the underlying data. Or it might include fields to relationships with other data (like a friends field on a User). In both these case, it wouldn’t make sense to make these fields part of the data that’s submitted as as argument for some field. Likewise, you might have some input field that you wouldn’t want to expose on its type counterpart (a password field comes to mind).

Leave a Comment