Skip to content

Support returning DataFetcherResult from subscriptions #1795

@smyrick

Description

@smyrick

Describe the bug
In graphql-java, a subscription must return a Publisher<T> from its data fetcher. This is enforced by the SubscriptionExecutionStrategy here:

CompletableFuture<FetchedValue> fieldFetched = fetchField(executionContext, newParameters);
return fieldFetched.thenApply(fetchedValue -> {
Object publisher = fetchedValue.getFetchedValue();
if (publisher != null) {
assertTrue(publisher instanceof Publisher, "You data fetcher must return a Publisher of events when using graphql subscriptions");
}
//noinspection unchecked
return (Publisher<Object>) publisher;

Just before the check for a type of Publisher, graphql-java calls the base ExecutionStrategy.fetchField. In that method, it checks if the result is of type DataFetcherResult and if it is, unboxes it:

if (result instanceof DataFetcherResult) {
//noinspection unchecked
DataFetcherResult<?> dataFetcherResult = (DataFetcherResult) result;

That means however that the object passed to fetchField is a Publisher so it will never unbox a DataFetcherResult.

To support partial results, a second check in the SubscriptionExecutionStrategy after unboxing the Publisher, should be added to check for a DataFetcherResult again. With some refactoring of the base ExecutionStrategy some of the existing code could be used to do this.

The GraphQL spec does allow subscription to return data and errors in a single message: https://spec.graphql.org/June2018/#sec-Response-Stream

To Reproduce
Create a subscription that returns a Publisher<DataFetcherResult<T>> and it fails to parse the proper data and errors fields.

Related Issues
This was brought to our attention in graphql-kotlin ExpediaGroup/graphql-kotlin#608

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions