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...
jacksonlibrary and map it to a POJO from the response data. baeldung.com/jackson-object-mapper-tutorial. If the schema is not there then POJOs need to be described oneway or the other