Strawberry supports code generation for GraphQL queries.
Note
Schema codegen will be supported in future releases. We are testing the query
codegen in order to come up with a nice API.
Letβs assume we have the following GraphQL schema built with Strawberry:
and we want to generate types based on the following query:
With the following command:
Weβll get the following output inside output/query.py :
Why is this useful?
Query code generation is usually used to generate types for clients using your
GraphQL APIs.
Tools like GraphQL Codegen exist in
order to create types and code for your clients. Strawberryβs codegen feature
aims to address the similar problem without needing to install a separate tool.
Plugin system
Strawberryβs codegen supports plugins, in the example above for example, we are
using the python plugin. To pass more plugins to the codegen tool, you can use
the -p flag, for example:
the plugin can be specified as a python path.
Custom plugins
The interface for plugins looks like this:
on_start is called before the codegen starts.
on_end is called after the codegen ends and it receives the result of the
codegen. You can use this to format code, or add licenses to files and so on.
generated_code is called when the codegen starts and it receives the types
and the operation. You cans use this to generate code for each type and
operation.
Console plugin
There is also a plugin that helps to orchestrate the codegen process and notify
the user about what the current codegen process is doing.