Skip to content

Commit 40dc47d

Browse files
author
Shane Myrick
committed
Change GraphQLContextFactory to return the interface
1 parent 2f776d4 commit 40dc47d

File tree

10 files changed

+43
-20
lines changed

10 files changed

+43
-20
lines changed

examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/context/MyGraphQLContextFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.springframework.stereotype.Component
99
* [GraphQLContextFactory] that generates [MyGraphQLContext] that will be available when processing GraphQL requests.
1010
*/
1111
@Component
12-
class MyGraphQLContextFactory : GraphQLContextFactory<MyGraphQLContext> {
12+
class MyGraphQLContextFactory : GraphQLContextFactory {
1313

1414
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): MyGraphQLContext = MyGraphQLContext(
1515
myCustomValue = request.headers.getFirst("MyHeader") ?: "defaultContext",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.execution
18+
19+
/**
20+
* Can be used as a default [GraphQLContext] if there is none provided.
21+
*/
22+
class EmptyGraphQLContext : GraphQLContext

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/GraphQLAutoConfiguration.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ class GraphQLAutoConfiguration {
123123

124124
@Bean
125125
@ConditionalOnMissingBean
126-
fun graphQLContextFactory(): GraphQLContextFactory<*> = EmptyContextFactory
126+
fun graphQLContextFactory(): GraphQLContextFactory = EmptyContextFactory
127127

128128
@Bean
129129
@ConditionalOnMissingBean
130130
fun contextWebFilter(
131131
config: GraphQLConfigurationProperties,
132-
graphQLContextFactory: GraphQLContextFactory<*>
132+
graphQLContextFactory: GraphQLContextFactory
133133
): ContextWebFilter = ContextWebFilter(config, graphQLContextFactory)
134134
}

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/execution/ContextWebFilter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const val GRAPHQL_CONTEXT_FILTER_ODER = 0
3333
/**
3434
* Default web filter that populates GraphQL context in the reactor subscriber context.
3535
*/
36-
open class ContextWebFilter(config: GraphQLConfigurationProperties, private val contextFactory: GraphQLContextFactory<Any>) : WebFilter, Ordered {
36+
open class ContextWebFilter(config: GraphQLConfigurationProperties, private val contextFactory: GraphQLContextFactory) : WebFilter, Ordered {
3737
private val graphQLRoute = enforceAbsolutePath(config.endpoint)
3838
private val subscriptionsRoute = enforceAbsolutePath(config.subscriptions.endpoint)
3939

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/execution/GraphQLContextFactory.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package com.expediagroup.graphql.spring.execution
1818

19-
import graphql.GraphQLContext
19+
import com.expediagroup.graphql.execution.EmptyGraphQLContext
20+
import com.expediagroup.graphql.execution.GraphQLContext
2021
import org.springframework.http.server.reactive.ServerHttpRequest
2122
import org.springframework.http.server.reactive.ServerHttpResponse
2223

@@ -28,18 +29,18 @@ const val GRAPHQL_CONTEXT_KEY = "graphQLContext"
2829
/**
2930
* Factory that generates GraphQL context.
3031
*/
31-
interface GraphQLContextFactory<out T : Any> {
32+
interface GraphQLContextFactory {
3233

3334
/**
3435
* Generate GraphQL context based on the incoming request and the corresponding response.
3536
*/
36-
suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): T
37+
suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): GraphQLContext
3738
}
3839

3940
/**
4041
* Default context factory that generates empty GraphQL context.
4142
*/
42-
internal object EmptyContextFactory : GraphQLContextFactory<GraphQLContext> {
43+
internal object EmptyContextFactory : GraphQLContextFactory {
4344

44-
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): GraphQLContext = GraphQLContext.newContext().build()
45+
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): EmptyGraphQLContext = EmptyGraphQLContext()
4546
}

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/SchemaConfigurationTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ class SchemaConfigurationTest {
166166
.build()
167167

168168
@Bean
169-
fun myCustomContextFactory(): GraphQLContextFactory<Map<String, Any>> = mockk()
169+
fun myCustomContextFactory(): GraphQLContextFactory = mockk()
170170

171171
@Bean
172172
fun myDataLoaderRegistryFactory(): DataLoaderRegistryFactory = mockk()
173173

174174
@Bean
175175
fun myCustomContextWebFilter(
176176
config: GraphQLConfigurationProperties,
177-
graphQLContextFactory: GraphQLContextFactory<*>
177+
graphQLContextFactory: GraphQLContextFactory
178178
): ContextWebFilter = object : ContextWebFilter(config, graphQLContextFactory) {
179179
private val regex = config.endpoint.toRegex()
180180

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/context/ContextWebFilterTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.expediagroup.graphql.spring.context
1818

19+
import com.expediagroup.graphql.execution.EmptyGraphQLContext
1920
import com.expediagroup.graphql.spring.GraphQLConfigurationProperties
2021
import com.expediagroup.graphql.spring.execution.ContextWebFilter
2122
import com.expediagroup.graphql.spring.execution.GRAPHQL_CONTEXT_KEY
2223
import com.expediagroup.graphql.spring.execution.GraphQLContextFactory
23-
import graphql.GraphQLContext
2424
import io.mockk.coEvery
2525
import io.mockk.every
2626
import io.mockk.mockk
@@ -54,16 +54,16 @@ class ContextWebFilterTest {
5454
}
5555
}
5656

57-
val simpleFactory: GraphQLContextFactory<Any> = mockk {
58-
coEvery { generateContext(any(), any()) } returns GraphQLContext.newContext().build()
57+
val simpleFactory: GraphQLContextFactory = mockk {
58+
coEvery { generateContext(any(), any()) } returns EmptyGraphQLContext()
5959
}
6060

6161
val contextFilter = ContextWebFilter(GraphQLConfigurationProperties(packages = listOf("com.expediagroup.graphql")), simpleFactory)
6262
StepVerifier.create(contextFilter.filter(exchange, chain))
6363
.verifyComplete()
6464

6565
assertNotNull(generatedContext)
66-
val graphQLContext = generatedContext?.getOrDefault<GraphQLContext>(GRAPHQL_CONTEXT_KEY, null)
66+
val graphQLContext = generatedContext?.getOrDefault<EmptyGraphQLContext>(GRAPHQL_CONTEXT_KEY, null)
6767
assertNotNull(graphQLContext)
6868
}
6969

@@ -90,8 +90,8 @@ class ContextWebFilterTest {
9090
}
9191
}
9292

93-
val simpleFactory: GraphQLContextFactory<Any> = mockk {
94-
coEvery { generateContext(any(), any()) } returns GraphQLContext.newContext().build()
93+
val simpleFactory: GraphQLContextFactory = mockk {
94+
coEvery { generateContext(any(), any()) } returns EmptyGraphQLContext()
9595
}
9696

9797
val contextFilter = ContextWebFilter(GraphQLConfigurationProperties(packages = listOf("com.expediagroup.graphql")), simpleFactory)

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/context/GraphQLContextFactoryIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class GraphQLContextFactoryIT(@Autowired private val testClient: WebTestClient)
6767

6868
@Bean
6969
@ExperimentalCoroutinesApi
70-
fun customContextFactory(): GraphQLContextFactory<CustomContext> = object : GraphQLContextFactory<CustomContext> {
70+
fun customContextFactory(): GraphQLContextFactory = object : GraphQLContextFactory {
7171
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): CustomContext {
7272
val firstValue = coroutineContext[ReactorContext]?.context?.get<String>("firstFilterValue")
7373
return CustomContext(

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/execution/SubscriptionWebSocketHandlerIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class SubscriptionWebSocketHandlerIT(@LocalServerPort private var port: Int) {
170170
fun subscription(): Subscription = SimpleSubscription()
171171

172172
@Bean
173-
fun customContextFactory(): GraphQLContextFactory<SubscriptionContext> = object : GraphQLContextFactory<SubscriptionContext> {
173+
fun customContextFactory(): GraphQLContextFactory = object : GraphQLContextFactory {
174174
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): SubscriptionContext = SubscriptionContext(
175175
value = request.headers.getFirst("X-Custom-Header") ?: "default"
176176
)

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/routes/RouteConfigurationIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RouteConfigurationIT(@Autowired private val testClient: WebTestClient) {
5050
fun query(): Query = SimpleQuery()
5151

5252
@Bean
53-
fun customContextFactory(): GraphQLContextFactory<CustomContext> = object : GraphQLContextFactory<CustomContext> {
53+
fun customContextFactory(): GraphQLContextFactory = object : GraphQLContextFactory {
5454
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): CustomContext = CustomContext(
5555
value = request.headers.getFirst("X-Custom-Header") ?: "default"
5656
)

0 commit comments

Comments
 (0)