Skip to content

Commit 066c0ea

Browse files
dariuszkucsmyrick
authored andcommitted
[docs] spring-server documentation updates (#456)
1 parent 66368ae commit 066c0ea

File tree

6 files changed

+79
-10
lines changed

6 files changed

+79
-10
lines changed

docs/federated/federated-schemas.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ data class Review(val reviewId: String, val text: String)
9696

9797
// Generate the schema
9898
val productResolver = object: FederatedTypeResolver<Product> {
99-
override fun resolve(keys: Map<String, Any>): Product {
100-
val id = keys["id"]?.toString()?.toIntOrNull()
101-
// instantiate product using id
99+
override suspend fun resolve(representations: List<Map<String, Any>>): List<Product?> = representations.map { keys ->
100+
keys["id"]?.toString()?.toIntOrNull()?.let { id ->
101+
Product(id)
102+
}
102103
}
103104
}
104105
val federatedTypeRegistry = FederatedTypeRegistry(mapOf("Product" to productResolver))

docs/server/spring-auto-config.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ type Widget {
5858
value: String!
5959
}
6060
```
61+
62+
Newly created GraphQL server starts up with following preconfigured default routes:
63+
64+
* **/graphql** - GraphQL server endpoint used for processing queries and mutations
65+
* **/sdl** - convenience endpoint that returns current schema in Schema Definition Language format
66+
* **/playground** - Prisma Labs GraphQL Playground IDE endpoint

docs/server/spring-beans.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: spring-beans
33
title: Automatically Created Beans
44
---
55

6-
`graphql-kotlin-spring-server` automatically creates all required beans to start GraphQL web server. Many of the beans are conditionally created and default behavior
6+
`graphql-kotlin-spring-server` automatically creates all the necessary beans to start GraphQL web server. Many of the beans are conditionally created and default behavior
77
can be customized by providing custom beans in your application context. See sections below for the information about all automatically created beans.
88

99
## Schema Generation

docs/server/spring-graphql-context.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: spring-graphql-context
3+
title: Generating GraphQL Context
4+
---
5+
6+
`graphql-kotlin-spring-server` provides a simple mechanism to build [GraphQL context](../execution/contextual-data) per query execution through
7+
[GraphQLContextFactory](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/execution/GraphQLContextFactory.kt).
8+
Once context factory bean is available in the Spring application context it will then be used in a corresponding
9+
[ContextWebFilter](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/execution/ContextWebFilter.kt)
10+
to populate GraphQL context based on the incoming request and make it available during query execution.
11+
12+
For example if we define our custom context as follows:
13+
14+
```kotlin
15+
data class MyGraphQLContext(val myCustomValue: String)
16+
```
17+
18+
We can generate corresponding `GraphQLContextFactory` bean:
19+
20+
```kotlin
21+
@Component
22+
class MyGraphQLContextFactory: GraphQLContextFactory<MyGraphQLContext> {
23+
override suspend fun generateContext(
24+
request: ServerHttpRequest,
25+
response: ServerHttpResponse
26+
): MyGraphQLContext = MyGraphQLContext(
27+
myCustomValue = request.headers.getFirst("MyHeader") ?: "defaultContext"
28+
)
29+
}
30+
```
31+
32+
Once your application is configured to build your custom `MyGraphQLContext`, we can then specify it as function argument by annotating it with `@GraphQLContext`.
33+
While executing the query, the corresponding GraphQL context will be read from the environment and automatically injected to the function input arguments.
34+
35+
```kotlin
36+
@Component
37+
class ContextualQuery: Query {
38+
fun contextualQuery(
39+
value: Int,
40+
@GraphQLContext context: MyGraphQLContext
41+
): ContextualResponse = ContextualResponse(value, context.myCustomValue)
42+
}
43+
```
44+
45+
The above query would produce the following GraphQL schema:
46+
47+
```graphql
48+
schema {
49+
query: Query
50+
}
51+
52+
type Query {
53+
contextualQuery(
54+
value: Int!
55+
): ContextualResponse!
56+
}
57+
```
58+
59+
Notice that the `@GraphQLContext` annotated argument is not reflected in the generated GraphQL schema.

docs/server/spring-properties.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ title: Configuration Properties
55

66
`graphql-kotlin-spring-server` relies on [GraphQLConfigurationProperties](https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/GraphQLConfigurationProperties.kt)
77
to provide various customizations of the auto-configuration library. All applicable configuration properties expose [configuration
8-
metadata](https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html) that provides
8+
metadata](https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html) that provide
99
details on the supported configuration properties.
1010

1111
| Property | Description | Default Value |
1212
|----------|-------------|---------------|
13-
| graphql.endpoint | GraphQL server endpoint | "graphql" |
13+
| graphql.endpoint | GraphQL server endpoint | graphql |
1414
| graphql.packages | List of supported packages that can contain GraphQL schema type definitions | |
1515
| graphql.federation.enabled | Boolean flag indicating whether to generate federated GraphQL model | false |
16-
| graphql.subscriptions.endpoint | GraphQL subscriptions endpoint | "subscriptions" |
17-
| graphql.subscriptions.keepAliveInterval | Keep the websocket alive and send a message to the client every interval in ms. Defaults to not sending messages | null |
1816
| graphql.playground.enabled | Boolean flag indicating whether to enabled Prisma Labs Playground GraphQL IDE | true |
19-
| graphql.playground.endpoint | Prisma Labs Playground GraphQL IDE endpoint | "playground" |
17+
| graphql.playground.endpoint | Prisma Labs Playground GraphQL IDE endpoint | playground |
18+
| graphql.sdl.enabled | Boolean flag indicating whether to expose SDL endpoint | true |
19+
| graphql.sdl.endpoint | GraphQL SDL endpoint | sdl |
20+
| graphql.subscriptions.endpoint | GraphQL subscriptions endpoint | subscriptions |
21+
| graphql.subscriptions.keepAliveInterval | Keep the websocket alive and send a message to the client every interval in ms. Defaults to not sending messages | null |

website/sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"Server": [
5454
"server/spring-auto-config",
5555
"server/spring-beans",
56-
"server/spring-properties"
56+
"server/spring-properties",
57+
"server/spring-graphql-context"
5758
],
5859
"Contributors": [
5960
"contributors/release-proc"

0 commit comments

Comments
 (0)