Как использовать JSON Scalar в SPQR - PullRequest
0 голосов
/ 28 февраля 2019

Я хочу вернуть литерал JSON в классе обслуживания

@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\"    }  ]}"
  }
}

Как удалить символы Escape?

Ответы [ 2 ]

0 голосов
/ 28 февраля 2019

Решение @kaqqao - лучшая практика на мой взгляд.Но если вы хотите вернуть только простую строку, сделайте это с заменой старой доброй строки:

return genratedSchema.replace("\\", "");
0 голосов
/ 28 февраля 2019

Ответ GraphQL уже в формате JSON, поэтому любые строки внутри, очевидно, должны быть экранированы соответствующим образом.Если вы хотите добавить в него динамический объект, вы должны вернуть объект, а не строку.Объект может быть любым, имеющим правильную структуру, может быть Map, Джексона ObjectNode, Гсона 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 ...;
}
...