-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
252 lines (231 loc) · 9.3 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
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
import strawberry
import socket
from typing import Optional, List
from neo4j import GraphDatabase
neo4j_host = socket.gethostbyname('graphql-test-neo4j')
driver = GraphDatabase.driver(f"bolt://{neo4j_host}:7687", auth=("neo4j", "terriblyinsecure"))
# ---------------------------------------------------------------------------------------------------------------
# Data Classes
# ---------------------------------------------------------------------------------------------------------------
# Define our dataclasses for Person and Movie nodes. All existing node properties are present as graphQL fields.
# Additional fields are lazily constructed when called, based on relationships present in the graph.
# Relationships have only been utilised for Person nodes in this example to simplify things.
@strawberry.type
class Movie:
title: str
released: Optional[int]
tagline: Optional[str]
# Lazily evaluated fields and their resolvers
# Find actors appearing in the movie
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
actors: List['Person'] = strawberry.field()
@strawberry.field
async def actors(self) -> List['Person']:
with driver.session() as session:
query = """
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE toLower(m.title) = toLower($title)
RETURN p.name AS name, p.born as born
"""
result = session.run(query, title=self.title)
people = [
Person(
born=record["born"],
name=record["name"]
)
for record in result
]
return people
# ---------------------------------------------------------------------------------------------------
@strawberry.type
class Person:
name: str
born: Optional[int]
# Lazily evaluated fields and their resolvers
# Link person to movies they acted in
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
acted_in: List[Movie] = strawberry.field()
@strawberry.field
async def acted_in(self) -> List[Movie]:
with driver.session() as session:
query = """
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE toLower(p.name) = toLower($name)
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query, name=self.name)
movies = [
Movie(
title=record["title"],
released=record["released"],
tagline=record["tagline"]
)
for record in result
]
return movies
# Link person to movies they directed
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
directed: List[Movie] = strawberry.field()
@strawberry.field
async def directed(self) -> List[Movie]:
with driver.session() as session:
query = """
MATCH (m:Movie)<-[:DIRECTED]-(p:Person)
WHERE toLower(p.name) = toLower($name)
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query, name=self.name)
movies = [
Movie(
title=record["title"],
released=record["released"],
tagline=record["tagline"]
)
for record in result
]
return movies
# Link person to movies they produced
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
produced: List[Movie] = strawberry.field()
@strawberry.field
async def produced(self) -> List[Movie]:
with driver.session() as session:
query = """
MATCH (m:Movie)<-[:PRODUCED]-(p:Person)
WHERE toLower(p.name) = toLower($name)
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query, name=self.name)
movies = [
Movie(
title=record["title"],
released=record["released"],
tagline=record["tagline"]
)
for record in result
]
return movies
# Match persond to movies they wrote
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
wrote: List[Movie] = strawberry.field()
@strawberry.field
async def wrote(self) -> List[Movie]:
with driver.session() as session:
query = """
MATCH (m:Movie)<-[:WROTE]-(p:Person)
WHERE toLower(p.name) = toLower($name)
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query, name=self.name)
movies = [
Movie(
title=record["title"],
released=record["released"],
tagline=record["tagline"]
)
for record in result
]
return movies
# Link person to movies they reviewed
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
reviewed: List[Movie] = strawberry.field()
@strawberry.field
async def reviewed(self) -> List[Movie]:
with driver.session() as session:
query = """
MATCH (m:Movie)<-[:REVIEWED]-(p:Person)
WHERE toLower(p.name) = toLower($name)
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query, name=self.name)
movies = [
Movie(
title=record["title"],
released=record["released"],
tagline=record["tagline"]
)
for record in result
]
return movies
# Link perston to people they follow
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
follows: List['Person'] = strawberry.field()
@strawberry.field
async def follows(self) -> List['Person']:
with driver.session() as session:
query = """
MATCH (p2:Person)<-[:FOLLOWS]-(p:Person)
WHERE toLower(p.name) = toLower($name)
RETURN p2.born AS born, p2.name AS name
"""
result = session.run(query, name=self.name)
people = [
Person(
born=record["born"],
name=record["name"]
)
for record in result
]
return people
# ---------------------------------------------------------------------------------------------------
# QUERIES
# ---------------------------------------------------------------------------------------------------
# Queries available to user to return Person and Movie information
@strawberry.type
class Query:
@strawberry.field
def people(self, names: Optional[List[str]]) -> List[Person]:
with driver.session() as session:
# If a list of names was provided by the user, filter on them. If not, return all Person nodes.
if not names:
query = """
MATCH (p:Person)
RETURN p.born AS born, p.name AS name
"""
result = session.run(query)
else:
names = [x.lower() for x in names]
query = """
UNWIND $names as val
MATCH (p:Person)
WHERE toLower(p.name) = val
RETURN p.born AS born, p.name AS name
"""
result = session.run(query, names=names)
people = [
Person(
born=record["born"],
name=record["name"]
)
for record in result
]
return people
@strawberry.field
def movies(self, titles: Optional[List[str]]) -> List[Movie]:
with driver.session() as session:
# If a list of titles was provided by the user, filter on them. If not, return all Movie nodes.
if not titles:
query = """
MATCH (m:Movie)
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query)
else:
titles = [x.lower() for x in titles]
query = """
UNWIND $titles as val
MATCH (m:Movie)
WHERE toLower(m.title) = val
RETURN m.released AS released, m.title AS title, m.tagline AS tagline
"""
result = session.run(query, titles=titles)
movies = [
Movie(
title=record["title"],
released=record["released"],
tagline=record["tagline"]
)
for record in result
]
return movies
schema = strawberry.Schema(query=Query)