-
Notifications
You must be signed in to change notification settings - Fork 10
/
schema.py
75 lines (50 loc) · 1.82 KB
/
schema.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
from graphene import Int, Interface, Mutation, ObjectType, String
from graphene_federation import FederationVersion, build_schema, key
class TextInterface(Interface):
id = Int(required=True)
body = String(required=True)
@key(fields="id")
class FunnyText(ObjectType):
class Meta:
interfaces = (TextInterface,)
def __resolve_reference(self, info, **kwargs):
return FunnyText(id=self.id, body=f"funny_text_{self.id}")
@key(fields="id")
class FileNode(ObjectType):
id = Int(required=True)
name = String(required=True)
def __resolve_reference(self, info, **kwargs):
# todo test raise exception here
return FileNode(id=self.id, name=f"file_{self.id}")
@key("id")
@key("primary_email")
class User(ObjectType):
id = Int(required=True)
primary_email = String()
age = Int()
def resolve_age(self, info):
return 17
def __resolve_reference(self, info, **kwargs):
if self.id is not None:
return User(id=self.id, primary_email=f"name_{self.id}@gmail.com")
user_id = (
1001
if self.primary_email == "[email protected]"
else hash(self.primary_email) % 10000000
)
return User(id=user_id, primary_email=self.primary_email)
# to test that @key applied only to FileNode, but not to FileNodeAnother
class FileNodeAnother(ObjectType):
id = Int(required=True)
name = String(required=True)
class FunnyMutation(Mutation):
result = String(required=True)
@classmethod
def mutate(cls, root, info, **data):
return FunnyMutation(result="Funny")
class Mutation(ObjectType):
funny_mutation = FunnyMutation.Field()
types = [FileNode, FunnyText, FileNodeAnother, User]
schema = build_schema(
mutation=Mutation, types=types, federation_version=FederationVersion.VERSION_1_0
)