Skip to content

Leveraging GraphQLContext

As of graphql-java version 17, GraphQLContext is now the approved context mechanism (replacing the previously used opaque Context) allowing passing of pertinent context via key/value pairs to provide frameworks and userspace code the capability to contribute, share and leverage various pieces of context independent of each other.

To make this easily leverageable by DGS customers, a new interface has been provided for which any Spring Beans registered that implement the GraphQLContextContributor interface, their contribute method will be invoked before any normal instrumentation classes are invoked, allowing implementors to inspect anything provided via the DgsRequestData (i.e.: http headers), and set values on the provided GraphQLContext via a provided GraphQLContext.Builder.


public class ExampleGraphQLContextContributor implements GraphQLContextContributor {

    public void contribute(@NotNull GraphQLContext.Builder builder, @Nullable Map<String, ?> extensions, @Nullable DgsRequestData dgsRequestData) {
        if (dgsRequestData != null && dgsRequestData.getHeaders() != null) {
            String contributedContextHeader = dgsRequestData.getHeaders().getFirst("x-context-contributor-header");
            if (CONTEXT_CONTRIBUTOR_HEADER_VALUE.equals("enabled")) {
                builder.put('exampleContributorEnabled', "true");