【问题标题】:GraphQL no object type?GraphQL 没有对象类型?
【发布时间】:2019-05-17 20:49:31
【问题描述】:

我已经阅读了所有内容,没有理解任何解决方案和具体解释(即使在这里:Apollo / GraphQl - Type must be Input type

我想创建一个包含 Suns 的对象 System。所以我这样做:

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

但我得到一个终端错误:“错误:Mutation.createSystem(suns:) 的类型必须是输入类型,但得到:[Sun]。”

我了解 Sun 不是作为对象接收的。如何声明为对象?

非常感谢您的回答

【问题讨论】:

  • 谢谢@DanielRearden。实际上,我已经阅读了这篇文章并且完全不明白如何在另一个对象类型中实现一个对象类型......谢谢你的帮助!:)

标签: graphql apollo apollo-server


【解决方案1】:

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

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

【讨论】:

  • 非常感谢肯的帮助。但是我仍然不明白如何将这个 SunInput 实现到我的突变中? createSystem(名称:字符串!,直径:Int,太阳:[SunInput]):系统!没用!
  • @AlexandreLafayette,这应该可以解决问题,您遇到了什么错误?
【解决方案2】:

您需要使用自己的解析器为 sun 制作自定义“类型”。

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

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

它将有一个解析器,它将一个新系统写入数据库,称为新系统,例如,将“SunType”的太阳添加到名为“Sun”的数据库集合中。

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 1970-01-01
    • 2018-08-15
    • 2020-05-29
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2015-08-21
    • 2021-03-20
    相关资源
    最近更新 更多