/
enum.py
122 lines (96 loc) 路 2.91 KB
/
enum.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from __future__ import annotations
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterable,
List,
Optional,
Union,
overload,
)
from strawberry.enum import _process_enum
from strawberry.enum import enum_value as base_enum_value
if TYPE_CHECKING:
from strawberry.enum import EnumType, EnumValueDefinition
def enum_value(
value: Any,
deprecation_reason: Optional[str] = None,
directives: Iterable[object] = (),
inaccessible: bool = False,
tags: Iterable[str] = (),
) -> EnumValueDefinition:
from strawberry.federation.schema_directives import Inaccessible, Tag
directives = list(directives)
if inaccessible:
directives.append(Inaccessible())
if tags:
directives.extend(Tag(name=tag) for tag in tags)
return base_enum_value(value, deprecation_reason, directives)
@overload
def enum(
_cls: EnumType,
*,
name: Optional[str] = None,
description: Optional[str] = None,
directives: Iterable[object] = (),
authenticated: bool = False,
inaccessible: bool = False,
policy: Optional[List[List[str]]] = None,
requires_scopes: Optional[List[List[str]]] = None,
tags: Optional[Iterable[str]] = (),
) -> EnumType:
...
@overload
def enum(
_cls: None = None,
*,
name: Optional[str] = None,
description: Optional[str] = None,
directives: Iterable[object] = (),
authenticated: bool = False,
inaccessible: bool = False,
policy: Optional[List[List[str]]] = None,
requires_scopes: Optional[List[List[str]]] = None,
tags: Optional[Iterable[str]] = (),
) -> Callable[[EnumType], EnumType]:
...
def enum(
_cls: Optional[EnumType] = None,
*,
name=None,
description=None,
directives=(),
authenticated: bool = False,
inaccessible: bool = False,
policy: Optional[List[List[str]]] = None,
requires_scopes: Optional[List[List[str]]] = None,
tags: Optional[Iterable[str]] = (),
) -> Union[EnumType, Callable[[EnumType], EnumType]]:
"""Registers the enum in the GraphQL type system.
If name is passed, the name of the GraphQL type will be
the value passed of name instead of the Enum class name.
"""
from strawberry.federation.schema_directives import (
Authenticated,
Inaccessible,
Policy,
RequiresScopes,
Tag,
)
directives = list(directives)
if authenticated:
directives.append(Authenticated())
if inaccessible:
directives.append(Inaccessible())
if policy:
directives.append(Policy(policies=policy))
if requires_scopes:
directives.append(RequiresScopes(scopes=requires_scopes))
if tags:
directives.extend(Tag(name=tag) for tag in tags)
def wrap(cls: EnumType) -> EnumType:
return _process_enum(cls, name, description, directives=directives)
if not _cls:
return wrap
return wrap(_cls) # pragma: no cover