sgqlc icon indicating copy to clipboard operation
sgqlc copied to clipboard

Union + __fields__() has misleading error message

Open barbieri opened this issue 5 years ago • 0 comments

While the error is misleading (I should fix that), in GraphQL you can't select fields of an union type directly, you must use fragments to select depending on each type you want to handle.

In your case, could you try:

import sgqlc
from sgqlc.operation import Operation
from sgqlc.types import String, Type, Union, Field, non_null


class TypeA(Type):
    i = int

class TypeB(Type):
    s = str


class TypeU(Union):
    __types__ = (TypeA, TypeB)


class Query(sgqlc.types.Type):
    some_query = Field(non_null(TypeU), graphql_name='someQuery')


op = Operation(Query, name="op_name")
q = op.some_query()
q.__fields__()  # this line throws 'AttributeError: TypeA has no field name'

# correct behavior would be to:
q.__as__(TypeA).i()

Originally posted by @barbieri in https://github.com/profusion/sgqlc/issues/71#issuecomment-555237354

barbieri avatar Nov 19 '19 16:11 barbieri