Skip to content

Conversation

@thearchitector
Copy link

@thearchitector thearchitector commented Dec 5, 2025

Adds support for overriding the GraphQL type on resolver arguments. Useful to avoid static typing errors when GraphQL must deal in one scalar (ie. BigInt) but Python in another (ie. int).

Description

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

#3901

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Add support for explicitly overriding the GraphQL type of resolver arguments via strawberry.argument, and document and test this behavior.

New Features:

  • Allow specifying a graphql_type on strawberry.argument to override the GraphQL type used for a resolver argument.

Enhancements:

  • Propagate argument-level graphql_type overrides into StrawberryArgument type annotations, including in federation helpers.

Documentation:

  • Document how to use graphql_type with strawberry.argument in the queries guide and add a release note describing the new capability.

Tests:

  • Add a test ensuring that arguments annotated with graphql_type use the overridden scalar type in the schema.

Chores:

  • Introduce a RELEASE.md entry declaring a minor release with an example of using graphql_type on arguments.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 5, 2025

Reviewer's Guide

Adds a graphql_type override option to strawberry.argument (including federation.argument) so resolver argument GraphQL types can differ from their Python annotations, and documents/tests this new behavior plus adds a release note.

File-Level Changes

Change Details Files
Allow resolver arguments to override their GraphQL type via strawberry.argument(graphql_type=...).
  • Extend StrawberryArgumentAnnotation to store an optional graphql_type override.
  • In StrawberryArgument.init, when encountering a StrawberryArgumentAnnotation with graphql_type set, replace the argument type_annotation with a StrawberryAnnotation built from that override.
  • Expose a graphql_type parameter on strawberry.types.arguments.argument and pass it through to StrawberryArgumentAnnotation.
  • Expose a matching graphql_type parameter on strawberry.federation.argument.argument and forward it to the core argument helper.
strawberry/types/arguments.py
strawberry/federation/argument.py
Document and test the new graphql_type override behavior for arguments and add release notes.
  • Add a test ensuring that an Annotated argument using strawberry.argument(graphql_type=list[BigInt]) produces a StrawberryList of the custom BigInt scalar in the schema definition.
  • Extend the queries documentation to describe using graphql_type on strawberry.argument for argument type overrides, with a concrete BigInt example.
  • Add RELEASE.md entry describing the new graphql_type parameter usage and example schema output.
tests/fields/test_arguments.py
docs/general/queries.md
RELEASE.md

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `strawberry/types/arguments.py:128-129` </location>
<code_context>
                         self.deprecation_reason = arg.deprecation_reason
                         self.directives = arg.directives
                         self.metadata = arg.metadata
+                        if arg.graphql_type is not None:
+                            self.type_annotation = StrawberryAnnotation(
+                                arg.graphql_type
+                            )
</code_context>

<issue_to_address>
**question (bug_risk):** Clarify/ensure precedence rules when both a Python annotation and `graphql_type` are present

Since `graphql_type` now always overrides the existing `type_annotation`, please confirm this matches the intended precedence with Python type hints and any other decorators that modify `type_annotation`. Also consider whether `graphql_type` might ever already be a `StrawberryAnnotation`, in which case we should avoid double-wrapping.
</issue_to_address>

### Comment 2
<location> `RELEASE.md:15-19` </location>
<code_context>
[email protected]
+class Query:
+    @strawberry.field()
+    def a(self, a: int = strawberry.argument(graphql_type=BigInt)) -> str:
+        return "3.4"
+
+
+schema = strawberry.Schema(Query)
+
+str(
+    schema
+) == """
+scalar BigInt
+
+type Query {
+  name(arg: BigInt!): String!
+}
</code_context>

<issue_to_address>
**issue (typo):** The field and argument names in the Python example and the printed schema don’t match.

The resolver is defined as `def a(self, a: int = strawberry.argument(...))`, but the schema string shows `name(arg: BigInt!): String!`. Please update either the resolver or the schema snippet so the field and argument names match to avoid confusion.

```suggestion
@strawberry.type
class Query:
    @strawberry.field()
    def name(self, arg: int = strawberry.argument(graphql_type=BigInt)) -> str:
        return "3.4"
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +128 to +129
if arg.graphql_type is not None:
self.type_annotation = StrawberryAnnotation(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Clarify/ensure precedence rules when both a Python annotation and graphql_type are present

Since graphql_type now always overrides the existing type_annotation, please confirm this matches the intended precedence with Python type hints and any other decorators that modify type_annotation. Also consider whether graphql_type might ever already be a StrawberryAnnotation, in which case we should avoid double-wrapping.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had this thought, but decided that if it wasn't a problem to the lazy reference, it wouldn't be a problem here either? happy to defer if that is not sound reasoning

@botberry
Copy link
Member

botberry commented Dec 5, 2025

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Adds a graphql_type parameter to strawberry.argument that allows you
to explicitly override the GraphQL type of an argument, useful for static typing
when the Python type differs from the desired GraphQL type.

For example:

BigInt = strawberry.scalar(
    int, name="BigInt", serialize=lambda v: str(v), parse_value=lambda v: int(v)
)


@strawberry.type
class Query:
    @strawberry.field()
    def username(
        self, user_id: Annotated[int, strawberry.argument(graphql_type=BigInt)]
    ) -> str:
        return "foobar"


schema = strawberry.Schema(Query)

str(
    schema
) == """
scalar BigInt

