File: union.py

package info (click to toggle)
python-graphene 2.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,024 kB
  • sloc: python: 7,295; makefile: 196; sh: 4
file content (75 lines) | stat: -rw-r--r-- 2,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from .base import BaseOptions, BaseType
from .unmountedtype import UnmountedType

# For static type checking with Mypy
MYPY = False
if MYPY:
    from .objecttype import ObjectType  # NOQA
    from typing import Iterable, Type  # NOQA


class UnionOptions(BaseOptions):
    types = ()  # type: Iterable[Type[ObjectType]]


class Union(UnmountedType, BaseType):
    """
    Union Type Definition

    When a field can return one of a heterogeneous set of types, a Union type
    is used to describe what types are possible as well as providing a function
    to determine which type is actually used when the field is resolved.

    The schema in this example can take a search text and return any of the GraphQL object types
    indicated: Human, Droid or Startship.

    Ambigous return types can be resolved on each ObjectType through ``Meta.possible_types``
    attribute or ``is_type_of`` method. Or by implementing ``resolve_type`` class method on the
    Union.

    .. code:: python

        from graphene import Union, ObjectType, List

        class SearchResult(Union):
            class Meta:
                types = (Human, Droid, Starship)

        class Query(ObjectType):
            search = List(SearchResult.Field(
                search_text=String(description='Value to search for'))
            )

    Meta:
        types (Iterable[graphene.ObjectType]): Required. Collection of types that may be returned
            by this Union for the graphQL schema.
        name (optional, str): the name of the GraphQL type (must be unique in schema). Defaults to class
            name.
        description (optional, str): the description of the GraphQL type in the schema. Defaults to class
            docstring.
    """

    @classmethod
    def __init_subclass_with_meta__(cls, types=None, **options):
        assert (
            isinstance(types, (list, tuple)) and len(types) > 0
        ), "Must provide types for Union {name}.".format(name=cls.__name__)

        _meta = UnionOptions(cls)
        _meta.types = types
        super(Union, cls).__init_subclass_with_meta__(_meta=_meta, **options)

    @classmethod
    def get_type(cls):
        """
        This function is called when the unmounted type (Union instance)
        is mounted (as a Field, InputField or Argument)
        """
        return cls

    @classmethod
    def resolve_type(cls, instance, info):
        from .objecttype import ObjectType  # NOQA

        if isinstance(instance, ObjectType):
            return type(instance)