diff --git a/commercetools/internal-docs/src/main/java/com/commercetools/docs/meta/GraphQL.java b/commercetools/internal-docs/src/main/java/com/commercetools/docs/meta/GraphQL.java index 42cc0dcee0a..28733bbaebc 100644 --- a/commercetools/internal-docs/src/main/java/com/commercetools/docs/meta/GraphQL.java +++ b/commercetools/internal-docs/src/main/java/com/commercetools/docs/meta/GraphQL.java @@ -1,6 +1,9 @@ package com.commercetools.docs.meta; +import com.commercetools.api.client.ByProjectKeyGraphqlRequestBuilder; +import com.commercetools.api.client.ProjectRequestBuilder; + /** * {@include.toc} * @@ -31,7 +34,15 @@ * instance too and can access it's methods by casting it.

* * {@include.example example.GraphQLModuleTest#graphQLJacksonModule} - + * + *

For retrieving data it's best to use the {@link ByProjectKeyGraphqlRequestBuilder#query(ProjectRequestBuilder) 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.

+ * + * {@include.example example.GraphQLModuleTest#graphQLExecute} + * + *

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

+ * + * {@include.example example.GraphQLModuleTest#graphQLCustom} + * *

Additionally for retrieving the result data it can be explicitly mapped to the {@link com.commercetools.graphql.api.GraphQLDataResponse} class which * uses the schema generated models.

* diff --git a/commercetools/internal-docs/src/test/java/example/GraphQLModuleTest.java b/commercetools/internal-docs/src/test/java/example/GraphQLModuleTest.java index 923d40223b4..252f7d74a94 100644 --- a/commercetools/internal-docs/src/test/java/example/GraphQLModuleTest.java +++ b/commercetools/internal-docs/src/test/java/example/GraphQLModuleTest.java @@ -81,4 +81,38 @@ public void graphQLJacksonModule() { GraphQLData data = (GraphQLData) response.getBody().getData(); final ProductQueryResult products = data.get(productQuery); } + + public void graphQLExecute() { + final GraphQLRequest productQuery = GraphQL + .products(query -> query.localeProjection(Collections.singletonList("en"))) + .projection(root -> root.results().id().key().productType().key().getParent().createdAt()); + + final ApiHttpResponse> response = projectApiRoot + .graphql() + .query(productQuery) + .executeBlocking(); + + Assertions.assertThat(response.getBody()).isNotNull(); + + final ProductQueryResult data = response.getBody().getData(); + Assertions.assertThat(data.getResults()).isNotNull(); + } + + public void graphQLCustom() { + final GraphQLRequest 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> response = projectApiRoot + .graphql() + .query(productQuery) + .executeBlocking(); + + Assertions.assertThat(response.getBody()).isNotNull(); + + final ProductQueryResult data = response.getBody().getData(); + Assertions.assertThat(data.getResults()).isNotNull(); + } }