graphql-java - 如何将 JSONObject 值分配给 graphql 模式文件中的字段?
问题描述
我正在使用 graphQL 和 spring boot 框架开发一个应用程序。在这里,我使用 GraphQL 的 Schema-first 方法来编写我的模式详细信息。在我的场景中,我需要将 JSONObject 和 JSONArray 作为输入分配给 Schema 文件中的几个字段。在 graphQL 模式文件中,我们不能直接将 JSONObject/JSONArray 设置为数据类型。有人可以指导我如何在 graphQL 中处理 JSONObject/JSONArray 吗?
解决方案
您可以定义自己的标量类型 JSON:
scalar JSON
type Foo {
field: JSON
}
然后实现一个新的 java GraphQLScalarType bean:
@Component
public class JSONScalarType extends GraphQLScalarType {
JSONScalarType() {
super("JSON", "JSON value", new Coercing<Object,Object>() {
...
});
}
}
最后实现标量强制。您可以在此处找到有关标量类型的教程https://www.graphql-java.com/documentation/v15/scalars/
或者,在 graphql 中,JSON 是一个对象,您可以查看 graphql-java-extended-scalars 如何实现 Object 和 JSON scalars:https ://github.com/graphql-java/graphql-java-extended -scalars/blob/master/src/main/java/graphql/scalars/object/ObjectScalar.java
另一种解决方案是使用 graphql-java-extended-scalars 库:https ://github.com/graphql-java/graphql-java-extended-scalars
推荐阅读
- javascript - 为什么我的 JavaScript 不能在 HTML 可视代码中运行
- angular - 在 Angular 表单组中传递数据
- java - 如何实现图书排名 - JAVA
- python - 当我们想在两列之间加入字符串时,如何在 sep 上设置条件?
- marklogic - Marklogic Datahub:在 Data Hub 中创建“映射”步骤时出现 JSON 文件内容问题
- javascript - useState 更改不会在函数中更新
- python - AWS Lambda Boto3 DynamoDB 并发
- c# - 如何使用 C#/.Net 从 AWS S3 下载文件列表到我的设备?
- tensorflow - 在 Pytorch 中制作 tf.nn.conv2d_transpose
- python - Python For 循环和 API