PacificNW_Lover
PacificNW_Lover

Reputation: 5374

How to Parse & Marshal GraphQL Response to Java POJOs?

Very new to GraphQL...

Am sending GraphQL queries to an external server that is controlled by a 3rd party.

Using this GraphQL query:

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

Query Variables:

{
  "propertyId" : "23456832-7862-5679-4342-415f32385830",
  "first": 20
}

Using this code snippet, I am able to send a query to the external server:

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);

Received the following response:

{
"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
                                }
                            }
                        }
                    ]
                }
            }
         }
      ]
    }
  }

Using these Java libs:

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

How can I marshal the response to a POJO and / or ArrayList of POJOs?

Is there a better set of libs that could help me parse / marshal GraphQL using Java?

The 3rd party server does not provide the GraphQL schema file, by the way...

Upvotes: 6

Views: 10602

Answers (2)

Truth
Truth

Reputation: 76

You can check out this java library called,

Glitr (GraphQL to Java) , and the manual in here.

Here's its sweet supported types,

GraphQLString           String
GraphQLBoolean          Boolean
GraphQLInt              Integer
GraphQLFloat            Float
GraphQLObjectType       Any POJO
GraphQLInterfaceType    Any interface type
GraphQLUnionType        Not supported
GraphQLEnumType         Any enum type
GraphQLInputObjectType  Any POJO
GraphQLList             Any implementing class of Collection
GraphQLNonNull          See @GlitrNonNull

Upvotes: 1

flakes
flakes

Reputation: 23624

If you only need to generate these classes once and use them for every call there-after, then you could download a JSON to POJO mapping tool. There are a lot of utilities available; just in a quick search I was able to find few options:

Once you have the POJO classes you can use a standard jackson mapper to get the Class

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);

Upvotes: 2

Related Questions