0

I've read everything, understood no solution and concrete explanation (even here: Apollo / GraphQl - Type must be Input type)

I want to create an object System that contains Suns. So I do:

  type System {
   _id: ID
   name: String! @unique
   diameter: Int
   suns: [Sun]
  }

  type Sun {
   _id: ID
   name: String
   diameter: Int
  }

  type Mutation {
   createSystem(name: String!, diameter: Int, suns: [Sun]): System!
  }

And I write in playground:

mutation {
  createSystem(name:"new system", suns: [{ name: "John" }]) {
    name
    suns
  }
}

But I got a terminal error: "Error: The type of Mutation.createSystem(suns:) must be Input Type but got: [Sun]."

I understand that Sun isn't received as an object. How to declare it as an object?

Thank you very much for your answers

  • 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 Dec 17 '18 at 14:39
  • Thank you @DanielRearden. Actually, I already read this post and didn't understand AT ALL how to implement an object type inside another object type... Thanks for help!:) – Guillaume Duhan Dec 17 '18 at 17:54

2 Answers2

2

The GraphQL spec. does not allow using type (i.e. output type) as the input argument.It only allows the input arguments to be enum , Scalar and Input . That means you have to create a SunInput

input SunInput {
   _id: ID
   name: String
   diameter: Int
}
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Thank you very much for your help Ken. But I still don't understand how to implement this SunInput into my mutation? createSystem(name: String!, diameter: Int, suns: [SunInput]): System! Doesn't work ! – Guillaume Duhan Dec 17 '18 at 17:53
  • @AlexandreLafayette, this should resolve the issue, what error you are getting? – Amit Bhoyar Jan 23 '19 at 19:25
0

You need to make a custom "Type" for sun with its own resolver.

suns: { type: new GraphQLList(SunType) } // just an example

mutation {
  createSystem(name:"new system", suns-names: "John") {
    name
  }
}

It will have resolver that writes a new system to the database called new system that adds a sun of "SunType" to the a database collection with the name of "Sun" for example.

Jeremy Scott Peters
  • 377
  • 1
  • 3
  • 15