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

Generated GraphQL query is missing fragments #284

Closed
dhay opened this issue Mar 7, 2024 · 1 comment · Fixed by #291
Closed

Generated GraphQL query is missing fragments #284

dhay opened this issue Mar 7, 2024 · 1 comment · Fixed by #291
Assignees
Labels
bug Something isn't working roadmap

Comments

@dhay
Copy link

dhay commented Mar 7, 2024

Similar to this issue, I'm seeing the generated GQL query for my mutation missing the associated fragments.

Given the following schema (sample-schema.graphql)

type Item {
  id: ID
}

type ItemResult {
  contacts: [Item!]
  errors: [ItemServiceError!]
}

interface ItemError {
  message: String!
}

union ItemServiceError = ItemServiceInternalError

type ItemServiceInternalError implements ItemError {
  message: String!
}

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

and the given queries file (sample-queries.graphql):

fragment Item on Item {
    id
}

fragment ItemError on ItemError {
    __typename
    message
}

mutation my_mutation($id: ID!) {
    change_item(id: $id) {
        contacts {
            ...Item
        }
        errors {
            ... on ItemError {
                ...ItemError
            }
        }
    }
}

Using the following config file (sample.toml)

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

When I run the codegen (version 0.13.0) as follows

ariadne-codegen --config sample.toml

The generated code in client.py is missing the ItemError fragment definition:

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

            fragment Item on Item {
              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)
@dhay
Copy link
Author

dhay commented Mar 11, 2024

I'm also noticing that the fields in the ItemError are not being defined on the model. The code generated looks like this:

class MyMutationChangeItemErrorsItemServiceInternalError(BaseModel):
    typename__: Literal["ItemServiceInternalError"] = Field(alias="__typename")

If I remove the ItemError fragment, the GraphQL code is generated correctly, but the message field is still missing from the MyMutationChangeItemErrorsItemServiceInternalError class.

@rafalp rafalp added the bug Something isn't working label Mar 12, 2024
@DamianCzajkowski DamianCzajkowski self-assigned this Mar 27, 2024
@DamianCzajkowski DamianCzajkowski linked a pull request Mar 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants