Skip to content

Allow resolver methods to take both the context and the environment - #888

Merged
oryan-block merged 1 commit into
masterfrom
feature/context-and-environment
Oct 19, 2026
Merged

oryan-block merged 1 commit into
masterfrom
feature/context-and-environment

Conversation

@oryan-block

Copy link
Copy Markdown
Collaborator

Checklist

  • Pull requests follows the contribution guide
  • New or modified functionality is covered by tests

Description

Currently a resolver method can only take one extra parameter after the schema arguments: DataFetchingEnvironment, graphql.GraphQLContext or the class set with contextClass. So a method that needs both the context and the environment has to take the environment and dig the context out of it:

public String greeting(String name, DataFetchingEnvironment environment) {
    var context = environment.getGraphQlContext().<AppContext>get(AppContext.class);
    // ...
}

Now it can take both:

fun greeting(name: String, context: AppContext, env: DataFetchingEnvironment): String

Any number of trailing parameters, in any order, as long as each one is one of those three types. FieldResolverScanner#verifyMethodArguments checks every trailing parameter instead of only the last one, and MethodFieldResolver#createDataFetcher adds 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) and foo(ctx, env), both match now and whichever is found first wins. Before, only foo(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 DataFetchingEnvironment together, in any order, after the schema arguments.

Testing

Added a test in MethodFieldResolverDataFetcherTest for the custom context followed by the environment and updated the two error message assertions in SchemaParserTest. mvn clean verify passes 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 a GraphQLResolver<AppUser> -> "locale of oryan: en-CA"
  • environment only and context only still work
  • a trailing String parameter still fails the build, with the new message

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@oryan-block
oryan-block merged commit e390d21 into master Oct 19, 2026
6 of 7 checks passed
@oryan-block
oryan-block deleted the feature/context-and-environment branch October 19, 2026 13:39
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