type Query {
  username(userId: BigInt!): String!
}
"""

Here's the tweet text:

🆕 Release (next) is out! Thanks to Elias Gabriel for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 5, 2025

Greptile Overview

Greptile Summary

This PR adds support for overriding the GraphQL type on resolver arguments via a new graphql_type parameter on strawberry.argument. This addresses issue #3901, allowing developers to specify a different GraphQL type than the Python type annotation - useful when custom scalars (like BigInt) need to be used in GraphQL while Python code works with built-in types (like int).

  • Added graphql_type parameter to StrawberryArgumentAnnotation class and argument() function in strawberry/types/arguments.py
  • Extended federation's argument() function to also accept graphql_type parameter
  • Added comprehensive test verifying the type override works correctly with list[BigInt]
  • Added documentation in docs/general/queries.md explaining the feature with a clear example

Confidence Score: 5/5

  • This PR is safe to merge - it adds a new optional parameter with no breaking changes to existing functionality.
  • The implementation is clean, follows existing patterns (similar to graphql_type on fields), includes tests, and documentation. The changes are additive and don't modify existing behavior.
  • No files require special attention.

Important Files Changed

File Analysis

Filename Score Overview
strawberry/types/arguments.py 5/5 Added graphql_type parameter to StrawberryArgumentAnnotation and argument() function. The implementation correctly overrides type_annotation when graphql_type is specified.
strawberry/federation/argument.py 5/5 Added graphql_type parameter to federation's argument() function and properly passes it to StrawberryArgumentAnnotation.
tests/fields/test_arguments.py 5/5 Added test for graphql_type override on resolver arguments, verifying the type annotation is correctly overridden.
docs/general/queries.md 5/5 Added documentation for the new graphql_type argument feature with a clear example.
RELEASE.md 5/5 Release notes for the new minor feature with a working code example.

Sequence Diagram

sequenceDiagram
    participant User as Developer Code
    participant Arg as strawberry.argument()
    participant SAA as StrawberryArgumentAnnotation
    participant SA as StrawberryArgument
    participant Schema as GraphQL Schema

    User->>Arg: argument(graphql_type=BigInt)
    Arg->>SAA: Create with graphql_type
    Note over SAA: Stores graphql_type attribute
    
    User->>SA: Define resolver with Annotated[int, argument]
    SA->>SA: Process Annotated args
    alt graphql_type is not None
        SA->>SA: Override type_annotation with graphql_type
    end
    SA->>Schema: Expose argument with overridden type
    Note over Schema: Argument shows as BigInt! instead of Int!
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Comments (1)

  1. RELEASE.md, line 30 (link)

    syntax: Schema output doesn't match the example code. The method is def a(self, a: ...) but the schema shows name(arg: BigInt!). Should be:

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@codecov
Copy link

codecov bot commented Dec 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.41%. Comparing base (23f8c50) to head (29218b0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4067      +/-   ##
==========================================
- Coverage   94.42%   94.41%   -0.01%     
==========================================
  Files         537      537              
  Lines       35077    35092      +15     
  Branches     1842     1843       +1     
==========================================
+ Hits        33120    33133      +13     
- Misses       1659     1661       +2     
  Partials      298      298              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@thearchitector thearchitector marked this pull request as draft December 5, 2025 16:07
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 5, 2025

CodSpeed Performance Report

Merging #4067 will not alter performance

Comparing thearchitector:elias/arg-type-override (29218b0) with main (23f8c50)

Summary

✅ 28 untouched

@thearchitector thearchitector marked this pull request as ready for review December 5, 2025 18:09
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants