Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom operation generation #296

Merged
merged 12 commits into from
Jun 11, 2024
Merged

Conversation

DamianCzajkowski
Copy link
Contributor

@DamianCzajkowski DamianCzajkowski commented Jun 3, 2024

Pull Request Description

Note:
This pull request will be merged into another branch and deployed as a dev version (experimental). I want to test in some real projects. The code needs a refactor and some optimization.

This pull request introduces custom operation generation in ariadne-codegen. The following features have been implemented and tested:

  • Query generation
  • Mutation Fields
  • Mutation generation
  • Response types (moved to a separate issue)
  • Scalars/Unions as a response
  • Turn off queries need validation when custom generation is on
  • Add selection on which queries/mutations custom operations can be done (to generate fewer classes) (moved to a separate issue)
  • Add more tests
  • Add examples of usage (new tests are the perfect example)

Response Types Generation

I attempted to generate response types using Pydantic classes. However, Pydantic version 2 is significantly slower. In the context of Saleor, which involves linking 81 classes, importing a single class from such a file takes approximately 7 seconds. Due to this performance issue, I am considering switching to regular dataclasses with custom functions for data type validation. This approach aims to improve performance while maintaining flexibility and robustness in type validation.

Feel free to review the changes and provide feedback!

Simple example:

    async with SaleorClient(saleor_url=saleor_url, api_key=None) as saleor_client:
        query_str = [
            Query.products(first=1, channel="default-channel").fields(
                ProductCountableConnectionFields.edges().fields(
                    ProductCountableEdgeFields.node().fields(
                        ProductFields.id,
                        ProductFields.slug,
                        ProductFields.category().fields(CategoryFields.id),
                    )
                ),
            ),
            Query.products(first=10, channel="default-channel").alias("my_products").fields(
                ProductCountableConnectionFields.edges().fields(
                    ProductCountableEdgeFields.node().fields(
                        ProductFields.id,
                        ProductFields.slug,
                    )
                ),
            )
        ]
        result = await saleor_client.query(
            *query_str,
            operation_name="getProducts",
        )
        result_json = json.dumps(result.json())

query_str:

query getProducts { 
    products(channel: "default-channel", first: 1) { 
        edges { 
            node { 
                id 
                slug 
                category { 
                    id 
                } 
            } 
        } 
    } 
    my_products: products(channel: "default-channel", first: 10) { 
        edges { 
            node { 
                id 
                slug 
            } 
        } 
    } 
}

@DamianCzajkowski DamianCzajkowski self-assigned this Jun 3, 2024
@DamianCzajkowski DamianCzajkowski linked an issue Jun 3, 2024 that may be closed by this pull request
@DamianCzajkowski DamianCzajkowski changed the base branch from main to dev June 11, 2024 12:21
@DamianCzajkowski DamianCzajkowski marked this pull request as ready for review June 11, 2024 12:21
@DamianCzajkowski DamianCzajkowski merged commit a82ecb8 into dev Jun 11, 2024
4 checks passed
@DamianCzajkowski DamianCzajkowski deleted the custom-operation-generation branch June 11, 2024 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Allow user-specified queries
1 participant