2

I'm trying to add a field to my Mutation that has an array in it with my own type. When I added the attraction the the createTour mutation, I started getting this error:

Error: The type of Mutation.createTour(attractions:) must be Input Type but got: [Attraction].

Here is the problematic Mutation

type Mutation {
    createTour(
    title: String!
    description: String!
    attractions: [Attraction]      <----- THIS LINE
    ): Tour
    addCommentToTour(id: ID!, comment: String!): Tour
}

And here are the typedefs I have as well.

type Attraction {
    title: String
    description: String
    coordinateLat: Int
    coordinateLong: Int
}

type Tour {
    id: ID!
    title: String!
    description: String!
    author: String!
    attractions: [Attraction]      
}

How can I get this Mutation to take in this array?

5
  • 1
    As the error indicates, only input types can be used for arguments, not object types. This question comes up a lot. Please see here, here, and here. Commented Jan 17, 2019 at 2:14
  • 1
    Possible duplicate of Apollo / GraphQl - Type must be Input type Commented Jan 17, 2019 at 2:15
  • @DanielRearden aaaahhhhhhh, thank you so much for pointing me in the right direction. I did see one of those answers and just changed the type to input; I didn't realize I needed to create a completely different input; thank you! Commented Jan 17, 2019 at 2:27
  • 1
    Glad to be of help! Feel free to delete this question. Commented Jan 17, 2019 at 2:56
  • Possible duplicate of Array of Objects Apollo Server and post from react Commented Jan 17, 2019 at 16:38

1 Answer 1

1

More or less already answered in the comment from DanielRearden.

All input properties for a mutation must be defined as input in your schema. You can't use the same type that you use for output.

In your case you must create an additional:

input AttractionInput {
  title: String
  description: String
  coordinateLat: Int
  coordinateLong: Int
}

The basic idea is, that your input could be different from the output. A bit of redundant code but the separation is clearer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.