Skip to main content
Version: 6.x.x

Generating GraphQL Context

graphql-kotlin-spring-server provides a Spring specific implementation of GraphQLContextFactory and the context.

  • SpringGraphQLContext (deprecated) - Implements the Spring ServerRequest and federation tracing HTTPRequestHeaders
  • SpringGraphQLContextFactory - Generates GraphQL context map with federated tracing information per request

If you are using graphql-kotlin-spring-server, you should extend DefaultSpringGraphQLContextFactory to automatically support federated tracing.

@Component
class MyGraphQLContextFactory : DefaultSpringGraphQLContextFactory() {
override suspend fun generateContextMap(request: ServerRequest): Map<*, Any> = super.generateContextMap(request) + mapOf(
"myCustomValue" to (request.headers().firstHeader("MyHeader") ?: "defaultContext")
)
}

Once your application is configured to build your custom GraphQL context map, you can then access it through a data fetching environment argument. While executing the query, data fetching environment will be automatically injected to the function input arguments. This argument will not appear in the GraphQL schema.

For more details, see the Contextual Data Documentation.

Federated Context

If you need federation tracing support, you can set the appropriate configuration properties. The provided DefaultSpringGraphQLContextFactory populates the required information for federated tracing, so as long as you extend this context class you will maintain feature support.