0
votes

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

2
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

2 Answers

2
votes

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
}
0
votes

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.