Skip to content

Support graphql_type for strawberry.argument #3901

Open
@thearchitector

Description

@thearchitector

Resolvers support different return types via the graphql_type argument to strawberry.field. This is super useful when the GraphQL semantics deal in one scalar, but the python semantics deal in another (for static typing purposes or otherwise).

If I have a custom scalar that (de)serializes to a built-in type, for ex., i can decorate the resolver with the built-in but tell GQL to use the custom scalar. Like so:

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

@strawberry.field(graphql_type=BigInt)
def blah(info) -> int:
   return 1238423948234

I cannot do the same for arguments to custom resovlers, though, which causes some static typing inconveniences. For ex, Pyright complains with the following:

def some_num(a: list[int]) -> str:
   return "hi".join(a)


@strawberry.field
def blah(info, nums: list[BigInt]) -> str:
    return some_num(nums)

In the same manner as the returned type, it would be useful to be able to define the type for the argument:

@strawberry.field
def blah(
    info,
    nums: Annotated[list[int], strawberry.argument(graphql_type=list[BigInt])],
) -> str:
    return some_num(nums)

Feature Request Type

  • Core functionality
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions