Data Fetching Context

Each data fetcher in [GraphQL] Java has a context. A data fetcher gets access to its context by calling DataFetchingEnvironment.getContext(). This is a common mechanism to pass request context to data fetchers and data loaders. The DGS framework has its own DgsContext implementation, which is used for log instrumentation among other things. It is designed in such a way that you can extend it with your own custom context.

To create a custom context, implement a Spring bean of type DgsCustomContextBuilder. Write the build() method so that it creates an instance of the type that represents your custom context object:

@Component
public class MyContextBuilder implements DgsCustomContextBuilder<MyContext> {
    @Override
    public MyContext build() {
        return new MyContext();
    }
}

public class MyContext {
    private final String customState = "Custom state!";

    public String getCustomState() {
        return customState;
    }
}

A data fetcher can now retrieve the context by calling the getCustomContext() method:

@DgsData(parentType = "Query", field = "withContext")
public String withContext(DataFetchingEnvironment dfe) {
    MyContext customContext = DgsContext.getCustomContext(dfe);
    return customContext.getCustomState();
}

Similarly, custom context can be used in a DataLoader.

@DgsDataLoader(name = "exampleLoaderWithContext")
public class ExampleLoaderWithContext implements BatchLoaderWithContext<String, String> {
    @Override
    public CompletionStage<List<String>> load(List<String> keys, BatchLoaderEnvironment environment) {

        MyContext context = DgsContext.getCustomContext(environment);

        return CompletableFuture.supplyAsync(() -> keys.stream().map(key -> context.getCustomState() + " " + key).collect(Collectors.toList()));
    }
}