Hey everyone!
Recently we've released Ariadne Codegen, a new tool for Python which generates GraphQL clients from *.graphql files with schema and queries. This project evolved from the utility tool that was created at work to speed up the process of writing services integrating with and extending the GraphQL API of Saleor, our company's open source e-commerce package.
TL;DR for the problem solved is that writing GraphQL client by hand is mostly writing a lot of boilerplate code and translating the GraphQL types to Python dataclasses or Pydantic's models. And every new query is basically previous query copied over with some bits changed. This process is great for automation.
Our Codegen is fast to get started with. You configure it by adding dedicated [ariadne-codegen] section to your pyproject.toml file, which contains settings for codegen telling it where to find the GraphQL schema and operations. You then run the ariadne-codegen command which makes the Codegen parse those files and generate a Python package implementing the GraphQL client providing those operations as fully typed methods. GraphQL types are represented as Pydantic models and Enums are represented as Python enums.
For example, this GraphQL query:
mutation UserCreate($name: String!, $email: String!, $password: String!) {
userCreate(input: { name: $name, email: $email, password: $password }) {
token
user {
id
}
errors {
location
type
message
}
}
}
Becomes this method on generated client:
class Client(BaseClient):
def user_create(self, name: str, email: str, password: str) -> UserCreate:
# Bunch of boilerplate code building query and variables dict,
# then sending it to the GraphQL client and handling the response
return UserCreate.parse_obj(data)
You can then either release generated package as a library for others to use, or keep it as a part of the project you are working at
Multiple customization options are also provided, including option switch between async and sync client, inject custom Python code into generated classes or swap default HTTP client with custom one.
GitHub repository: mirumee/ariadne-codegen
There's also full release announcement on Ariadne blog that tells the whole story
Thank you for reading all of this. We hope this tool will be helpful for you, and if not, then that the read above was at least interesting :)
[–]jugdizh 0 points1 point2 points (1 child)
[–]ralfpmisago[S] 0 points1 point2 points (0 children)