-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
76 lines (61 loc) · 2.06 KB
/
server.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
#!/usr/bin/python3
__author__ = "Martin Riedl"
__copyright__ = "Copyright 2020"
__credits__ = [
"coreGreenberet (https://github.com/coreGreenberet)",
"hahn-th (https://github.com/hahn-th/homematicip-rest-api)",
"Seth Corker (https://blog.sethcorker.com/how-to-create-a-react-flask-graphql-project)"
]
__license__ = "GPL"
__maintainer__ = "Martin Riedl"
from flask import Flask, request, jsonify
from ariadne import graphql_sync, make_executable_schema, gql, load_schema_from_path
from ariadne.explorer import ExplorerGraphiQL
from model import query
type_defs = gql(load_schema_from_path("./schema.graphql"))
schema = make_executable_schema(type_defs, query)
explorer_html = ExplorerGraphiQL().html(None)
app = Flask(__name__)
# Switch caching off
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route("/graphql", methods=["GET"])
def graphql_playground():
"""Serve GraphQL playground"""
if 'query' in request.args:
# serve query
success, result = graphql_sync(
schema,
{
"query" : request.args['query']
}
)
status_code = 200 if success else 400
return jsonify(result), status_code
else:
# only serve playground if no query argument is given ...
return explorer_html, 200
@app.route("/graphql", methods=["POST"])
def graphql_server():
data = request.get_json()
#print(data)
success, result = graphql_sync(
schema,
data,
context_value=request,
debug=app.debug
)
status_code = 200 if success else 400
return jsonify(result), status_code
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=8191)