首页 > 解决方案 > 语法错误 GraphQL 预期名称,找到字符串

问题描述

我正在尝试在 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 exceptioned Name 中遇到错误,但发现 String as object contains 1 as string。我该如何解决这个问题。我需要将这种类型的对象存储在我的数据库中。有人可以帮我解决这个问题。

错误图像

标签: node.jsgraphqlsyntax-errorapolloapollo-server

解决方案


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

type Product { 
    product : String,
    product_status : ProductStatus
}

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

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


推荐阅读