如何在 SPQR 中使用 JSON 标量

How to use JSON Scalar in SPQR

我想 return 服务中的 JSON 文字 class

@GraphQLQuery(name = "renderUI", description = "Schema for your form")
public String renderUI() {
     String genratedSchema = "{" +
            "  \"schema\": {" +
            "    \"type\": \"object\"," +
            "    \"id\": \"urn:jsonschema:profile:model:DemoForm\"," +
            "    \"properties\": {" +
            "      \"comment\": {" +
            "        \"type\": \"string\"," +
            "        \"title\": \"Comment\"" +
            "      }" +
            "    }" +
            "  }," +
            "  \"form\": [" +
            "    {" +
            "      \"key\": \"comment\"," +
            "      \"type\": \"textarea\"," +
            "      \"required\": false," +
            "      \"description\": \"Add your Comment here\"," +
            "      \"placeholder\": \"fill your comment please\"" +
            "    }" +
            "  ]" +
            "}";
    return  genratedSchema;
}

上面的代码转义了响应中的所有引号

{
  "data": {
    "renderUI": "{  \"schema\": {    \"type\": \"object\",    \"id\": \"urn:jsonschema:com:fnstr:bankprofile:gppbankprofile:model:DemoForm\",    \"properties\": {      \"comment\": {        \"type\": \"string\",        \"title\": \"Comment\"      }    }  },  \"form\": [    {      \"key\": \"comment\",      \"type\": \"textarea\",      \"required\": false,      \"description\": \"Add your Comment here\",      \"placeholder\": \"fill your comment please\"    }  ]}"
  }
}

如何去除转义字符?

GraphQL 响应已经是 JSON,因此显然需要对其中的任何字符串进行适当转义。 如果要向其中添加动态对象,实际上必须 return 一个对象,而不是字符串。对象可以是任何具有正确结构的东西,可以是 Map、Jackson 的 ObjectNode、Gson 的 JsonObject 或 POJO。

例如

@GraphQLQuery(name = "renderUI", description = "Schema for your form")
public Map<String, Object> renderUI() {
    Map<String, Object> dynamic = new HashMap<>();
    dynamic.put("schema", ...); //fill the whole structure
    return dynamic;
}

@GraphQLQuery(name = "renderUI", description = "Schema for your form")
public ObjectNode renderUI() {
    return ...;
}