Add DgsGraphQlClient

Closes gh-846
This commit is contained in:
rstoyanchev
2024-02-19 19:41:11 +00:00
parent b727854fa5
commit c5acccf770
5 changed files with 323 additions and 38 deletions

View File

@@ -25,6 +25,12 @@ with options applicable to all transports.
Once `GraphQlClient` is built you can begin to make xref:client.adoc#client.requests[requests].
Typically, the GraphQL operation for a request is provided as text. Alternatively, you
can use https://github.com/Netflix/dgs-codegen[DGS Codegen] client API classes through
xref:client.adoc#client.dgsgraphqlclient[DgsGraphQlClient], which can wrap any of the
above `GraphQlClient` extensions.
[[client.httpsyncgraphqlclient]]
=== HTTP Sync
@@ -247,6 +253,7 @@ builders of all extensions. Currently, it has lets you configure:
[[client.requests]]
== Requests
@@ -591,3 +598,47 @@ Once the interceptor is created, register it through the client builder. For exa
.build();
----
[[client.dgsgraphqlclient]]
== DGS Codegen
As an alternative to providing the operation such as a mutation, query, or subscription as
text, you can use the https://github.com/Netflix/dgs-codegen[DGS Codegen] library to
generate client API classes that let you use a fluent API to define the request.
Spring for GraphQL provides xref:client.adoc#client.dgsgraphqlclient[DgsGraphQlClient]
that wraps any `GraphQlClient` and helps to prepare the request with generated client
API classes.
For example, given the following schema:
[source,graphql,indent=0,subs="verbatim,quotes"]
----
type Query {
books: [Book]
}
type Book {
id: ID
name: String
}
----
You can perform a request as follows:
[source,java,indent=0,subs="verbatim,quotes"]
----
HttpGraphQlClient client = ... ;
DgsGraphQlClient dgsClient = DgsGraphQlClient.create(client); // <1>
List<Book> books = dgsClient.request(new BooksGraphQLQuery()) // <2>
.projection(new BooksProjectionRoot<>().id().name()) // <3>
.retrieveSync()
.toEntityList(Book.class);
----
<1> - Create `DgsGraphQlClient` by wrapping any `GraphQlClient`.
<2> - Specify the operation for the request.
<3> - Define the selection set.

View File

@@ -2,10 +2,10 @@
= Code Generation
You can use tools such as
https://netflix.github.io/dgs/generating-code-from-schema/[DGS Code Generation] to generate
https://netflix.github.io/dgs/generating-code-from-schema/[DGS Codegen] to generate
Java types from the GraphQL schema. The following can be generated:
1. Client types for requests (e.g. queries, mutations) input types, and response selection types.
1. Client types for requests (e.g. query, mutation) input types, and response selection types.
2. Data types corresponding to GraphQL schema types.
Code generation may not be ideal for your own application's data types especially if you
@@ -13,41 +13,10 @@ want to add logic to them. Code generation, however, is a good fit for client ty
those define the request, and don't need to have other logic. As a client, you may also
choose to generate the data types for the response.
Client generated types can be used with Spring's `GraphQlClient`. Start by following the
Client generated types can be used with Spring's
xref:client.adoc#client.dgsgraphqlclient[DgsGraphQlClient]. Start by following the
instructions for the DGS code generation plugin to generate client API types. Then, given
a schema like this:
[source,graphql,indent=0,subs="verbatim,quotes"]
----
type Query {
books: [Book]
}
type Book {
id: ID
name: String
}
----
DGS Codegen generates `BooksGraphQLQuery` and `BooksProjectionRoot` that you can use with
`GraphQlClient` over HTTP (or any supported transport) as follows:
[source,java,indent=0,subs="verbatim,quotes"]
----
HttpGraphQlClient client =
HttpGraphQlClient.create(WebClient.create("http://localhost:8080/graphql"));
BooksGraphQLQuery query = new BooksGraphQLQuery();
String document = new GraphQLQueryRequest(query, new BooksProjectionRoot<>().id().name()).serialize();
List<Book> books = client.document(document)
.retrieve(query.getOperationName())
.toEntityList(Book.class) // possibly also generated or imported if available
.block();
----
TIP: We intend to further simplify the above code in
https://github.com/spring-projects/spring-graphql/issues/846[spring-graphql#846].
You can use Spring Initializer at https://start.spring.io to create a Spring project with
the DGS Code Generation Gradle or Maven plugin.
TIP: Spring Initializer at https://start.spring.io can create a Spring project with
the DGS Codegen Gradle or Maven plugin.