如何将 JSON 值从 GrapgQL 传递到后端?

问题描述 投票:0回答:1

使用扩展的 grapgQL 标量,我们可以在 JSON 标量的帮助下将“Map”值显示为 JSON。但是如何在存储数据的同时传递 JSON 值呢?

架构:

Type Mutation{
               createProduct(ProductRequest: ProductInput) : Product
             }

input ProductInput{
    id : Int
    name: String
    attributes: JSON
}

查询:

mutation {
  createProduct(ProductRequest: {
        id:1,
        name:"ABC"
  attributes:{
    Key: "Value"
  }
  })
  {
    id
    name
    attributes
  }
}

如果我这样通过,对于“属性”,它将被视为空值。所以没有任何东西被存储在数据库中。 如果你知道在 graphQL 中传递 JSON 值,请告诉我。

我尝试使用“PostMapping”传递数据并且成功了!但我需要从 GraphQL 传递。

谢谢。

java spring-boot graphql graphql-java scalar
1个回答
0
投票

在 GraphQL 中存储数据时传递 JSON 值 我尝试使用您已经在模式中定义的 JSON 标量类型。要传递 JSON 值,您只需提供一个有效的 JSON 字符串作为突变中属性字段的值。 我试过你的代码,这对我有用:

mutation {
  createProduct(ProductRequest: {
    id: 1,
    name: "ABC",
    attributes: "{\"Key\": \"Value\"}"
  }) {
    id
    name
    attributes
  }
}

在这段代码中,我传递了一个 JSON 字符串作为属性字段的值。哪个应该可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.