The code generator doesn't seem to produce correct Python type annotations.
Given a simple schema in schema.graphql
:
type Query {
name: String
other: Other
}
type Other {
foo: String
}
Running strawberry schema-codegen schema.graphql
generates the following python code:
import strawberry
@strawberry.type
class Query:
name: str | None
other: Other | None # <--- reference to `Other`
@strawberry.type
class Other:
foo: str | None
schema = strawberry.Schema(query=Query)
The class Query
references Other
before it is defined. Running the code makes Python complain:
Traceback (most recent call last):
File ".../schema.py", line 4, in <module>
class Query:
File ".../schema.py", line 6, in Query
other: Other | None
^^^^^
NameError: name 'Other' is not defined. Did you mean: 'iter'?
Possible fixes are:
Other
comes first and thus making the reference valid. This works as long as the types form no cycle.from __future__ import annotations
to the Python code making forward references legal. See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#class-name-forward-referencestyping.Optional["Other"]
(Note the string quotes). Interestingly, "Other" | None
is not valid, so Optional
has to be used here.Of course, for a small schema those issues can be quickly fixed, but for a large schema this isn't really feasible.
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