6

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

4
  • Doesn't this help ? baeldung.com/graphql Commented May 18, 2018 at 5:58
  • @TarunLalwani - how can this help me if I told you the external server is closed and doesn't provide the GraphQL schema file. Their representative told me to parse the JSON natively. The tutorial you provided gives the schema / IDL. Thanks for reaching out, nonetheless. Commented May 18, 2018 at 6:42
  • I think you will still need to create the POJOs yourself as you do know the fields. Because there is no way for a lib to know how many objects to break this into. Or you would just use jackson library 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 Commented May 18, 2018 at 7:41
  • My sympathies @PacificNW_Lover. It's like they don't want their service to easily used. Commented Mar 2, 2021 at 13:58

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I already know about these tools. I was asking if there was a way to marshal using the graphql-java libs.
@PacificNW_Lover I am also having the same issue so I decided to build my own library to help parse graphQL response and query, you can take a look at it here github.com/daviestobialex/bpd-grapgql-parser, it is not finished but it is a work in progress
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.