-
-
Notifications
You must be signed in to change notification settings - Fork 603
Description
👋 looking into pairing https://strawberry.rocks/docs/codegen/query-codegen and https://github.com/graphql-python/gql
specifically, I want the client application (another python service) to do everything, rather than "generating clientlibs" and maintaining a pipeline for that. (this approach is similar to web clients)
Here's the current workflow
- download the schema.graphql of the service I want to call from the registry
- use
$ strawberry schema-codegen schema.graphqlto generate aschema.py - use
$ strawberry codegen --schema schema ...to generate operation result files - find and replace to add
@dataclassto all generated classes - use
from dataclass_wizard import fromdictto recursively convert the raw dict response into the dataclasses
...which I think works pretty well!
Here's some improvements I'd love to add to add to strawberry to reduce the amount of logic on our side - let me know if you'd be open to some PRs for this!
-
for
$ strawberry codegen, allow a--sdlflag as a (mutually exclusive) alternative to--schemawhich accepts the schema in SDL format. (Internally, strawberry could still runschema-codegen). This will make it easier for external services and tooling to do codegen, rather than assuming it all happens in the server's codebase. -
$ strawberry codegencurrently uses the basename of the input file, rather than the name of the query, as the name of the output file. This potentially leads to conflicts when run in parallel if there are two files namedget_foo.pybut in different locations. An option to use the query name, or arbitrarily specify would be good. Former pairs well with the common rule of "no duplicated query names" within a codebase. This would let us just xargs into the codegen command. -
use dataclasses (or ability to specify this) rather than raw classes
-
add some
__post_init__logic to allow dict unpacking into a dataclass for nested types. This would remove the need to usefrom dataclass_wizard import fromdict.Something like this(?):
from dataclasses import dataclass @dataclass class ReviewResultUser: name: str @dataclass class ReviewResultReview: id: str author: User def __post_init__(self): if isinstance(self.author, dict): self.author = ReviewResultUser(**self.author) @dataclass class ReviewResult: review: ReviewResultReview def __post_init__(self): if isinstance(self.review, dict): self.review = ReviewResultReview(**self.review) # raw dict response from https://github.com/graphql-python/gql raw_result = {"review": {"id": "123", "author": {"name": "alice"}}} # parsed into dataclasses generated by strawberry result = ReviewResult(**raw_result)
wdyt?