Class GraphQL

java.lang.Object
com.commercetools.docs.meta.GraphQL

public class GraphQL extends Object
Table of content

GraphQL endpoint

The SDK provides support for the usage of the GraphQL endpoint.

ProjectApiRoot apiRoot = createProjectClient();

GraphQLRequest request = GraphQLRequest.builder()
        .query("query($productFilter:String) { products(where: $productFilter) { results { id } } }")
        .variables(builder -> builder.addValue("productFilter", "id = \"" + product.getId() + "\""))
        .build();
final ApiHttpResponse<GraphQLResponse> response = apiRoot.graphql().post(request).executeBlocking();

Assertions.assertThat(response.getBody()).isInstanceOf(GraphQLResponse.class);
Assertions.assertThat(response.getBody().getData()).isInstanceOf(Map.class);

See the test code.

In case a result model class exists the class reference can be given to the execute method so that the response will be mapped accordingly.

ProjectApiRoot apiRoot = createProjectClient();

GraphQLRequest request = GraphQLRequest.builder()
        .query("query($productFilter:String) { products(where: $productFilter) { results { id } } }")
        .variables(builder -> builder.addValue("productFilter", "id = \"" + product.getId() + "\""))
        .build();
final ApiHttpResponse<JsonNode> response = apiRoot.graphql().post(request).executeBlocking(JsonNode.class);

Assertions.assertThat(response.getBody()).isInstanceOf(JsonNode.class);

See the test code.

GraphQL module

The SDK comes with a module for enhanced GraphQL support. With the help of the DGS codegen we generate a type safe query and projection builder. The results will be mapped to the correct response type.

The response types will have all available fields, but only the projected will be filled with a value.

The GraphQL class has factory methods for all available operations in the GraphQL schema

final GraphQLRequest<ProductQueryResult> productsQuery = GraphQL
        .products(query -> query.localeProjection(Collections.singletonList("en")))
        .projection(root -> root.results().id().key().productType().key().getParent().createdAt().key());

String query = "{\n"
        + "  products(localeProjection: [\"en\"]) {\n"
        + "    results {\n"
        + "      id\n"
        + "      key\n"
        + "      productType {\n"
        + "        key\n"
        + "      }\n"
        + "      createdAt\n"
        + "    }\n"
        + "  }\n"
        + "}";
Assertions.assertThat(productsQuery.getQuery()).isEqualTo(query);

See the test code.

The module provides a Jackson module. It's configured as SPI and gets automatically registered to the SDKs ObjectMapper. This module will configure the ObjectMapper to deserialize the response data object as GraphQLData instance too and can access it's methods by casting it.

final GraphQLRequest<ProductQueryResult> productQuery = GraphQL
        .products(query -> query.localeProjection(Collections.singletonList("en")))
        .projection(root -> root.results().id().key().productType().key().getParent().createdAt().key());

final ApiHttpResponse<GraphQLResponse> response = projectApiRoot.graphql().post(productQuery).executeBlocking();

Assertions.assertThat(response.getBody().getData()).isInstanceOf(GraphQLData.class);

GraphQLData data = (GraphQLData) response.getBody().getData();
final ProductQueryResult products = data.get(productQuery);

See the test code.

For retrieving data it's best to use the query method. It's mapping the result to the response data class with information about the executed operation, so the result data can be accessed directly.

final GraphQLRequest<ProductQueryResult> productQuery = GraphQL
        .products(query -> query.localeProjection(Collections.singletonList("en")))
        .projection(root -> root.results().id().key().productType().key().getParent().createdAt());

final ApiHttpResponse<com.commercetools.graphql.api.GraphQLResponse<ProductQueryResult>> response = projectApiRoot
        .graphql()
        .query(productQuery)
        .executeBlocking();

Assertions.assertThat(response.getBody()).isNotNull();

final ProductQueryResult data = response.getBody().getData();
Assertions.assertThat(data.getResults()).isNotNull();

See the test code.

It's also possible to execute a custom query from a string

final GraphQLRequest<ProductQueryResult> productQuery = GraphQL.query(
    "query($localeProjection:[Locale!]) { products(localeProjection: $localeProjection) { results { id } } }")
        .variables(b -> b.addValue("$localeProjection", Collections.singletonList("en")))
        .dataMapper(GraphQLData::getProducts)
        .build();

final ApiHttpResponse<com.commercetools.graphql.api.GraphQLResponse<ProductQueryResult>> response = projectApiRoot
        .graphql()
        .query(productQuery)
        .executeBlocking();

Assertions.assertThat(response.getBody()).isNotNull();

final ProductQueryResult data = response.getBody().getData();
Assertions.assertThat(data.getResults()).isNotNull();

See the test code.

Additionally for retrieving the result data it can be explicitly mapped to the GraphQLDataResponse class which uses the schema generated models.

final GraphQLRequest<ProductQueryResult> productQuery = GraphQL
        .products(query -> query.localeProjection(Collections.singletonList("en")))
        .projection(root -> root.results().id().key().productType().key().getParent().createdAt().key());

final ApiHttpResponse<GraphQLDataResponse> response = projectApiRoot.graphql()
        .post(productQuery)
        .executeBlocking(GraphQLDataResponse.class);

See the test code.

The GraphQLDataResponse provides a method to retrieve the operations result data

final GraphQLRequest<ProductQueryResult> productQuery = GraphQL
        .products(query -> query.localeProjection(Collections.singletonList("en")))
        .projection(root -> root.results().id().key().productType().key().getParent().createdAt().key());

final ApiHttpResponse<GraphQLDataResponse> response = projectApiRoot.graphql()
        .post(productQuery)
        .executeBlocking(GraphQLDataResponse.class);

final ProductQueryResult data = response.getBody().getData(productQuery);

See the test code.

  • Constructor Details

    • GraphQL

      public GraphQL()