If you are using
graphql-kotlin-spring-server, see the Spring specific documentation.
GraphQLContextFactory is a generic method for generating a GraphQL context for each request.
Given the generic server request, the interface should create a legacy
GraphQLContext class and a new context map to be used for every new operation.
The legacy context class must implement the
GraphQLContext interface from
See execution context for more info on how the context can be used in the schema functions.
The factory may return
null if a context is not required for execution. This allows the library to have a default factory that just returns
If your custom factory never returns
null, then there is no need to use nullable arguments.
However, if your custom factory may return
null, you must define the context argument as nullable in the schema functions or a runtime exception will be thrown.
The interface is marked as a
suspend function to allow the asynchronous fetching of context data.
This may be helpful if you need to call some other services to calculate a context value.
graphql-kotlin-*-server library may provide an abstract class on top of this interface so users only have to be concerned with the context class and not the server class type.
For example the
graphql-kotlin-spring-server provides the following class, which sets the request type:
For common use cases around authorization, authentication, or tracing you may need to read HTTP headers and cookies.
This should be done in the
GraphQLContextFactory and relevant data should be added to the context to be accessible during schema exectuion.
If you need federation tracing support, the context must implement the separate
FederatedGraphQLContext interface from
The reference server implementation
graphql-kotlin-spring-server supports federated tracing in the context.