Skip to content

Conversation

@reicheltp
Copy link

@reicheltp reicheltp commented Jul 28, 2025

Pull request checklist

  • Please read our contributor guide
  • Consider creating a discussion on the discussion forum
    first
  • Make sure the PR doesn't introduce backward compatibility issues
  • Make sure to have sufficient test cases

Pull Request type

  • Bugfix

Changes in this PR

Make the @Source attribute more robust by allowing to be bound to an interface / abstract class parameter.

  • Updated SourceArgumentResolver to support assigning interface types by using isAssignableFrom.
  • Added test cases for handling @Source with interface types.
  • Extended GraphQL schema and test data to include Movie and Entertainment examples.

This PR solves the following problem:

Given a schema like this:

interface A {
   foo: String
}

type B extends A {
   foo: String
}

type C extends A {
   foo: String
}

Allows us to have a resolver using DgsDataFetchingEnvironment like this:

  @DgsData(parentType = "B", field = "foo")
  @DgsData(parentType = "C", field = "foo")
  public String getFoo(DgsDataFetchingEnvironment dfe) {
    A a = dfe.getSource();
    return service.resolve(a);
  }

But the implementation with @Source crashed with error: Invalid source type 'B'. Expected type 'A'. Regardless, that B is assignable to A.

  @DgsData(parentType = "B", field = "foo")
  @DgsData(parentType = "C", field = "foo")
  public String getFoo(@Source A a) {
    return service.resolve(a);
  }

Alternatives considered

  • One resolver for each type -> bloating code.
  • Sticking with dfe.getSource() -> boiler code.

@reicheltp
Copy link
Author

@paulbakker Is there something missing, to get this reviewed?

@reicheltp reicheltp force-pushed the robust-source-annotation branch from 90d04a0 to 89c8705 Compare August 19, 2025 09:27
- Updated `SourceArgumentResolver` to support assigning interface types by using `isAssignableFrom`.
- Added test cases for handling `@Source` with interface types.
- Extended GraphQL schema and test data to include `Movie` and `Entertainment` examples.
@reicheltp reicheltp force-pushed the robust-source-annotation branch from 89c8705 to e288467 Compare September 25, 2025 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant