Skip to content

Commit c5acccf

Browse files
committed
Add DgsGraphQlClient
Closes gh-846
1 parent b727854 commit c5acccf

File tree

5 files changed

+323
-38
lines changed

5 files changed

+323
-38
lines changed

Diff for: platform/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
api("jakarta.persistence:jakarta.persistence-api:3.1.0")
3434

3535
api("com.apollographql.federation:federation-graphql-java-support:4.4.0")
36+
api("com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:6.1.4")
3637

3738
api("com.google.code.findbugs:jsr305:3.0.2")
3839

Diff for: spring-graphql-docs/modules/ROOT/pages/client.adoc

+51
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ with options applicable to all transports.
2525

2626
Once `GraphQlClient` is built you can begin to make xref:client.adoc#client.requests[requests].
2727

28+
Typically, the GraphQL operation for a request is provided as text. Alternatively, you
29+
can use https://github.com/Netflix/dgs-codegen[DGS Codegen] client API classes through
30+
xref:client.adoc#client.dgsgraphqlclient[DgsGraphQlClient], which can wrap any of the
31+
above `GraphQlClient` extensions.
32+
33+
2834

2935
[[client.httpsyncgraphqlclient]]
3036
=== HTTP Sync
@@ -247,6 +253,7 @@ builders of all extensions. Currently, it has lets you configure:
247253

248254

249255

256+
250257
[[client.requests]]
251258
== Requests
252259

@@ -591,3 +598,47 @@ Once the interceptor is created, register it through the client builder. For exa
591598
.build();
592599
----
593600

601+
602+
603+
604+
[[client.dgsgraphqlclient]]
605+
== DGS Codegen
606+
607+
As an alternative to providing the operation such as a mutation, query, or subscription as
608+
text, you can use the https://github.com/Netflix/dgs-codegen[DGS Codegen] library to
609+
generate client API classes that let you use a fluent API to define the request.
610+
611+
Spring for GraphQL provides xref:client.adoc#client.dgsgraphqlclient[DgsGraphQlClient]
612+
that wraps any `GraphQlClient` and helps to prepare the request with generated client
613+
API classes.
614+
615+
For example, given the following schema:
616+
617+
[source,graphql,indent=0,subs="verbatim,quotes"]
618+
----
619+
type Query {
620+
books: [Book]
621+
}
622+
623+
type Book {
624+
id: ID
625+
name: String
626+
}
627+
----
628+
629+
You can perform a request as follows:
630+
631+
[source,java,indent=0,subs="verbatim,quotes"]
632+
----
633+
HttpGraphQlClient client = ... ;
634+
DgsGraphQlClient dgsClient = DgsGraphQlClient.create(client); // <1>
635+
636+
List<Book> books = dgsClient.request(new BooksGraphQLQuery()) // <2>
637+
.projection(new BooksProjectionRoot<>().id().name()) // <3>
638+
.retrieveSync()
639+
.toEntityList(Book.class);
640+
----
641+
642+
<1> - Create `DgsGraphQlClient` by wrapping any `GraphQlClient`.
643+
<2> - Specify the operation for the request.
644+
<3> - Define the selection set.

Diff for: spring-graphql-docs/modules/ROOT/pages/codegen.adoc

+6-37
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,21 @@
22
= Code Generation
33

44
You can use tools such as
5-
https://netflix.github.io/dgs/generating-code-from-schema/[DGS Code Generation] to generate
5+
https://netflix.github.io/dgs/generating-code-from-schema/[DGS Codegen] to generate
66
Java types from the GraphQL schema. The following can be generated:
77

8-
1. Client types for requests (e.g. queries, mutations) input types, and response selection types.
8+
1. Client types for requests (e.g. query, mutation) input types, and response selection types.
99
2. Data types corresponding to GraphQL schema types.
1010

1111
Code generation may not be ideal for your own application's data types especially if you
1212
want to add logic to them. Code generation, however, is a good fit for client types since
1313
those define the request, and don't need to have other logic. As a client, you may also
1414
choose to generate the data types for the response.
1515

16-
Client generated types can be used with Spring's `GraphQlClient`. Start by following the
16+
Client generated types can be used with Spring's
17+
xref:client.adoc#client.dgsgraphqlclient[DgsGraphQlClient]. Start by following the
1718
instructions for the DGS code generation plugin to generate client API types. Then, given
1819
a schema like this:
1920

20-
[source,graphql,indent=0,subs="verbatim,quotes"]
21-
----
22-
type Query {
23-
books: [Book]
24-
}
25-
26-
type Book {
27-
id: ID
28-
name: String
29-
}
30-
----
31-
32-
DGS Codegen generates `BooksGraphQLQuery` and `BooksProjectionRoot` that you can use with
33-
`GraphQlClient` over HTTP (or any supported transport) as follows:
34-
35-
[source,java,indent=0,subs="verbatim,quotes"]
36-
----
37-
HttpGraphQlClient client =
38-
HttpGraphQlClient.create(WebClient.create("http://localhost:8080/graphql"));
39-
40-
BooksGraphQLQuery query = new BooksGraphQLQuery();
41-
String document = new GraphQLQueryRequest(query, new BooksProjectionRoot<>().id().name()).serialize();
42-
43-
List<Book> books = client.document(document)
44-
.retrieve(query.getOperationName())
45-
.toEntityList(Book.class) // possibly also generated or imported if available
46-
.block();
47-
----
48-
49-
TIP: We intend to further simplify the above code in
50-
https://github.com/spring-projects/spring-graphql/issues/846[spring-graphql#846].
51-
52-
You can use Spring Initializer at https://start.spring.io to create a Spring project with
53-
the DGS Code Generation Gradle or Maven plugin.
21+
TIP: Spring Initializer at https://start.spring.io can create a Spring project with
22+
the DGS Codegen Gradle or Maven plugin.

Diff for: spring-graphql/build.gradle

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ dependencies {
3232

3333
compileOnly 'com.fasterxml.jackson.core:jackson-databind'
3434

35-
compileOnly 'com.apollographql.federation:federation-graphql-java-support'
35+
compileOnly('com.apollographql.federation:federation-graphql-java-support')
36+
compileOnly('com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core') {
37+
exclude group: "com.apollographql.federation", module: "federation-graphql-java-support"
38+
exclude group: "com.graphql-java", module: "graphql-java"
39+
}
3640

3741
testImplementation 'org.junit.jupiter:junit-jupiter'
3842
testImplementation 'org.assertj:assertj-core'

0 commit comments

Comments
 (0)