JayBauer
JayBauer

Reputation: 35

Passing a list of custom types to a GraphQL mutation

The goal of my app is for a user to be able to save and recall a list of forms to be filled out and edited. One user will have many forms, and a single form will be made up of several fields of its own.

If my have my types set up like this, for example:

const FormType = new GraphQLObjectType({
  name: 'FormType',
  fields: {
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    email: { type: GraphQLString }
  }
});
const UserType = new GraphQLObjectType({
  name: 'UserType',
  fields: {
    id: { type: GraphQLID },
    email: { type: GraphQLString },
    firstName: { type: GraphQLString, default: '' },
    lastName: { type: GraphQLString, default: '' },
    phone: { type: GraphQLString, default: '' },
    forms: { type: new GraphQLList(FormType) }
  }
});

And my mutation like this:

const mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    saveForm: {
      type: UserType,
      args: {
        userID: { type: GraphQLID },
        form: { type: FormType } // ???
      },
      resolve(parentValue, { userID, form }) {
        // I'll figure out what to do here when the time comes
      }
    }
  }
});

I can't seem to create a mutation that works with GraphQLList. I keep getting this error:

Error: Mutation.saveForm(form:) argument type must be Input Type but got: FormType.

...which my research is telling me means that FormType needs to be a GraphQLInputObjectType. When I change it accordingly, I just get the error:

Error: Mutation.saveForm(form:) argument type must be Output Type but got: FormType.

So nothing really changes. Does anyone know of a working example of this problem?

Upvotes: 1

Views: 815

Answers (1)

Ed.
Ed.

Reputation: 2062

The problem you are seeing is because while you've correctly changed your input type to be a GraphQLInputObjectType, that changes the type of field UserType.forms to an input type too - remember UserType is an output type. Here you would need to make a FormType and also a FormInputType, and use each in the appropriate place.

Separately: why do you need a full input type for a form? Wouldn't it function equally well to just pass in the ID? Or, if the intention is to have FormType represent form data, why does it need an ID? Also, perhaps it could be named better to FormData?

Finally, while I believe this provides an answer, your question didn't provide completely cut+pasteable code for a minimal, complete and verifiable example so that I could easily check.

Upvotes: 1

Related Questions