-
-
Notifications
You must be signed in to change notification settings - Fork 609
feat: support graphql type override on resolver arguments #4067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: support graphql type override on resolver arguments #4067
Conversation
Reviewer's GuideAdds 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
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if arg.graphql_type is not None: | ||
| self.type_annotation = StrawberryAnnotation( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
Thanks for adding the Here's a preview of the changelog: Adds a 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: |
Greptile OverviewGreptile SummaryThis PR adds support for overriding the GraphQL type on resolver arguments via a new
Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
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!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional Comments (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 showsname(arg: BigInt!). Should be:
5 files reviewed, 1 comment
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
CodSpeed Performance ReportMerging #4067 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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

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
Issues Fixed or Closed by This PR
#3901
Checklist
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:
Enhancements:
Documentation:
Tests:
Chores: