【问题标题】:Syntax Error GraphQL Expected Name, found String语法错误 GraphQL 预期名称,找到字符串
【发布时间】:2020-10-03 08:12:30
【问题描述】:

我正在尝试在 GraphQL 中实现突变。我正在使用 GraphQL Play Ground Ui 进行查询。

这是我的突变:

mutation{
  createProduct (data :{
     product: "abc", 
     product_status : {
      "1" : {
      order : "done"    
}

}

这是我的 TypeDef

type Product { 
product : String,
product_status : JSON
}

但我在 product_status excepted Name but found String 中遇到错误,因为对象包含 1 作为字符串。我该如何解决这个问题。我需要将这种类型的对象存储在我的数据库中。有人可以帮我解决这个问题。

Error Image

【问题讨论】:

  • product_status 不是JSON 类型。事实上,GraphQL 中没有这种类型,除非您创建自定义类型。 product_status 在您的第一个代码中 sn-p 看起来像一个对象,因此您需要为此创建一个自定义类型。

标签: node.js graphql syntax-error apollo apollo-server


【解决方案1】:

GraphQL 支持严格的数据类型。所以基本上你不能将字符串作为属性键,因为这意味着它不知道要反序列化成什么。另外我不确定 JSON 是否是一种数据类型。我知道字符串和类型,但不知道 JSON。你可以有一个像这样的对象:

type Product { 
    product : String,
    product_status : ProductStatus
}

type ProductStatus {
    id: ID,
    order: String,
}

这就是你可以设计它的方式。

【讨论】:

    猜你喜欢
    • 2021-02-18
    • 2018-06-28
    • 2020-01-01
    • 1970-01-01
    • 2021-07-28
    • 2023-03-31
    • 1970-01-01
    • 2021-12-09
    • 2019-04-05
    相关资源
    最近更新 更多