Skip to main content

Generate fully typed GraphQL client from schema, queries and mutations!

Project description

Ariadne

Build Status


Ariadne Code Generator

Python code generator that takes graphql schema, queries and mutations and generates Python package with fully typed and asynchronous GraphQL client.

It's available as ariadne-codegen command and reads configuration from the pyproject.toml file:

$ ariadne-codegen

Features

  • Generate pydantic models from schema types, inputs and enums.
  • Generate pydnatic models for GraphQL results.
  • Generate client package with each GraphQL operation available as async method.

Installation

Ariadne Code Generator can be installed with pip:

$ pip install ariadne-codegen

Configuration

ariadne-codegen reads configuration from [ariadne-codegen] section in your pyproject.toml'.

Required settings:

  • schema_path - path to file/directory with graphql schema
  • queries_path - path to file/directory with queries

Optional settings:

  • target_package_name (defaults to "graphql_client") - name of generated package
  • target_package_path (defaults to cwd) - path where to generate package
  • client_name (defaults to "Client") - name of generated client class
  • client_file_name (defaults to "client") - name of file with generated client class
  • base_client_name (defaults to "AsyncBaseClient") - name of base client class
  • base_client_file_path (defaults to .../graphql_sdk_gen/generators/async_base_client.py) - path to file where base_client_name is defined
  • enums_module_name (defaults to "enums") - name of file with generated enums models
  • input_types_module_name (defaults to "input_types") - name of file with generated input types models
  • include_comments (defaults to true) - a flag that specifies whether to include comments in generated files
  • convert_to_snake_case (defaults to true) - a flag that specifies whether to convert fields and arguments names to snake case
  • async_client (defaults to true) - default generated client is async, change this to option false to generate synchronous client instead
  • files_to_include (defaults to []) - list of files which will be copied into generated package

Extending generated types

Extending models with custom mixins

mixin directive allows to extend class generated for query/mutation field with custom logic. mixin takes two required arguments:

  • from - name of a module to import from
  • import - name of a parent class

Generated class will use import as extra base class, and import will be added to the file.

from {from} import {import}
...
class OperationNameField(BaseModel, {import}):
    ...

This directive can be used along with files_to_include option to extend funcionallity of generated classes.

Example of usage of mixin and files_to_include:

Query with mixin directive:

query listUsers {
    users @mixin(from: ".mixins", import: "UsersMixin") {
        id
    }
}

Part of pyproject.toml with files_to_include (mixins.py contains UsersMixin implementation)

files_to_include = [".../mixins.py"]

Part of generated list_users.py file:

...
from .mixins import UsersMixin
...
class ListUsersUsers(BaseModel, UsersMixin):
    ...

Generated code dependencies

Generated code requires:

  • pydantic
  • httpx (can be avoided by providing another base client class with base_client_file_path and base_client_name parameters)

Example

Example with simple schema and few queries and mutations is available here.

Contributing

We welcome all contributions to Ariadne! If you've found a bug or issue, feel free to use GitHub issues. If you have any questions or feedback, don't hesitate to catch us on GitHub discussions.

Also make sure you follow @AriadneGraphQL on Twitter for latest updates, news and random musings!

Crafted with ❤️ by Mirumee Software hello@mirumee.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ariadne_codegen-0.1.0.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ariadne_codegen-0.1.0-py2.py3-none-any.whl (36.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ariadne_codegen-0.1.0.tar.gz.

File metadata

  • Download URL: ariadne_codegen-0.1.0.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for ariadne_codegen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d55b0c55bf09b936668ee63f8f4fb65185b3e09957a20fa0203f99f8dd4cb583
MD5 1505e954cac22e9afcf7d1f580d89316
BLAKE2b-256 ed5b33c95b75a1ab381003a2fa1bcbac049fc22adb69011e4247b4986b244a1d

See more details on using hashes here.

File details

Details for the file ariadne_codegen-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ariadne_codegen-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fed10120508d5501426037c458af0a97ba4bfec78a3cdcf8b656883a05049c5e
MD5 24b087f6102eb6101c68ef00c42ef65c
BLAKE2b-256 81efa45126c8bb350e06e71b67dc341705df71cdc3ca4759a4eade837c4fd503

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page