forked from ISA-tools/psql2graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
47 lines (37 loc) · 1.33 KB
/
app.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
from pathlib import Path
from json import loads
from dotenv import dotenv_values
from flask import Flask, request, jsonify
from flask_cors import CORS
from psql2graphql import generate_endpoints
app = Flask(__name__)
CORS(app)
@app.route('/graphql', methods=['POST', 'OPTIONS'])
def graphql():
"""
:example: http://127.0.0.1:5000/graphql
"""
try:
if request.method == 'OPTIONS':
return '', 200
data = schema.execute(loads(request.data)['query'])
if data.errors:
return jsonify({"errors": [error.message for error in data.errors]}), 400
response = jsonify(data.data)
response.headers.add('Access-Control-Allow-Origin', '*')
return response, 200
except Exception as e:
return jsonify({"errors": str(e)}), 500
@app.route('/introspection', methods=['GET'])
def introspection():
try:
response = jsonify(tables)
response.headers.add('Access-Control-Allow-Origin', '*')
return response, 200
except Exception as e:
return jsonify({"errors": str(e)}), 500
dotenv_path = Path('.env')
config = dotenv_values(dotenv_path=dotenv_path)
schema, tables = generate_endpoints(config)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)