-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathariadne_example.py
More file actions
57 lines (38 loc) · 1.3 KB
/
ariadne_example.py
File metadata and controls
57 lines (38 loc) · 1.3 KB
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
# vim: set expandtab ts=4 sw=4 filetype=python fileencoding=utf8:
import os
import textwrap
import psycopg2
from ariadne import load_schema_from_path
from ariadne import ObjectType
from ariadne.wsgi import GraphQL
from ariadne import make_executable_schema
query = ObjectType("Query")
@query.field("demo")
def resolve_demo_query(_, info):
return dict(
current_timestamp="placeholder current_timestamp...",
bogus_field_a="AAA",
bogus_field_b="BBB")
@query.field("current_timestamp")
def resolve_current_timestamp(_, info):
pgconn = info.context["pgconn"]
cursor = pgconn.cursor()
cursor.execute("select current_timestamp")
return cursor.fetchone()[0]
@query.field("upcoming_shows")
def resolve_upcoming_shows(_, info):
def build_app():
if "PGCONN" not in os.environ:
raise Exception("Sorry, I need a connection string!")
else:
pgconn = psycopg2.connect(os.environ["PGCONN"])
schema = make_executable_schema(
load_schema_from_path("bogus_schema.graphql"),
query)
app = GraphQL(schema, debug=True,
context_value=dict(pgconn=pgconn))
return app
if __name__ == "__main__":
print(textwrap.dedent("""Do this:
gunicorn --reload "ariadne_example:build_app()"
"""))