3

I have a React client-side project and a Node.js/GraphQL api in two separate repo's.

In my React app, I want to pass an object as variable type into my mutation. Here's how my mutation looks like:

export const CREATE_SPEAKER = gql`

  input Expertise {
    title: String!
    domain: String!
  }

  mutation CreateSpeaker(
    $name: String!
    $age: String!
    $nationality: String!
    $avatar: String!
    $expertise: Expertise!
  ) {
    createSpeaker(
      speakerInput: {
        name: $name
        age: $age
        nationality: $nationality
        avatar: $avatar
        expertise: $expertise
      }
    ) {
      name
      age
      nationality
      avatar
      expertise {
        title
        domain
      }
    }
  }
`;

In my Node.js project I have the following schema:

input SpeakerInput {
      name: String!
      age: String!
      expertise: ExpertiseInput!
      nationality: String!
      avatar: String
}

input ExpertiseInput {
      title: String!
      domain: String!
}

And my resolver:

createSpeaker: async args => {
    const { name, age, nationality, avatar, expertise } = args.speakerInput;

    const newSpeaker = new Speaker({
      name,
      age,
      nationality,
      avatar,
      expertise: {
        title: expertise.title,
        domain: expertise.domain
      }
    });

    try {
      return await newSpeaker.save();
    } catch (error) {
      throw ("Failed to create speaker:: ", error);
    }
}

But I'm getting the following error when trying to create the speaker:

Uncaught (in promise) Invariant Violation: Schema type definitions not allowed in queries. Found: "InputObjectTypeDefinition"

Any suggestions/ideas how to do this?

1 Answer 1

7

You can't define additional types when sending requests to a GraphQL service and you don't need to -- just use the types you've already defined on the server (in this case ExpertiseInput:

$expertise: ExpertiseInput!

However, there's no need to use this many variables in the first place:

mutation CreateSpeaker($input: SpeakerInput!) {
  createSpeaker(speakerInput: $input) {
    name
    age
    nationality
    avatar
    expertise {
      title
      domain
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.