What’s Changed
Highlights
- Upgrade to
graphql-java:17.3
, which has many improvements. - A WebClient based GraphQL client implementation.
- Refactored existing GraphQL client implementation for better ergonomics.
Full list of changes
- Add ParameterNamesModule and Jdk8Module to the GraphQLResponse object mapper. (#670) @srinivasankavitha
- Added createWithWebClient factory method that takes header consumer (#669) @paulbakker
- refactored dgs client, and new Webclient based client. (#658) @paulbakker
- Update contributing doc (#662) @hantsy
- Upgrade to graphql-java 17.0 (#518) @berngp
- Adopt kotlinter-gradle instead of org.jlleitschuh.gradle:ktlint-gradle (#393) @berngp
Migrating to the new GraphQLClient
In this release we deprecated the DefaultGraphQLClient
class and the existing executeQuery
methods on the GraphQLClient
and MonoGraphQLClient
interfaces.
The deprecated methods required a RequestExecutor
to be passed in that takes care of the actual HTTP request. Requiring a RequestExecutor
makes it possible to plug-in any HTTP client, but requires quite a bit of plumbing.
The deprecated class/methods are replaced by the following:
- A brand new
WebClientGraphQLClient
. If you don't have a strong opinion about what HTTP client to use, this is the easiest way to use the client. The user just provides a WebClient instance, and the framework takes care of the rest. This should be the preferred method for most users! - If you do have a strong opinion about what HTTP client to use, you can continue to use the
RequestExecutor
approach using the newCustomGraphQLClient
orCustomMonoGraphQLClient
classes. TheRequestExecutor
is now defined as a constructor argument instead of at eachexecute*
method for better ergonomics. You can use your existingRequestExecutor
implementation however!
Below are two examples of how to migrate an existing usage of DefaultGraphQLClient
to either of the new approaches.
A DefaultGraphQLClient
that we'll migrate.
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/graphql";
DefaultGraphQLClient client = new DefaultGraphQLClient(url);
RequestExecutor requestExecutor = (url, headers, body) -> {
HttpHeaders httpHeaders = new HttpHeaders();
headers.forEach(httpHeaders::addAll);
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(body, httpHeaders),String.class);
return new HttpResponse(exchange.getStatusCodeValue(), exchange.getBody());
};
client.executeQuery(query, variables, requestExecutor);
Using WebClient
WebClient webClient = WebClient.create("http://localhost:8080/graphql");
WebClientGraphQLClient client = MonoGraphQLClient.createWithWebClient(webClient);
client.reactiveExecuteQuery(query);
Optionally, you can provide a Consumer<HttpHeader>
to the WebClientGraphQLClient
to provide additional headers.
WebClient webClient = WebClient.create("http://localhost:8080/graphql");
WebClientGraphQLClient client = MonoGraphQLClient.createWithWebClient(WebClient.create("http://localhost:$port/graphql" , headers ->
headers.add("myheader", "test")
);
client.reactiveExecuteQuery(query);
Using CustomGraphQLClient
RequestExecutor requestExecutor = (url, headers, body) -> {
HttpHeaders httpHeaders = new HttpHeaders();
headers.forEach(httpHeaders::addAll);
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(body, httpHeaders),String.class);
return new HttpResponse(exchange.getStatusCodeValue(), exchange.getBody());
};
CustomGraphQLClient client = GraphQLClient.createCustom(url, requestExecutor);
client.executeQuery(query); //the RequestExecutor was already passed in to the constructor
Updated docs for the new client.