Skip to content

Commit 23af996

Browse files
committed
Implemented graphql request and response wrapper.
1 parent 8d04e53 commit 23af996

File tree

1 file changed

+36
-0
lines changed
  • examples/nameko_sqlalchemy

1 file changed

+36
-0
lines changed

examples/nameko_sqlalchemy/app.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from database import db_session, init_db
2+
from schema import schema
3+
4+
from graphql_server import (HttpQueryError, default_format_error,
5+
encode_execution_results, json_encode,load_json_body, run_http_query)
6+
7+
8+
class App():
9+
def __init__(self):
10+
init_db()
11+
12+
def query(self, request):
13+
data = self.parse_body(request)
14+
execution_results, params = run_http_query(
15+
schema,
16+
'post',
17+
data)
18+
result, status_code = encode_execution_results(
19+
execution_results,
20+
format_error=default_format_error,is_batch=False, encode=json_encode)
21+
return result
22+
23+
def parse_body(self,request):
24+
# We use mimetype here since we don't need the other
25+
# information provided by content_type
26+
content_type = request.mimetype
27+
if content_type == 'application/graphql':
28+
return {'query': request.data.decode('utf8')}
29+
30+
elif content_type == 'application/json':
31+
return load_json_body(request.data.decode('utf8'))
32+
33+
elif content_type in ('application/x-www-form-urlencoded', 'multipart/form-data'):
34+
return request.form
35+
36+
return {}

0 commit comments

Comments
 (0)