Skip to content

Commit 6cd3e42

Browse files
committed
Add reference docs section on codegen
Closes gh-848
1 parent 0f8d20f commit 6cd3e42

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Diff for: spring-graphql-docs/src/docs/asciidoc/codegen.adoc

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
include::attributes.adoc[]
2+
3+
4+
5+
[[codegen]]
6+
= Code Generation
7+
8+
You can use tools such as
9+
https://netflix.github.io/dgs/generating-code-from-schema/[DGS Code Generation] to generate
10+
Java types from the GraphQL schema. The following can be generated:
11+
12+
1. Client types for requests (e.g. queries, mutations) input types, and other types to
13+
express the response selection set.
14+
2. Data types.
15+
3. Server handling classes (e.g. controllers).
16+
17+
Code generation provides convenience initially, but is not ideal for your own application
18+
domain types over which you'll typically want control. For client types, however, code
19+
generation can be very useful since you typically don't need to manually change generated
20+
request types, input types, and selection set types. Response types could be imported,
21+
if you have access to them, or otherwise could also be generated.
22+
23+
Client generated types can be used with Spring's `GraphQlClient`. Start by following the
24+
instructions for the DGS code generation plugin to generate client API types. Then, given
25+
a schema like this:
26+
27+
[source,graphql,indent=0,subs="verbatim,quotes"]
28+
----
29+
type Query {
30+
books: [Book]
31+
}
32+
33+
type Book {
34+
id: ID
35+
name: String
36+
}
37+
----
38+
39+
DGS Codegen will generate a `BooksGraphQLQuery` and `BooksProjectionRoot` classes.
40+
You can then use those with Spring's `GraphQlClient` along with your own `Book` class
41+
for the response:
42+
43+
[source,java,indent=0,subs="verbatim,quotes"]
44+
----
45+
HttpGraphQlClient client =
46+
HttpGraphQlClient.create(WebClient.create("http://localhost:8080/graphql"));
47+
48+
BooksGraphQLQuery query = new BooksGraphQLQuery();
49+
String document = new GraphQLQueryRequest(query, new BooksProjectionRoot<>().id().name()).serialize();
50+
51+
List<Book> books = client.document(document)
52+
.retrieve(query.getOperationName())
53+
.toEntityList(Book.class)
54+
.block();
55+
----
56+
57+
NOTE: Spring Initializer at https://start.spring.io is scheduled to add support for the DGS
58+
Code Generation with Gradle and Maven. See
59+
https://github.com/spring-io/start.spring.io/pull/1348[start.spring.io#1348].

Diff for: spring-graphql-docs/src/docs/asciidoc/index.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,11 @@ include::client.adoc[leveloffset=+1]
17271727

17281728

17291729

1730+
include::codegen.adoc[leveloffset=+1]
1731+
1732+
1733+
1734+
17301735
include::testing.adoc[leveloffset=+1]
17311736

17321737

0 commit comments

Comments
 (0)