Skip to content

Commit ae742b0

Browse files
authored
[server] update default GraphQLContext (ExpediaGroup#955)
Some time ago we updated our `GraphQLContext` parameter handling logic from requiring explicit `@GraphQLContext` parameter annotation to requiring context classes to implement our marker interface but we never updated our fallback logic. By default, `spring-server` created `EmptyGraphQLContext` context factory with a fallback to `graphql-java` `GraphQLContext` that could never be specified as function parameter as it (obviously) did not implement our interface. Updating default generated `GraphQLContext` to more usable `DefaultGraphQLContext` that holds concurrent hash map of arbitrary elements (similar to `graphql-java` default).
1 parent 4fa5406 commit ae742b0

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/execution/GraphQLContext.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616

1717
package com.expediagroup.graphql.execution
1818

19+
import java.util.concurrent.ConcurrentHashMap
20+
import java.util.concurrent.ConcurrentMap
21+
1922
/**
2023
* Marker interface to indicate that the implementing class should be considered
2124
* as the GraphQL context. This means the implementing class will not appear in the schema.
2225
*/
2326
interface GraphQLContext
2427

2528
/**
26-
* Can be used as a default [GraphQLContext] if there is none provided.
29+
* Default [GraphQLContext] that can be used if there is none provided. Exposes generic concurrent hash map
30+
* that can be populated with custom data.
2731
*/
28-
class EmptyGraphQLContext : GraphQLContext
32+
class DefaultGraphQLContext : GraphQLContext {
33+
val contents: ConcurrentMap<Any, Any> = ConcurrentHashMap()
34+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import com.expediagroup.graphql.execution.KotlinDataFetcherFactoryProvider
2020
import com.expediagroup.graphql.spring.exception.KotlinDataFetcherExceptionHandler
2121
import com.expediagroup.graphql.spring.execution.ContextWebFilter
2222
import com.expediagroup.graphql.spring.execution.DataLoaderRegistryFactory
23-
import com.expediagroup.graphql.spring.execution.EmptyContextFactory
23+
import com.expediagroup.graphql.spring.execution.DefaultContextFactory
2424
import com.expediagroup.graphql.spring.execution.EmptyDataLoaderRegistryFactory
2525
import com.expediagroup.graphql.spring.execution.GraphQLContextFactory
2626
import com.expediagroup.graphql.spring.execution.SpringKotlinDataFetcherFactoryProvider
@@ -58,7 +58,7 @@ class GraphQLExecutionConfiguration {
5858

5959
@Bean
6060
@ConditionalOnMissingBean
61-
fun graphQLContextFactory(): GraphQLContextFactory<*> = EmptyContextFactory
61+
fun graphQLContextFactory(): GraphQLContextFactory<*> = DefaultContextFactory
6262

6363
@Bean
6464
@ConditionalOnMissingBean

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

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

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

19-
import com.expediagroup.graphql.execution.EmptyGraphQLContext
19+
import com.expediagroup.graphql.execution.DefaultGraphQLContext
2020
import com.expediagroup.graphql.execution.GraphQLContext
2121
import org.springframework.http.server.reactive.ServerHttpRequest
2222
import org.springframework.http.server.reactive.ServerHttpResponse
@@ -38,9 +38,9 @@ interface GraphQLContextFactory<out T : GraphQLContext> {
3838
}
3939

4040
/**
41-
* Default context factory that generates empty GraphQL context.
41+
* Default context factory that generates GraphQL context with empty concurrent map that can store any elements.
4242
*/
43-
internal object EmptyContextFactory : GraphQLContextFactory<EmptyGraphQLContext> {
43+
internal object DefaultContextFactory : GraphQLContextFactory<DefaultGraphQLContext> {
4444

45-
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse) = EmptyGraphQLContext()
45+
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse) = DefaultGraphQLContext()
4646
}

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

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

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

19+
import com.expediagroup.graphql.execution.DefaultGraphQLContext
1920
import com.expediagroup.graphql.types.GraphQLRequest
2021
import graphql.ExecutionInput
21-
import graphql.GraphQLContext
2222
import org.dataloader.DataLoaderRegistry
2323

2424
/**
@@ -29,6 +29,6 @@ fun GraphQLRequest.toExecutionInput(graphQLContext: Any? = null, dataLoaderRegis
2929
.query(this.query)
3030
.operationName(this.operationName)
3131
.variables(this.variables ?: emptyMap())
32-
.context(graphQLContext ?: GraphQLContext.newContext().build())
32+
.context(graphQLContext ?: DefaultGraphQLContext())
3333
.dataLoaderRegistry(dataLoaderRegistry ?: DataLoaderRegistry())
3434
.build()

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

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

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

19-
import com.expediagroup.graphql.execution.EmptyGraphQLContext
19+
import com.expediagroup.graphql.execution.DefaultGraphQLContext
2020
import com.expediagroup.graphql.spring.GraphQLConfigurationProperties
2121
import com.expediagroup.graphql.spring.execution.ContextWebFilter
2222
import com.expediagroup.graphql.spring.execution.GRAPHQL_CONTEXT_KEY
@@ -55,15 +55,15 @@ class ContextWebFilterTest {
5555
}
5656

5757
val simpleFactory: GraphQLContextFactory<*> = mockk {
58-
coEvery { generateContext(any(), any()) } returns EmptyGraphQLContext()
58+
coEvery { generateContext(any(), any()) } returns DefaultGraphQLContext()
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<EmptyGraphQLContext>(GRAPHQL_CONTEXT_KEY, null)
66+
val graphQLContext = generatedContext?.getOrDefault<DefaultGraphQLContext>(GRAPHQL_CONTEXT_KEY, null)
6767
assertNotNull(graphQLContext)
6868
}
6969

@@ -91,7 +91,7 @@ class ContextWebFilterTest {
9191
}
9292

9393
val simpleFactory: GraphQLContextFactory<*> = mockk {
94-
coEvery { generateContext(any(), any()) } returns EmptyGraphQLContext()
94+
coEvery { generateContext(any(), any()) } returns DefaultGraphQLContext()
9595
}
9696

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

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

19+
import com.expediagroup.graphql.execution.GraphQLContext
1920
import com.expediagroup.graphql.types.GraphQLRequest
20-
import graphql.GraphQLContext
2121
import io.mockk.mockk
2222
import org.dataloader.DataLoaderRegistry
2323
import org.junit.jupiter.api.Test

0 commit comments

Comments
 (0)