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?

SaiyanGirl
  • 16,376
  • 11
  • 41
  • 57
  • 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](https://stackoverflow.com/questions/54119116/array-of-objects-apollo-server-and-post-from-react?noredirect=1#comment95072872_54119116), [here](https://stackoverflow.com/questions/52744900/apollo-graphql-type-must-be-input-type), and [here](https://stackoverflow.com/questions/46158288/graphql-how-to-reuse-same-type-for-query-and-mutation/46159440). – Daniel Rearden Jan 17 '19 at 02:14
  • 1
    Possible duplicate of [Apollo / GraphQl - Type must be Input type](https://stackoverflow.com/questions/52744900/apollo-graphql-type-must-be-input-type) – Daniel Rearden Jan 17 '19 at 02: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! – SaiyanGirl Jan 17 '19 at 02:27
  • 1
    Glad to be of help! Feel free to delete this question. – Daniel Rearden Jan 17 '19 at 02:56
  • Possible duplicate of [Array of Objects Apollo Server and post from react](https://stackoverflow.com/questions/54119116/array-of-objects-apollo-server-and-post-from-react) – SaiyanGirl Jan 17 '19 at 16:38

1 Answers1

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.

Christof Aenderl
  • 4,233
  • 3
  • 37
  • 48