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
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

查询变量:
{
  "propertyId" : "23456832-7862-5679-4342-415f32385830",
  "first": 20
}

使用此代码段,我可以将查询发送到外部服务器:
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);

收到以下回复:
{
"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
                                }
                            }
                        }
                    ]
                }
            }
         }
      ]
    }
  }

使用这些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>

如何整理对POJO和/或POJO的ArrayList的响应?

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

顺便说一下,第三方服务器不提供GraphQL模式文件。

最佳答案

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

  • Convert XML or JSON to Java Pojo Classes - Online
  • Json2Pojo Intellij Plugin

  • 一旦有了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);
    

    关于java - 如何解析和编码(marshal)GraphQL对Java POJO的响应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50360295/

    10-15 14:35