Repository navigation
Allow resolver methods to take both the context and the environment - #888
Merged
Merged
Conversation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Checklist
Description
Currently a resolver method can only take one extra parameter after the schema arguments:
DataFetchingEnvironment,graphql.GraphQLContextor the class set withcontextClass. So a method that needs both the context and the environment has to take the environment and dig the context out of it:Now it can take both:
Any number of trailing parameters, in any order, as long as each one is one of those three types.
FieldResolverScanner#verifyMethodArgumentschecks every trailing parameter instead of only the last one, andMethodFieldResolver#createDataFetcheradds an argument for each. The "No method found" error now says(optionally followed by any of [...])instead of(with or without one of [...] as the last argument).One thing that changes: if a class has both
foo(env)andfoo(ctx, env), both match now and whichever is found first wins. Before, onlyfoo(env)matched. I doubt anyone has that but worth mentioning.No public API change, nothing that builds today should stop building.
Release note: Resolver methods can take the context and
DataFetchingEnvironmenttogether, in any order, after the schema arguments.Testing
Added a test in
MethodFieldResolverDataFetcherTestfor the custom context followed by the environment and updated the two error message assertions inSchemaParserTest.mvn clean verifypasses on JDK 25. Didn't run JDK 11 or graphql-java 25.1 locally, CI covers those.Also checked through the public API with a small app using
contextClass(AppContext::class):{ greeting(name: "ada") farewell(name: "ada") user { name locale } environmentOnly contextOnly }greeting(name, context, env)->"greeting: hi ada, from oryan"farewell(name, env, context)->"farewell: bye ada, from oryan"locale(user, GraphQLContext, env)on aGraphQLResolver<AppUser>->"locale of oryan: en-CA"Stringparameter still fails the build, with the new message