-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_uses.py
323 lines (272 loc) · 9.45 KB
/
complex_uses.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import os
from typing import Any
import graphene
from graphql import (
GraphQLArgument,
GraphQLBoolean,
GraphQLInt,
GraphQLNonNull,
GraphQLString,
)
from graphene_directives import (
CustomDirective,
DirectiveLocation,
Schema,
SchemaDirective,
build_schema,
directive,
)
curr_dir = os.path.dirname(os.path.realpath(__file__))
def input_transform(inputs: dict, _schema: Schema) -> dict:
"""
def input_transform (inputs: Any, schema: Schema) -> dict,
"""
if inputs.get("max_age") > 200:
inputs["swr"] = 30
return inputs
def validate_non_field_input(_type: Any, inputs: dict, _schema: Schema) -> bool:
"""
def validator (type_: graphene type, inputs: Any, schema: Schema) -> bool,
if validator returns False, library raises DirectiveCustomValidationError
"""
if inputs.get("max_age") > 2500:
return False
return True
def validate_field_input(
_parent_type: Any, _field_type: Any, inputs: dict, _schema: Schema
) -> bool:
"""
def validator (parent_type_: graphene_type, field_type_: graphene type, inputs: Any, schema: Schema) -> bool,
if validator returns False, library raises DirectiveCustomValidationError
"""
if inputs.get("max_age") > 2500:
return False
return True
CacheDirective = CustomDirective(
name="cache",
locations=[
DirectiveLocation.OBJECT,
DirectiveLocation.INTERFACE,
DirectiveLocation.ENUM,
DirectiveLocation.UNION,
DirectiveLocation.INPUT_OBJECT,
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.INPUT_FIELD_DEFINITION,
DirectiveLocation.SCALAR,
],
args={
"max_age": GraphQLArgument(
GraphQLNonNull(GraphQLInt),
description="Specifies the maximum age for cache in seconds.",
),
"swr": GraphQLArgument(
GraphQLInt, description="Stale-while-revalidate value in seconds. Optional."
),
"scope": GraphQLArgument(
GraphQLString, description="Scope of the cache. Optional."
),
},
description="Caching directive to control cache behavior of fields or fragments.",
non_field_validator=validate_non_field_input,
field_validator=validate_field_input,
input_transform=input_transform,
)
AuthenticatedDirective = CustomDirective(
name="authenticated",
locations=[
DirectiveLocation.OBJECT,
DirectiveLocation.INTERFACE,
DirectiveLocation.ENUM,
DirectiveLocation.ENUM_VALUE,
DirectiveLocation.UNION,
DirectiveLocation.INPUT_OBJECT,
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.SCALAR,
DirectiveLocation.ARGUMENT_DEFINITION,
],
args={
"required": GraphQLArgument(
GraphQLNonNull(GraphQLBoolean), description="Auth required"
)
},
description="Auth directive to control authorization behavior.",
)
# This will prevent @key definition from being added to the schema, but decorator will work
KeyDirective = CustomDirective(
name="key",
locations=[DirectiveLocation.OBJECT, DirectiveLocation.INTERFACE],
description="Declaring an entity",
add_definition_to_schema=False,
)
# No argument directive
InternalDirective = CustomDirective(
name="internal",
locations=[
DirectiveLocation.OBJECT,
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.ARGUMENT_DEFINITION,
],
description="Directive to indicate this is a internal field.",
)
# A Repeatable directive
RepeatableDirective = CustomDirective(
name="repeatable_directive",
locations=[DirectiveLocation.OBJECT, DirectiveLocation.FIELD_DEFINITION],
description="A repeatable directive.",
args={
"service_name": GraphQLArgument(
GraphQLNonNull(GraphQLString), description="Service Name required"
)
},
is_repeatable=True,
)
# A Schema directive
ComposeDirective = CustomDirective(
name="compose",
locations=[DirectiveLocation.SCHEMA],
description="A schema directive.",
args={
"directive_name": GraphQLArgument(
GraphQLNonNull(GraphQLString), description="Directive Name required"
)
},
is_repeatable=True,
)
@directive(target_directive=CacheDirective, max_age=100)
@directive(target_directive=AuthenticatedDirective, required=True)
class Animal(graphene.Interface): # DirectiveLocation.INTERFACE
age = graphene.Int(required=True)
kind = directive(
target_directive=CacheDirective, field=graphene.Int(required=True), max_age=60
) # DirectiveLocation.FIELD_DEFINITION
@directive(target_directive=CacheDirective, max_age=100)
@directive(target_directive=AuthenticatedDirective, required=True)
class TruthEnum(graphene.Enum): # DirectiveLocation.ENUM
A = 1
B = 2
# Add directives to enum values [DirectiveLocation.ENUM_VALUE]
directive(field=TruthEnum.A, target_directive=AuthenticatedDirective, required=True)
@directive(target_directive=CacheDirective, max_age=100)
class Position(graphene.ObjectType): # DirectiveLocation.OBJECT
x = graphene.Int(required=True)
y = directive(
target_directive=CacheDirective, field=graphene.Int(required=True), max_age=60
)
@directive(target_directive=CacheDirective, max_age=60) # DirectiveLocation.OBJECT
class Human(graphene.ObjectType):
name = graphene.String()
born_in = graphene.String()
@directive(target_directive=CacheDirective, max_age=60)
@directive(target_directive=AuthenticatedDirective, required=True)
class HumanInput(graphene.InputObjectType): # DirectiveLocation.INPUT_OBJECT
born_in = graphene.String()
name = directive( # DirectiveLocation.INPUT_FIELD_DEFINITION
CacheDirective,
field=graphene.String(
description="Test Description", deprecation_reason="Deprecated use born in"
),
max_age=300,
)
@directive(CacheDirective, max_age=200)
class Droid(graphene.ObjectType):
name = directive(
CacheDirective,
field=graphene.String(
description="Test Description", deprecation_reason="Deprecated use born in"
),
max_age=300,
)
primary_function = graphene.String()
@directive(CacheDirective, max_age=200)
class Starship(graphene.ObjectType):
name = graphene.String()
length = directive(
target_directive=CacheDirective,
field=graphene.Int(deprecation_reason="Use another field"),
max_age=60,
)
@directive(target_directive=CacheDirective, max_age=500)
@directive(target_directive=AuthenticatedDirective, required=True)
class SearchResult(graphene.Union): # DirectiveLocation.UNION
class Meta:
types = (Human, Droid, Starship)
@directive(target_directive=CacheDirective, max_age=500)
@directive(target_directive=AuthenticatedDirective, required=True)
class DateNewScalar(graphene.Scalar): # DirectiveLocation.SCALAR
pass
@directive(target_directive=KeyDirective)
@directive(target_directive=InternalDirective)
class Admin(graphene.ObjectType):
name = graphene.String()
password = graphene.String()
@directive(target_directive=AuthenticatedDirective, required=True)
class User(graphene.ObjectType):
name = graphene.String()
password = graphene.String()
price = graphene.Field(
graphene.String,
currency=directive( # DirectiveLocation.ARGUMENT_DEFINITION
InternalDirective, field=graphene.Argument(graphene.Int)
), # Argument
country=directive( # Multiple DirectiveLocation.ARGUMENT_DEFINITION
target_directive=AuthenticatedDirective,
field=directive(InternalDirective, field=graphene.Argument(graphene.Int)),
required=True,
),
)
@directive(target_directive=RepeatableDirective, service_name="ProductService")
@directive(target_directive=RepeatableDirective, service_name="CompanyService")
@directive(target_directive=AuthenticatedDirective, required=True)
class Company(graphene.ObjectType):
established = graphene.Int(required=True)
name = directive(
target_directive=RepeatableDirective,
field=directive(
target_directive=RepeatableDirective,
field=graphene.String(
required=True,
deprecation_reason="This field is deprecated and will be removed in future",
),
service_name="CompanyService Field",
),
service_name="ProductService Field",
)
class Query(graphene.ObjectType):
position = graphene.Field(Position, deprecation_reason="unused field")
schema = build_schema(
query=Query,
types=(
SearchResult,
Animal,
Admin,
HumanInput,
TruthEnum,
DateNewScalar,
User,
Company,
),
directives=(
CacheDirective,
AuthenticatedDirective,
InternalDirective,
KeyDirective,
RepeatableDirective,
),
schema_directives=( # extend schema directives
SchemaDirective(
target_directive=ComposeDirective, arguments={"directive_name": "lowercase"}
),
SchemaDirective(
target_directive=ComposeDirective, arguments={"directive_name": "uppercase"}
),
SchemaDirective(
target_directive=ComposeDirective,
arguments={"directive_name": "pascalcase"},
),
),
)
def generate_schema() -> None:
with open(f"{curr_dir}/complex_uses.graphql", "w") as f:
f.write(str(schema))
if __name__ == "__main__":
generate_schema()