Skip to content

Warn at build when a resolver method is missing a parameter for an argument - #890

Merged
oryan-block merged 2 commits into
masterfrom
feature/418-warn-argument-on-context
Oct 19, 2026
Merged

oryan-block merged 2 commits into
masterfrom
feature/418-warn-argument-on-context

Conversation

@oryan-block

@oryan-block oryan-block commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #418

Checklist

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

Description

When a resolver method is missing the parameter for an argument but has a DataFetchingEnvironment, GraphQLContext or contextClass parameter instead, the parameter count still matches. So FieldResolverScanner#verifyMethodArguments accepts it and maps the arguments by position, and in the issue's example persist goes into the CustomContext parameter. The issue says this should throw like it does for DataFetchingEnvironment, but DataFetchingEnvironment isn't checked either:

type Query {
    users(first: Int): UserConnection
}
fun users(env: DataFetchingEnvironment): Connection<User>

This builds, and every query of users fails: Parameter specified as non-null is null: ... parameter env without first, Cannot construct instance of graphql.schema.DataFetchingEnvironment with it. Same for a type resolver, e.g. thumbnail(image: Image, env: DataFetchingEnvironment) for thumbnail(size: Int).

Now SchemaClassScanner#warnIfParameterMissing checks the parameter of every argument, not just the last one, and logs:

Field 'Query.users' has no parameter for its argument 'first: Int' in scenarios.ContextMisboundQuery.users, so the DataFetchingEnvironment parameter gets the argument instead. Add a parameter for each argument, in order, before the DataFetchingEnvironment parameter.

It uses the same three types verifyMethodArguments allows after the arguments, so allowedTrailingParameterTypes isn't private anymore. For the contextClass it only warns when the argument is a spec scalar, since the context class can also be a real argument type, e.g. contextClass(Map) with an input object mapped to a Map. The check runs after the field's types are scanned, so a resolver that already fails the build (like a generic method fun <T> gen(value: T)) gets the same error as before.

Nothing else changes, and the field still fails at query time like before. Throwing instead has to wait for 15.0.0: #889.

The new test in MethodFieldResolverTest covers a Map context class, a GraphQLContext in the first slot and the thumbnail case on a GraphQLResolver, plus two fields that shouldn't warn (a Map input object and a correct trailing context). mvn clean verify passes (346 tests), also with -Dgraphql-java.version=25.1. Didn't run JDK 11 locally, CI covers it.

I checked it through the installed jar in a separate consumer project with contextClass(AppContext::class):

  • users(first: Int) -> users(env), contextArgument(name: String) -> contextArgument(context: AppContext) and User.thumbnail(size: Int) -> thumbnail(user, env) each log the warning and fail at query time like before
  • greeting(name: String!) -> greeting(name, env) doesn't warn and returns "greeting: hi ada"
  • on master the same run gives the same results, with no warning

edealer-graphql's schema (351 resolvers) didn't log any of these on the first version of this change. The later changes can only make it warn less.

Release note: Resolver methods that are missing the parameter for an argument, where a DataFetchingEnvironment, GraphQLContext or context class parameter takes its place, now log a warning at build.

@oryan-block oryan-block changed the title Warn at build when an argument is bound to a context parameter Warn at build when a resolver method is missing a parameter for an argument Oct 19, 2026
@sonarqubecloud

Copy link
Copy Markdown

@oryan-block
oryan-block merged commit 3386722 into master Oct 19, 2026
7 checks passed
@oryan-block
oryan-block deleted the feature/418-warn-argument-on-context branch October 19, 2026 22:26
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.

Last argument to field resolver is not checked when using the context class schema parser option

1 participant