Skip to content

Commit

Permalink
add graphql query docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Nov 1, 2023
1 parent d28e94c commit 55c823b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

package com.commercetools.docs.meta;

import com.commercetools.api.client.ByProjectKeyGraphqlRequestBuilder;
import com.commercetools.api.client.ProjectRequestBuilder;

/**
* {@include.toc}
*
Expand Down Expand Up @@ -31,7 +34,15 @@
* instance too and can access it's methods by casting it.</p>
*
* {@include.example example.GraphQLModuleTest#graphQLJacksonModule}
*
* <p>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.</p>
*
* {@include.example example.GraphQLModuleTest#graphQLExecute}
*
* <p>It's also possible to execute a custom query from a string</p>
*
* {@include.example example.GraphQLModuleTest#graphQLCustom}
*
* <p>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.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,38 @@ public void graphQLJacksonModule() {
GraphQLData data = (GraphQLData) response.getBody().getData();
final ProductQueryResult products = data.get(productQuery);
}

public void graphQLExecute() {
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();
}

public void graphQLCustom() {
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();
}
}

0 comments on commit 55c823b

Please sign in to comment.