graphql-java使用手冊:part8 關於 Relay 支持

原文:http://blog.mygraphql.com/wordpress/?p=114java

關於 Relay 支持

包含了一些基礎的 Relay 特性的支持。git

注意: 這裏的 Relay 指 「Relay Classic」, 暫不支持 「Relay Modern」.github

完整的例子,見 https://github.com/graphql-ja...服務器

Relay 以 JSON 格式,向服務器發送 queryvariables
兩個字段。query 字段是一個 JSON 格式的字符串, variables 字段是一個
變量定義( variable definitions) 的 map。relay 兼容的服務器,須要解釋
JSON 而後傳 query 字符串到本框架。包括 variables map 做爲 execute
方法的第3個參數。以下:mvc

@RequestMapping(value = "/graphql", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object executeOperation(@RequestBody Map body) {
    String query = (String) body.get("query");
    Map<String, Object> variables = (Map<String, Object>) body.get("variables");
    if (variables == null) {
        variables = new LinkedHashMap<>();
    }
    ExecutionResult executionResult = graphql.execute(query, (Object) null, variables);
    Map<String, Object> result = new LinkedHashMap<>();
    if (executionResult.getErrors().size() > 0) {
        result.put("errors", executionResult.getErrors());
        log.error("Errors: {}", executionResult.getErrors());
    }
    result.put("data", executionResult.getData());
    return result;
}

Apollo 支持

沒有爲對接 Apollo 客戶端作什麼。因它兼容全部schema。app

上面的 Controller 例子同樣能夠與 Apollo 客戶端交互。框架

相關文章
相關標籤/搜索