GraphQL没有对象类型?

GraphQL没有对象类型?,graphql,apollo,apollo-server,Graphql,Apollo,Apollo Server,我什么都读过,不懂解决方案和具体解释(即使在这里:) 我想创建一个包含太阳的对象系统。因此,我: type System { _id: ID name: String! @unique diameter: Int suns: [Sun] } type Sun { _id: ID name: String diameter: Int } type Mutation { createSystem(name: String!, d

我什么都读过,不懂解决方案和具体解释(即使在这里:)

我想创建一个包含太阳的对象系统。因此,我:

  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!
  }
我在操场上写下:

mutation {
  createSystem(name:"new system", suns: [{ name: "John" }]) {
    name
    suns
  }
}
但我得到了一个终端错误:“错误:突变的类型。createSystem(suns:)必须是输入类型,但得到:[Sun]”

我知道太阳不是一个物体。如何将其声明为对象


非常感谢您的回答

GraphQL规范不允许使用
类型
(即输出类型)作为输入参数。它只允许输入参数为
枚举
标量
输入
。这意味着您必须创建一个
SunInput

input SunInput {
   _id: ID
   name: String
   diameter: Int
}

GraphQL规范不允许使用
类型
(即输出类型)作为输入参数。它只允许输入参数为
枚举
标量
输入
。这意味着您必须创建一个
SunInput

input SunInput {
   _id: ID
   name: String
   diameter: Int
}
您需要使用自己的解析器为sun创建自定义“类型”

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

mutation {
  createSystem(name:"new system", suns-names: "John") {
    name
  }
}
它将有一个解析器,该解析器将一个新系统写入名为new system的数据库,该数据库将一个名为“sun”的sun添加到数据库集合中,例如,名为“sun”的数据库集合。

您需要使用自己的解析器为sun创建一个自定义的“Type”

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

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

它将有一个解析器,该解析器将一个新系统写入名为new system的数据库,该解析器将一个名为“sun”的sun添加到数据库集合中。例如,“sun”的sun可能与Thank you@DanielRearden重复。事实上,我已经读过这篇文章,根本不明白如何在另一个对象类型中实现一个对象类型。。。谢谢你的帮助!:)可能是DanielRearden的“谢谢你”的副本。事实上,我已经读过这篇文章,根本不明白如何在另一个对象类型中实现一个对象类型。。。谢谢你的帮助!:)非常感谢你的帮助,肯。但我仍然不明白如何将这个SunInput应用到我的变异中?createSystem(名称:String!,直径:Int,suns:[SunInput]):System!不行@AlexandreLafayette,这应该可以解决问题,你会遇到什么错误?非常感谢你的帮助Ken。但我仍然不明白如何将这个SunInput应用到我的变异中?createSystem(名称:String!,直径:Int,suns:[SunInput]):System!不行@AlexandreLafayette,这应该可以解决问题,你会遇到什么错误?