blb-ventures / strawberry-django-plus

Enhanced Strawberry GraphQL integration with Django
MIT License
179 stars 47 forks source link

Can't make a union of `gql.Connection | OperationMessage` #223

Closed pomidoroshev closed 1 year ago

pomidoroshev commented 1 year ago

I want both of the following queries to return either profiles or a permission error. It works as expected for the profile field, but it doesn't work for the second field, all_profiles:

@gql.type
class ProfilesQuery:
    profile: ProfileType | OperationMessage = gql.django.node(
        directives=[IsAuthenticated()]
    )
    all_profiles: gql.Connection[ProfileType] | OperationMessage = gql.django.connection(
        directives=[IsAuthenticated()]
    )

Query:

query {
  allProfiles {
    ... on ProfileTypeConnection {
      edges {
        node {
          id
        }
      }
    }
    ... on OperationMessage {
      message
    }
  }
}

Response:

{
  "data": null,
  "errors": [
    {
      "message": "'StrawberryUnion' object has no attribute '_type_definition'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "allProfiles"
      ]
    }
  ]
}

Error:

  File "<...>/python3.11/site-packages/strawberry_django_plus/relay.py", line 1001, in get_result
    type_def = info.return_type._type_definition  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'StrawberryUnion' object has no attribute '_type_definition'
pomidoroshev commented 1 year ago

@bellini666 thank you!