Skip to content

Commit 77e1044

Browse files
committed
Add new docs version
1 parent 4694d99 commit 77e1044

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
id: version-4.0.0-alpha.4-graphql-java-comparison
3+
title: GraphQL Java Comparison
4+
original_id: graphql-java-comparison
5+
---
6+
7+
[graphql-java](https://graphql-java.com/) is one of the most popular JVM based GraphQL implemenations. GraphQL Kotlin is
8+
built on top of `grahpql-java` as it can be easily extended with additional functionality and this implementation
9+
has been used and tested by many users.
10+
11+
### GraphQL Java Schema
12+
13+
The most common way to create the schema in `graphql-java` is to first manually write the SDL file:
14+
15+
```graphql
16+
schema {
17+
query: Query
18+
}
19+
20+
type Query {
21+
bookById(id: ID): Book
22+
}
23+
24+
type Book {
25+
id: ID!
26+
name: String!
27+
pageCount: Int!
28+
author: Author
29+
}
30+
31+
type Author {
32+
id: ID!
33+
firstName: String!
34+
lastName: String!
35+
}
36+
```
37+
38+
Then write the runtime code that matches this schema to build the `GraphQLSchema` object.
39+
40+
```kotlin
41+
// Internal DB class, not schema class
42+
class Book(
43+
val id: ID,
44+
val name: String,
45+
val totalPages: Int, // This needs to be renamed to pageCount
46+
val authorId: ID // This is not in the schema
47+
)
48+
49+
// Internal DB class, not schema class
50+
class Author(
51+
val id: ID,
52+
val firstName: String,
53+
val lastName: String
54+
)
55+
56+
class GraphQLDataFetchers {
57+
private val books: List<Book> = booksFromDB()
58+
private val authors: List<Author> = authorsFromDB()
59+
60+
fun getBookByIdDataFetcher() = DataFetcher { dataFetchingEnvironment ->
61+
val bookId: String = dataFetchingEnvironment.getArgument("id")
62+
return books.firstOrNull { it.id == bookId }
63+
}
64+
65+
fun getAuthorDataFetcher() = DataFetcher { dataFetchingEnvironment ->
66+
val book: Book = dataFetchingEnvironment.getSource() as Book
67+
return authors.firstOrNull { it.id == book.authorId }
68+
}
69+
70+
fun getPageCountDataFetcher() = DataFetcher { dataFetchingEnvironment ->
71+
val book: Book = dataFetchingEnvironment.getSource() as Book
72+
return book.totalPages
73+
}
74+
}
75+
76+
val schemaParser = SchemaParser()
77+
val schemaGenerator = SchemaGenerator()
78+
val schemaFile = loadSchema("schema.graphqls")
79+
val typeRegistry = schemaParser.parse(schemaFile)
80+
val graphQLDataFetchers = GraphQLDataFetchers()
81+
82+
val runtimeWiring = RuntimeWiring.newRuntimeWiring()
83+
.type(
84+
newTypeWiring("Query")
85+
.dataFetcher("bookById", graphQLDataFetchers.getBookByIdDataFetcher())
86+
)
87+
.type(
88+
newTypeWiring("Book")
89+
.dataFetcher("author", graphQLDataFetchers.getAuthorDataFetcher())
90+
.dataFetcher("pageCount", graphQLDataFetchers.getPageCountDataFetcher())
91+
)
92+
.build()
93+
94+
// Combine the types and runtime code together to make a schema
95+
val graphQLSchema: GraphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring)
96+
```
97+
98+
This means that there are two sources of truth for your schema and changes in either have to be reflected in both locations.
99+
As your schema scales to hundreds of types and many different resolvers, it can get more difficult to track what code needs to be changed if you want to add a new field,
100+
deprecate or delete an existing one, or fix a bug in the resolver code.
101+
102+
These errors will hopefully be caught by your build or automated tests, but it is another layer your have to be worried about when creating your API.
103+
104+
### GraphQL Kotlin Schema
105+
106+
`graphql-kotlin-schema-generator` aims to simplify this process by using Kotlin reflection to generate the schema for you.
107+
All you need to do is write your schema code in a Kotlin class with public functions or properties.
108+
109+
```kotlin
110+
private val books: List<Book> = booksFromDB()
111+
private val authors: List<Author> = authorsFromDB()
112+
113+
class Query {
114+
fun bookById(id: ID): Book? = books.find { it.id == id }
115+
}
116+
117+
class Book(
118+
val id: ID,
119+
val name: String,
120+
private val totalPages: Int,
121+
private val authorId: ID
122+
) {
123+
fun author(): Author? = authors.find { it.id == authorId }
124+
fun pageCount(): Int = totalPages
125+
}
126+
127+
class Author(
128+
val id: ID,
129+
val firstName: String,
130+
val lastName: String
131+
)
132+
133+
val config = SchemaGeneratorConfig(supportedPackages = "com.example")
134+
val queries = listOf(TopLevelObject(Query()))
135+
val schema: GraphQLSchema = toSchema(config, queries)
136+
```
137+
138+
This makes changes in code directly reflect to your schema and you can still produce the `GraphQLSchema` to print and export an SDL file.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"version-4.0.0-alpha.4-docs": {
3+
"Introduction": [
4+
"version-4.0.0-alpha.4-getting-started",
5+
"version-4.0.0-alpha.4-examples",
6+
"version-4.0.0-alpha.4-graphql-java-comparison",
7+
"version-4.0.0-alpha.4-blogs-and-videos"
8+
],
9+
"Schema Generator": [
10+
"version-4.0.0-alpha.4-schema-generator/schema-generator-getting-started",
11+
{
12+
"type": "subcategory",
13+
"label": "Writing schemas with Kotlin",
14+
"ids": [
15+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/nullability",
16+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/arguments",
17+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/scalars",
18+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/enums",
19+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/lists",
20+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/interfaces",
21+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/unions",
22+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/annotations",
23+
"version-4.0.0-alpha.4-schema-generator/writing-schemas/nested-arguments"
24+
]
25+
},
26+
{
27+
"type": "subcategory",
28+
"label": "Customizing Schema",
29+
"ids": [
30+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/generator-config",
31+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/documenting-fields",
32+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/excluding-fields",
33+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/renaming-fields",
34+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/directives",
35+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/deprecating-schema",
36+
"version-4.0.0-alpha.4-schema-generator/customizing-schemas/advanced-features"
37+
]
38+
},
39+
{
40+
"type": "subcategory",
41+
"label": "Execution",
42+
"ids": [
43+
"version-4.0.0-alpha.4-schema-generator/execution/fetching-data",
44+
"version-4.0.0-alpha.4-schema-generator/execution/async-models",
45+
"version-4.0.0-alpha.4-schema-generator/execution/exceptions",
46+
"version-4.0.0-alpha.4-schema-generator/execution/data-fetching-environment",
47+
"version-4.0.0-alpha.4-schema-generator/execution/contextual-data",
48+
"version-4.0.0-alpha.4-schema-generator/execution/optional-undefined-arguments",
49+
"version-4.0.0-alpha.4-schema-generator/execution/subscriptions",
50+
"version-4.0.0-alpha.4-schema-generator/execution/introspection"
51+
]
52+
}
53+
],
54+
"Federation": [
55+
"version-4.0.0-alpha.4-federated/apollo-federation",
56+
"version-4.0.0-alpha.4-federated/federated-schemas",
57+
"version-4.0.0-alpha.4-federated/federated-directives",
58+
"version-4.0.0-alpha.4-federated/type-resolution"
59+
],
60+
"Spring Server": [
61+
"version-4.0.0-alpha.4-spring-server/spring-overview",
62+
"version-4.0.0-alpha.4-spring-server/spring-beans",
63+
"version-4.0.0-alpha.4-spring-server/spring-properties",
64+
"version-4.0.0-alpha.4-spring-server/spring-schema",
65+
"version-4.0.0-alpha.4-spring-server/spring-graphql-context",
66+
"version-4.0.0-alpha.4-spring-server/http-request-response",
67+
"version-4.0.0-alpha.4-spring-server/subscriptions"
68+
],
69+
"HTTP Client": [
70+
"version-4.0.0-alpha.4-client/client-overview",
71+
"version-4.0.0-alpha.4-client/client-features",
72+
"version-4.0.0-alpha.4-client/client-customization"
73+
],
74+
"Build Plugins": [
75+
"version-4.0.0-alpha.4-plugins/gradle-plugin",
76+
"version-4.0.0-alpha.4-plugins/maven-plugin"
77+
],
78+
"Contributors": [
79+
"version-4.0.0-alpha.4-contributors/release-proc"
80+
]
81+
}
82+
}

website/versions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
2+
"4.0.0-alpha.4",
23
"4.0.0-alpha.3",
34
"4.0.0-alpha.2",
45
"3.6.1"

0 commit comments

Comments
 (0)