如何解析和编写对Java POJO的GraphQL响应?

Pac*_*ver 5 java graphql

GraphQL很新...

我将GraphQL查询发送到由第三方控制的外部服务器.

使用此GraphQL查询:

query Photos($first: Int!, $propertyId: String) {
  viewer {
    content {
      contentLists(first: $first, property_id: $propertyId, contentListType: IMAGE, publishState: PUBLISHED, orderBy: originalPublishDate, orderByDirection: DESC) {
        edges {
          node {
            id
            title
            caption
            originalPublishDate
            lastModifiedDate
            contents(first: $first) {
              edges {
                node {
                  id
                  title
                  caption
                  type
                  ... on Image {
                    asset {
                      createdDate
                      lastModifiedDate
                      id
                      url
                      title
                      height
                      width
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

查询变量:

{
  "propertyId" : "23456832-7862-5679-4342-415f32385830",
  "first": 20
}
Run Code Online (Sandbox Code Playgroud)

使用此代码段,我可以向外部服务器发送查询:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+accessToken);

HttpEntity<String> entity = new HttpEntity<String>(request,headers);
String response = restTemplate.postForObject(EXTERNAL_URL, entity, String.class);
Run Code Online (Sandbox Code Playgroud)

收到以下回复:

{
"data": {
    "viewer": {
        "content": {
            "contentLists": {
                "edges": [
                {
                    "node": {
                    "id": "3510e8f7-452s-f531-43b0-ac36ba979e5a9f4m",
                    "title": "Homer Simpson goes Camping",
                    "caption": "Homer Simpson goes Camping",
                    "originalPublishDate": "2018-02-18T03:31:56.352Z",
                    "lastModifiedDate": "2018-02-18T03:32:13.530Z",
                    "contents": {
                        "edges": [
                        {
                            "node": {
                                "id": "3506d951-1332-86fg-42bc-b11183db50394f40",
                                "title": "No Title",
                                "caption": "",
                                "type": "IMAGE",
                                "asset": {
                                    "createdDate": "2018-02-18T03:31:38.406Z",
                                    "lastModifiedDate": "2018-02-18T03:31:38.991Z",
                                    "id": "65037262-7169-7065-7861-7035676a6f65323467617866",
                                    "url": "",
                                    "title": "No Title",
                                    "height": null,
                                    "width": null
                                }
                            }
                        }
                    ]
                }
            }
         }
      ]
    }
  }
Run Code Online (Sandbox Code Playgroud)

使用这些Java库:

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java</artifactId>
    <version>8.0</version>
</dependency>

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java-tools</artifactId>
    <version>5.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如何编组对POJO的POJO和/或ArrayList的响应?

是否有更好的库可以帮助我使用Java解析/编组GraphQL?

顺便说一句,第三方服务器不提供GraphQL架构文件......

fla*_*kes 2

如果您只需要生成这些类一次并在之后的每次调用中使用它们,那么您可以下载一个 JSON 到 POJO 映射工具。有很多可用的实用程序;只是在快速搜索中我找到了几个选项:

一旦你有了 POJO 类,你就可以使用标准的 jackson 映射器来获取类

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+accessToken);

HttpEntity<String> entity = new HttpEntity<String>(request,headers);
String rawResponse = restTemplate.postForObject(EXTERNAL_URL, entity, String.class);

ObjectMapper objectMapper = new ObjectMapper();
PhotoQueryResponse response = objectMapper.readValue(rawResponse, PhotoQueryResponse.class);
Run Code Online (Sandbox Code Playgroud)