denisart / graphql-query

Complete Domain Specific Language (DSL) for GraphQL query in Python.
https://denisart.github.io/graphql-query/
MIT License
56 stars 6 forks source link

Singular union type is not supported for composite type with `graphql_fields` #44

Open kyle-kc opened 1 month ago

kyle-kc commented 1 month ago

Describe the bug When using a singular union type with graphql_fields (i.e. a field with a union type with only one sub-type within the union), the field is rendered only with the fields within the singular type, not using an ... on statement for composite types. This breaks if you're querying a composite type but only want one potential return type.

To Reproduce Steps to reproduce the behavior: Create the following classes:

class ClassA:
    x: int

class ClassB:
    y: Union[ClassA]

Then call ClassB.graphql_fields(). This renders as:

y {
    x
}

Expected behavior It should render as

y {
    ... on ClassA {
        x
    }
}
denisart commented 1 month ago

Yes, it works like that by default. I think that this need fix

Thank for your feedback about this feature! I haven't tested it much.

denisart commented 1 month ago

I understood why it was done this way.

Because Union[ClassA] it is defined as a type ClassA. See https://docs.python.org/3/library/typing.html#typing.Union