It seems strawberry doesn't support default_factory for query arguments. Like in example below:
import json
import typing
from typing import NewType
import strawberry
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
data = [{"name": "A", "age": 12}, {"name": "B", "age": 20}]
@strawberry.type
class Info:
name: str
age: int
JSON = strawberry.scalar(
NewType("JSON", object),
description="The `JSON` scalar type represents JSON values as specified by ECMA-404",
serialize=lambda v: v,
parse_value=lambda v: v,
)
def get_data(
where: typing.Optional[JSON] = strawberry.field(default_factory=dict), #FIXME to None
) -> typing.Optional[typing.List[Info]]:
if where is None: result= data
elif len(where) != 0: result = [d for d in data if d.get("name") == where.get("name")]
else: result = data
return json.loads(json.dumps(result), object_hook=lambda d: Info(**d))
@strawberry.type
class Query:
informations: typing.Optional[typing.List[Info]] = strawberry.field(resolver=get_data)
schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(
schema
)
app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")
Here, I couldn't find a way to set where
argument be dict using default_factory. Using the same code as above result in AST error unless we replace strawberry.field(default_factory=dict)
or {}
by None
.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too