Skip to content

default value fixes#739

Open
jbellenger wants to merge 2 commits intographql-go:masterfrom
jbellenger:nonnull-args-default-value
Open

default value fixes#739
jbellenger wants to merge 2 commits intographql-go:masterfrom
jbellenger:nonnull-args-default-value

Conversation

@jbellenger
Copy link
Copy Markdown

@jbellenger jbellenger commented Apr 27, 2026

Hi there! I'm the maintainer of graphql-conformance, a GraphQL verifier similar to thesis-graphql-go.

I noticed that graphql-go has a couple of deviations from the spec and I thought I might be able to contribute a fix for one of the easy ones (see graphql-go's conformance page for other gaps).

This PR fixes an issue that prevents arguments with non-nullable types from having default values.

For example, in this configuration:

# schema
type Query { x(y:Int! = 0): Int }

# query
{ x }

graphql-go returns this error:

  {
    "data": null,
    "errors": [
      {
        "message": "Field \"x\" argument \"y\" of type \"Int!\" is required but not provided.",
        "locations": [
          {
            "line": 1,
            "column": 3
          }
        ]
      }
    ]
  }

With this PR applied, the above query now returns:

  {
    "data": {
      "x": 0
    }
  }

This kind of usage is explicitly allowed by section 5.4.3 of the graphql-spec:

An argument is required if the argument type is non-null and does not have a default value. Otherwise, the argument is optional

Thank you for considering this PR! I'm not familiar with this code-base or its conventions, so please feel free to point out any issue no matter how small.

Summary by CodeRabbit

  • Bug Fixes

    • Improved GraphQL argument validation to correctly recognize that non-null arguments with default values should not be required when omitted. This prevents false validation errors for properly defined schema arguments.
  • Tests

    • Added test coverage for both field arguments and directive arguments with default values to ensure validation behaves correctly in all scenarios.

A field or directive argument declared with a non-null type and a default
value is satisfied by the default when omitted, so ProvidedNonNullArgumentsRule
must not flag it as required. Matches graphql-js's ProvidedRequiredArgumentsRule
and supports directives with the shape `@defer(if: Boolean! = true)` from the
incremental delivery RFC.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 31da9cfa-ca05-483b-905b-e500f8240a10

📥 Commits

Reviewing files that changed from the base of the PR and between d5f8f3c and 17cfd19.

📒 Files selected for processing (2)
  • rules.go
  • rules_provided_non_null_arguments_test.go

📝 Walkthrough

Walkthrough

The ProvidedNonNullArgumentsRule has been refined to only treat non-null arguments as "required" when they lack a default value. The validation logic now checks that both the argument type is non-null AND no default value exists, preventing false errors for non-null arguments satisfied via schema-level defaults.

Changes

Cohort / File(s) Summary
Rule Implementation
rules.go
Updated ProvidedNonNullArgumentsRule field-argument and directive-argument validation to add DefaultValue == nil condition, allowing non-null arguments with schema defaults to be optional.
Test Coverage
rules_provided_non_null_arguments_test.go
Added two new test cases validating that the rule passes when non-null arguments have schema-level defaults—one for field arguments and one for directive arguments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rule refined with care so fine,
Non-null arguments need not whine,
When defaults bloom like morning dew,
The schema's promise sees them through,
No errors now for arguments blessed,
With defaults making validation rest! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'default value fixes' is vague and doesn't clearly convey the specific change being made to the ProvidedNonNullArgumentsRule for handling non-null arguments with defaults. Consider a more descriptive title such as 'Allow non-null arguments with default values' or 'Fix ProvidedNonNullArgumentsRule to handle default values' to better communicate the primary change.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jbellenger jbellenger changed the title rules: allow non-null arguments with a default value default value fixes Apr 27, 2026
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 27, 2026

Coverage Status

coverage: 92.284%. remained the same — jbellenger:nonnull-args-default-value into graphql-go:master

@jbellenger jbellenger marked this pull request as ready for review April 27, 2026 12:28
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.

2 participants