/
DgsWebFluxAutoConfiguration.kt
186 lines (167 loc) · 8.23 KB
/
DgsWebFluxAutoConfiguration.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.graphql.dgs.webflux.autoconfiguration
import com.netflix.graphql.dgs.internal.CookieValueResolver
import com.netflix.graphql.dgs.internal.DefaultDgsQueryExecutor
import com.netflix.graphql.dgs.internal.DgsDataLoaderProvider
import com.netflix.graphql.dgs.internal.DgsSchemaProvider
import com.netflix.graphql.dgs.reactive.DgsReactiveCustomContextBuilderWithRequest
import com.netflix.graphql.dgs.reactive.DgsReactiveQueryExecutor
import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveGraphQLContextBuilder
import com.netflix.graphql.dgs.reactive.internal.DefaultDgsReactiveQueryExecutor
import com.netflix.graphql.dgs.reactive.internal.FluxDataFetcherResultProcessor
import com.netflix.graphql.dgs.reactive.internal.MonoDataFetcherResultProcessor
import com.netflix.graphql.dgs.webflux.handlers.DgsReactiveWebsocketHandler
import com.netflix.graphql.dgs.webflux.handlers.DgsWebfluxHttpHandler
import com.netflix.graphql.dgs.webflux.handlers.WebFluxCookieValueResolver
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.execution.*
import graphql.execution.instrumentation.ChainedInstrumentation
import graphql.introspection.IntrospectionQuery
import graphql.schema.GraphQLSchema
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.core.io.Resource
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.server.RequestPredicates.accept
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.RouterFunctions
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.ServerResponse.ok
import org.springframework.web.reactive.function.server.ServerResponse.permanentRedirect
import org.springframework.web.reactive.function.server.json
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter
import reactor.core.publisher.Mono
import java.net.URI
import java.util.*
@Suppress("SpringJavaInjectionPointsAutowiringInspection")
@Configuration
@EnableConfigurationProperties(DgsWebfluxConfigurationProperties::class)
open class DgsWebFluxAutoConfiguration(private val configProps: DgsWebfluxConfigurationProperties) {
@Bean
open fun dgsReactiveQueryExecutor(
applicationContext: ApplicationContext,
schema: GraphQLSchema,
schemaProvider: DgsSchemaProvider,
dgsDataLoaderProvider: DgsDataLoaderProvider,
dgsContextBuilder: DefaultDgsReactiveGraphQLContextBuilder,
dataFetcherExceptionHandler: DataFetcherExceptionHandler,
chainedInstrumentation: ChainedInstrumentation,
environment: Environment,
@Qualifier("query") providedQueryExecutionStrategy: Optional<ExecutionStrategy>,
@Qualifier("mutation") providedMutationExecutionStrategy: Optional<ExecutionStrategy>,
idProvider: Optional<ExecutionIdProvider>,
reloadSchemaIndicator: DefaultDgsQueryExecutor.ReloadSchemaIndicator
): DgsReactiveQueryExecutor {
val queryExecutionStrategy =
providedQueryExecutionStrategy.orElse(AsyncExecutionStrategy(dataFetcherExceptionHandler))
val mutationExecutionStrategy =
providedMutationExecutionStrategy.orElse(AsyncSerialExecutionStrategy(dataFetcherExceptionHandler))
return DefaultDgsReactiveQueryExecutor(
schema,
schemaProvider,
dgsDataLoaderProvider,
dgsContextBuilder,
chainedInstrumentation,
queryExecutionStrategy,
mutationExecutionStrategy,
idProvider,
reloadSchemaIndicator
)
}
@Bean
@ConditionalOnMissingBean
open fun reactiveGraphQlContextBuilder(
dgsReactiveCustomContextBuilderWithRequest: Optional<DgsReactiveCustomContextBuilderWithRequest<*>>
): DefaultDgsReactiveGraphQLContextBuilder {
return DefaultDgsReactiveGraphQLContextBuilder(dgsReactiveCustomContextBuilderWithRequest)
}
@Bean
@ConditionalOnProperty(name = ["dgs.graphql.graphiql.enabled"], havingValue = "true", matchIfMissing = true)
open fun graphiQlConfigurer(configProps: DgsWebfluxConfigurationProperties): GraphiQlConfigurer {
return GraphiQlConfigurer(configProps)
}
@Bean
@ConditionalOnProperty(name = ["dgs.graphql.graphiql.enabled"], havingValue = "true", matchIfMissing = true)
open fun graphiQlIndexRedirect(@Value("classpath:/static/graphiql/index.html") indexHtml: Resource): RouterFunction<ServerResponse> {
return RouterFunctions.route()
.GET(configProps.graphiql.path) {
permanentRedirect(URI.create(configProps.graphiql.path + "/index.html")).build()
}
.GET(configProps.graphiql.path + "/index.html") {
ok().bodyValue(indexHtml)
}
.build()
}
@Bean
open fun dgsGraphQlRouter(dgsQueryExecutor: DgsReactiveQueryExecutor): RouterFunction<ServerResponse> {
val graphQlHandler = DgsWebfluxHttpHandler(dgsQueryExecutor)
return RouterFunctions.route()
.POST(
configProps.path, accept(MediaType.APPLICATION_JSON, MediaType.valueOf("application/graphql")),
graphQlHandler::graphql
).build()
}
@Bean
@ConditionalOnProperty(name = ["dgs.graphql.schema-json.enabled"], havingValue = "true", matchIfMissing = true)
open fun schemaRouter(schemaProvider: DgsSchemaProvider): RouterFunction<ServerResponse> {
return RouterFunctions.route()
.GET(
configProps.schemaJson.path
) {
val graphQLSchema: GraphQLSchema = schemaProvider.schema()
val graphQL = GraphQL.newGraphQL(graphQLSchema).build()
val executionInput: ExecutionInput =
ExecutionInput.newExecutionInput().query(IntrospectionQuery.INTROSPECTION_QUERY)
.build()
val execute = graphQL.executeAsync(executionInput)
return@GET Mono.fromCompletionStage(execute)
.map { it.toSpecification() }
.flatMap { ok().json().bodyValue(it) }
}.build()
}
@Bean
open fun websocketSubscriptionHandler(dgsReactiveQueryExecutor: DgsReactiveQueryExecutor): SimpleUrlHandlerMapping {
val simpleUrlHandlerMapping = SimpleUrlHandlerMapping(mapOf("/subscriptions" to DgsReactiveWebsocketHandler(dgsReactiveQueryExecutor)))
simpleUrlHandlerMapping.order = 1
return simpleUrlHandlerMapping
}
@Bean
open fun handlerAdapter(): WebSocketHandlerAdapter? {
return WebSocketHandlerAdapter()
}
@Bean
open fun monoReactiveDataFetcherResultProcessor(): MonoDataFetcherResultProcessor {
return MonoDataFetcherResultProcessor()
}
@Bean
open fun fluxReactiveDataFetcherResultProcessor(): FluxDataFetcherResultProcessor {
return FluxDataFetcherResultProcessor()
}
@Bean
open fun webfluxCookieResolver(): CookieValueResolver {
return WebFluxCookieValueResolver()
}
}