-
Notifications
You must be signed in to change notification settings - Fork 285
/
DgsAutoConfiguration.kt
237 lines (216 loc) · 10.2 KB
/
DgsAutoConfiguration.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright 2022 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.autoconfig
import com.netflix.graphql.dgs.DgsFederationResolver
import com.netflix.graphql.dgs.DgsQueryExecutor
import com.netflix.graphql.dgs.context.DgsCustomContextBuilder
import com.netflix.graphql.dgs.context.DgsCustomContextBuilderWithRequest
import com.netflix.graphql.dgs.exceptions.DefaultDataFetcherExceptionHandler
import com.netflix.graphql.dgs.internal.*
import com.netflix.graphql.dgs.internal.DefaultDgsQueryExecutor.ReloadSchemaIndicator
import com.netflix.graphql.dgs.scalars.UploadScalar
import com.netflix.graphql.mocking.MockProvider
import graphql.execution.AsyncExecutionStrategy
import graphql.execution.AsyncSerialExecutionStrategy
import graphql.execution.DataFetcherExceptionHandler
import graphql.execution.ExecutionIdProvider
import graphql.execution.ExecutionStrategy
import graphql.execution.instrumentation.ChainedInstrumentation
import graphql.execution.instrumentation.Instrumentation
import graphql.execution.preparsed.PreparsedDocumentProvider
import graphql.schema.DataFetcherFactory
import graphql.schema.GraphQLCodeRegistry
import graphql.schema.GraphQLSchema
import graphql.schema.idl.TypeDefinitionRegistry
import graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY
import graphql.schema.visibility.GraphqlFieldVisibility
import graphql.schema.visibility.NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.autoconfigure.ImportAutoConfiguration
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
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 java.util.*
import kotlin.streams.toList
/**
* Framework auto configuration based on open source Spring only, without Netflix integrations.
* This does NOT have logging, tracing, metrics and security integration.
*/
@Suppress("SpringJavaInjectionPointsAutowiringInspection")
@Configuration
@EnableConfigurationProperties(value = [DgsConfigurationProperties::class, DgsIntrospectionConfigurationProperties::class])
@ImportAutoConfiguration(classes = [JacksonAutoConfiguration::class])
open class DgsAutoConfiguration(private val configProps: DgsConfigurationProperties) {
companion object {
const val AUTO_CONF_PREFIX = "dgs.graphql"
}
@Bean
open fun dgsQueryExecutor(
applicationContext: ApplicationContext,
schema: GraphQLSchema,
schemaProvider: DgsSchemaProvider,
dgsDataLoaderProvider: DgsDataLoaderProvider,
dgsContextBuilder: DefaultDgsGraphQLContextBuilder,
dataFetcherExceptionHandler: DataFetcherExceptionHandler,
instrumentations: ObjectProvider<Instrumentation>,
environment: Environment,
@Qualifier("query") providedQueryExecutionStrategy: Optional<ExecutionStrategy>,
@Qualifier("mutation") providedMutationExecutionStrategy: Optional<ExecutionStrategy>,
idProvider: Optional<ExecutionIdProvider>,
reloadSchemaIndicator: ReloadSchemaIndicator,
preparsedDocumentProvider: ObjectProvider<PreparsedDocumentProvider>,
queryValueCustomizer: QueryValueCustomizer
): DgsQueryExecutor {
val queryExecutionStrategy =
providedQueryExecutionStrategy.orElse(AsyncExecutionStrategy(dataFetcherExceptionHandler))
val mutationExecutionStrategy =
providedMutationExecutionStrategy.orElse(AsyncSerialExecutionStrategy(dataFetcherExceptionHandler))
val instrumentationImpls = instrumentations.orderedStream().toList()
val instrumentation: Instrumentation? = when {
instrumentationImpls.size == 1 -> instrumentationImpls.single()
instrumentationImpls.isNotEmpty() -> ChainedInstrumentation(instrumentationImpls)
else -> null
}
return DefaultDgsQueryExecutor(
defaultSchema = schema,
schemaProvider = schemaProvider,
dataLoaderProvider = dgsDataLoaderProvider,
contextBuilder = dgsContextBuilder,
instrumentation = instrumentation,
queryExecutionStrategy = queryExecutionStrategy,
mutationExecutionStrategy = mutationExecutionStrategy,
idProvider = idProvider,
reloadIndicator = reloadSchemaIndicator,
preparsedDocumentProvider = preparsedDocumentProvider.ifAvailable,
queryValueCustomizer = queryValueCustomizer
)
}
@Bean
@ConditionalOnMissingBean
open fun defaultQueryValueCustomizer(): QueryValueCustomizer {
return QueryValueCustomizer { a -> a }
}
@Bean
open fun dgsDataLoaderProvider(applicationContext: ApplicationContext): DgsDataLoaderProvider {
return DgsDataLoaderProvider(applicationContext)
}
/**
* Used by the [DefaultDgsQueryExecutor], it controls if, and when, such executor should reload the schema.
* This implementation will return either the boolean value of the `dgs.reload` flag
* or `true` if the `laptop` profile is an active Spring Boot profiles.
* <p>
* You can provide a bean of type [ReloadSchemaIndicator] if you want to control when the
* [DefaultDgsQueryExecutor] should reload the schema.
*
* @implSpec the implementation of such bean should be thread-safe.
*/
@Bean
@ConditionalOnMissingBean
open fun defaultReloadSchemaIndicator(environment: Environment): ReloadSchemaIndicator {
val isLaptopProfile = environment.activeProfiles.contains("laptop")
val hotReloadSetting = environment.getProperty("dgs.reload", Boolean::class.java, isLaptopProfile)
return ReloadSchemaIndicator {
hotReloadSetting
}
}
@Bean
@ConditionalOnMissingBean
open fun dgsSchemaProvider(
applicationContext: ApplicationContext,
federationResolver: Optional<DgsFederationResolver>,
existingTypeDefinitionFactory: Optional<TypeDefinitionRegistry>,
existingCodeRegistry: Optional<GraphQLCodeRegistry>,
mockProviders: Optional<Set<MockProvider>>,
dataFetcherResultProcessors: List<DataFetcherResultProcessor>,
dataFetcherExceptionHandler: Optional<DataFetcherExceptionHandler> = Optional.empty(),
cookieValueResolver: Optional<CookieValueResolver> = Optional.empty(),
inputObjectMapper: Optional<InputObjectMapper> = Optional.empty(),
entityFetcherRegistry: EntityFetcherRegistry,
defaultDataFetcherFactory: Optional<DataFetcherFactory<*>> = Optional.empty()
): DgsSchemaProvider {
return DgsSchemaProvider(
applicationContext = applicationContext,
federationResolver = federationResolver,
existingTypeDefinitionRegistry = existingTypeDefinitionFactory,
mockProviders = mockProviders,
schemaLocations = configProps.schemaLocations,
dataFetcherResultProcessors = dataFetcherResultProcessors,
dataFetcherExceptionHandler = dataFetcherExceptionHandler,
cookieValueResolver = cookieValueResolver,
inputObjectMapper = inputObjectMapper.orElse(DefaultInputObjectMapper()),
entityFetcherRegistry = entityFetcherRegistry,
defaultDataFetcherFactory = defaultDataFetcherFactory
)
}
@Bean
open fun entityFetcherRegistry(): EntityFetcherRegistry {
return EntityFetcherRegistry()
}
@Bean
@ConditionalOnMissingBean
open fun dataFetcherExceptionHandler(): DataFetcherExceptionHandler {
return DefaultDataFetcherExceptionHandler()
}
@Bean
@ConditionalOnMissingBean
open fun schema(dgsSchemaProvider: DgsSchemaProvider, fieldVisibility: GraphqlFieldVisibility): GraphQLSchema {
return dgsSchemaProvider.schema(null, fieldVisibility)
}
@Bean
@ConditionalOnProperty(
prefix = "$AUTO_CONF_PREFIX.introspection",
name = ["enabled"], havingValue = "false", matchIfMissing = false
)
open fun noIntrospectionFieldVisibility(): GraphqlFieldVisibility {
return NO_INTROSPECTION_FIELD_VISIBILITY
}
@Bean
@ConditionalOnMissingBean
open fun defaultFieldVisibility(): GraphqlFieldVisibility {
return DEFAULT_FIELD_VISIBILITY
}
@Bean
@ConditionalOnMissingBean
open fun graphQLContextBuilder(
dgsCustomContextBuilder: Optional<DgsCustomContextBuilder<*>>,
dgsCustomContextBuilderWithRequest: Optional<DgsCustomContextBuilderWithRequest<*>>
): DefaultDgsGraphQLContextBuilder {
return DefaultDgsGraphQLContextBuilder(dgsCustomContextBuilder, dgsCustomContextBuilderWithRequest)
}
@Bean
open fun uploadScalar(): UploadScalar {
return UploadScalar()
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = ["reactor.core.publisher.Mono"])
open fun monoReactiveDataFetcherResultProcessor(): MonoDataFetcherResultProcessor {
return MonoDataFetcherResultProcessor()
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = ["reactor.core.publisher.Flux"])
open fun fluxReactiveDataFetcherResultProcessor(): FluxDataFetcherResultProcessor {
return FluxDataFetcherResultProcessor()
}
}