Let's go straight to the point, here you can see a very simple input schema content and its corresponding output:
There are some more complex examples available under tests if you are curious about how accurate is this tool.
While I was trying out Ariadne (a library for implementing GraphQL servers using schema-first approach) and writing the resolvers for the queries and mutations that I defined in my GraphQL schema, I was missing the ability to define the types for the params and the return values. I hope this library helps some devs to write better typed resolvers for their projects, while keeping the resolvers code synced with the schema definition.
You can easily install it via pip:
$ pip install gql_schema_codegen
Three use cases depending on where the GraphQL schema is defined:
This is the simplest case, you have just a single file with all the types declared there.
$ python -m gql_schema_codegen -p ./schema.graphql -t ./schema_types.py
You have deployed a GraphQL server with introspection enabled, you can just provide a link to that server and this tool will do its job.
$ python -m gql_schema_codegen -u https://gitlab.com/api/graphql -t ./schema_types.py
⚠️ For now, it only works with public schemas, without any authentication required. I will be adding support for this soon.
It's a common thing that you don't have your schema definition centralized in a single file but in multiple ones instead, if that's your case you can provide that directory path where all the graphql/gql files are located and this tool will find them and merge them into a single schema to process it later on.
$ python -m gql_schema_codegen -p ./dir_with_gql_types -t ./schema_types.py
If you just need more info about how to run it:
$ python -m gql_schema_codegen --help
usage: __main__.py [-h] [--schema-path SCHEMA_PATH] [--schema-url SCHEMA_URL] [--to-path TO_PATH] [--config-file CONFIG_FILE]
Generate python file with types from a GraphQL schema file.
optional arguments:
-h, --help show this help message and exit
--schema-path SCHEMA_PATH, -p SCHEMA_PATH
path of the schema file (default: schema.graphql)
--schema-url SCHEMA_URL, -u SCHEMA_URL
url of the schema
--to-path TO_PATH, -t TO_PATH
wanted output file path (default: schema_types.py)
--config-file CONFIG_FILE, -c CONFIG_FILE
path of the config file in yaml format (default: gql_schema_codegen.config.yml)
By default, scalars found in your schema will be generated as Any
, if you already know the types for these scalars you can create a custom config file and define these types there:
scalars:
DateTime: str
Time: str
BigInt: int
BoardID: int
Duration: DesignFields
Notice how you can also set another type from your schema (or a Python class) as custom value for your scalar.
They are simple snapshot tests, they work by comparing the output with the expected output, each test corresponds to a file stored under tests/snapshots
.
You will find different tests declared in the tests
directory.
If you want to run them you need to install the pytest-snapshot
module first. Then, you can run them with:
$ pytest --snapshot-update
You can also run a specific test this way:
$ pytest --snapshot-update -k "test_pokeapi_schema_snapshot"
Feel free to open some issues and/or pull requests if you want to participate in the development of this module, either just by proposing changes or by actively participating with code. At the moment of writing this, I have at least a couple of ideas planned to improve the way this generator works. ✨
If you like the work I do and want to support me, you can do it below:
MIT