mirumee / ariadne-codegen

Generate fully typed Python client for any GraphQL API from schema, queries and mutations
BSD 3-Clause "New" or "Revised" License
255 stars 31 forks source link

Mixin not stripped from Fragments (take 2) #297

Open dhay opened 3 months ago

dhay commented 3 months ago

I'm still seeing issues when adding a @mixin to a fragment. I saw https://github.com/mirumee/ariadne-codegen/issues/176, but the issue persists. In my case, I'm trying to add the mixin to the fragment itself, not a field within the fragment.

sample-schema.graphql

type Item {
    id: ID
}

type Mutation {
    change_item(id: ID!): Item
}

sample-queries.graphql

fragment Item on Item @mixin(from: "sample_mixin", import: "ItemMixin") {
    id
}
mutation my_mutation($id: ID!) {
    change_item(id: $id) {
        ...Item
    }
}

sample_mixin.py

class ItemMixin:
    pass

sample.toml

[tool.ariadne-codegen]
schema_path = "sample-schema.graphql"
queries_path = "sample-queries.graphql"

Running ariadne-codegen --config sample.toml results in a client.py that looks like this:

class Client(AsyncBaseClient):
    async def my_mutation(self, id: str, **kwargs: Any) -> MyMutation:
        query = gql(
            """
            mutation my_mutation($id: ID!) {
              change_item(id: $id) {
                ...Item
              }
            }

            fragment Item on Item @mixin(from: "sample_mixin", import: "ItemMixin") {
              id
            }
            """
        )
        variables: Dict[str, object] = {"id": id}
        response = await self.execute(
            query=query, operation_name="my_mutation", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return MyMutation.model_validate(data)
$ pip list | grep ariadne
ariadne-codegen           0.13.0
jukiewiczm commented 2 months ago

I confirm this doesn't work currently. Seems like the PR in the other issue addresses only the part when the mixin is defined in a field, not on the entire fragment.