-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
executable file
·146 lines (120 loc) · 3.91 KB
/
hello.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
#!/usr/bin/env python
import json
import graphene
from collections import OrderedDict
from graphene.types.resolver import dict_resolver, attr_resolver
from graphene.types.generic import GenericScalar
from google.protobuf.json_format import MessageToDict
import hello_pb2
def dict_or_attr_resolver(attname, default_value, root, info, **args):
resolver = attr_resolver
if isinstance(root, dict):
resolver = dict_resolver
return resolver(attname, default_value, root, info, **args)
class Episode(graphene.Enum):
NEWHOPE = 4
EMPIRE = 5
JEDI = 6
@property
def description(self):
if self == Episode.NEWHOPE:
return 'New Hope Episode'
return 'Other episode'
class Starship(graphene.ObjectType):
name = graphene.String()
length = graphene.Int()
class Character(graphene.Interface):
id = graphene.ID(required=True)
name = graphene.String(required=True)
friends = graphene.List(lambda: Character)
type = graphene.String()
@classmethod
def resolve_type(cls, instance, info):
if instance.type == 'DROID':
return Droid
return Human
class Human(graphene.ObjectType):
class Meta:
interfaces = (Character, )
starships = graphene.List(Starship)
home_planet = graphene.String()
class Droid(graphene.ObjectType):
class Meta:
interfaces = (Character, )
primary_function = graphene.String()
class SearchResult(graphene.Union):
class Meta:
types = (Human, Droid, Starship)
class CreatePerson(graphene.Mutation):
class Arguments:
name = graphene.String()
ok = graphene.Boolean()
person = graphene.Field(lambda: Person)
def mutate(self, info, name):
person = hello_pb2.Person()
person.name = name
person.details['item1'] = "some string1"
person.details['item2'] = "some string2"
person.details['item3'] = "some string3"
print(OrderedDict(person.details))
for key, val in person.details.items():
print(key, val)
ok = True
return CreatePerson(person=MessageToDict(person,
including_default_value_fields=True), ok=ok)
class Person(graphene.ObjectType):
class Meta:
default_resolver = dict_resolver
name = graphene.String()
age = graphene.Int()
details = graphene.Field(GenericScalar)
class MyMutations(graphene.ObjectType):
create_person = CreatePerson.Field()
class Query(graphene.ObjectType):
person = graphene.Field(Person)
hello = graphene.String(name=graphene.String(default_value="stranger"))
hero = graphene.Field(
Character,
required=True,
episode=graphene.Int(required=True)
)
def resolve_hello(self, info, name):
return 'Hello ' + name
def resolve_hero(_, info, episode):
# Luke is the hero of Episode V
if episode == 5:
return hello_pb2.Human(name='Luke Skywalker')
return hello_pb2.Droid(name='R2-D2', type='DROID')
schema = graphene.Schema(query=Query, mutation=MyMutations, types=[Human, Droid])
if __name__ == '__main__':
#print(get_human(name='Luke Skywalker'))
result = schema.execute("""{ hello }""")
result2 = schema.execute("""query HeroForEpisode($episode: Int = 5){
hero(episode: $episode){
__typename
name
... on Droid{
primaryFunction
}
... on Human{
homePlanet
}
}
}
""", variable_values={'episode': 4})
result3 = schema.execute("""mutation myFirstMutation{
createPerson(name:"Peter") {
person{
name
details
}
ok
}
}
""")
print(result.data['hello'])
print(json.dumps(result2.data))
print(json.dumps(result2.errors))
print(dict(result3.data))
print(json.dumps(result3.data))
print(json.dumps(result3.errors))