Skip to content

A DataFetcherResult inside an Optional is never unwrapped #939

Closed

Description

@oryan-block

Description

A resolver method that returns a DataFetcherResult inside an Optional gives graphql-java the DataFetcherResult itself as the field value, so the data comes out wrong and its errors are lost.

class Query : GraphQLQueryResolver {
    fun payload(): Optional<DataFetcherResult<Payload>> = Optional.of(
        DataFetcherResult.newResult<Payload>().data(Payload("a")).error(GraphQLError.newError().message("partial").build()).build()
    )
}
type Query {
    payload: Payload
}

type Payload {
    data: String
}

Expected behavior

{ payload { data } } returns {"payload": {"data": "a"}} plus the partial error, the same as DataFetcherResult<Optional<Payload>> does.

Actual behavior

The schema builds, but the query returns

{"data": {"payload": {"data": "Payload@656f62dc"}}}

with no errors. A String field gets "DataFetcherResult{data=a, errors=[], localContext=null, extensions=null}" as its value.

Two other shapes fail the build instead:

  • a list, e.g. Optional<DataFetcherResult<List<Payload>>> for [Payload]

    Java class is not a List or generic type information was lost: graphql.execution.DataFetcherResult<java.util.List>

  • any other field that returns Payload some other way, e.g. a plain Payload or a DataFetcherResult<Optional<Payload>>

    Two different classes used for type Payload

Steps to reproduce the bug

  1. Return Optional<DataFetcherResult<T>> from a resolver method.
  2. Query the field.

Cause

TypeClassMatcher.PotentialMatch.unwrap checks for a DataFetcherResult before an Optional but never after one. So for Optional<DataFetcherResult<Payload>> the build binds Payload to the DataFetcherResult class. At runtime graphql-java unboxes the Optional but doesn't look for a DataFetcherResult inside it, so the field resolves data on the DataFetcherResult object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions