update graphql helper

pull/75/head
Ciro 2023-03-10 12:36:29 -03:00
rodzic a340dda35b
commit f006601588
1 zmienionych plików z 35 dodań i 23 usunięć

Wyświetl plik

@ -1,6 +1,6 @@
import strawberry import strawberry
import strawberry.utils.graphiql import strawberry.utils.graphiql
from io import BytesIO
def graphiql_from(Query, Mutation=None): def graphiql_from(Query, Mutation=None):
if Mutation: if Mutation:
@ -8,17 +8,29 @@ def graphiql_from(Query, Mutation=None):
else: else:
schema = strawberry.Schema(Query) schema = strawberry.Schema(Query)
async def post(res, req): def post(res, req):
# we can pass whatever we want to context, query, headers or params, cookies etc # we can pass whatever we want to context, query, headers or params, cookies etc
context_value = req.preserve() context_value = req.preserve()
# get all incoming data and parses as json buffer = BytesIO()
body = await res.get_json() def on_data(res, chunk, is_end):
buffer.write(chunk)
if is_end:
try:
body = res.app._json_serializer.loads(buffer.getvalue().decode("utf-8"))
res.run_async(graph_ql(res, body, context_value))
except Exception as err:
res.app.trigger_error(err, res, None)
res.grab_aborted_handler()
res.on_data(on_data)
async def graph_ql(res, body, context_value):
query = body["query"] query = body["query"]
variables = body.get("variables", None) variables = body.get("variables", None)
root_value = body.get("root_value", None) root_value = body.get("rootValue", None)
operation_name = body.get("operation_name", None) operation_name = body.get("operationName", None)
data = await schema.execute( data = await schema.execute(
query, query,
@ -28,7 +40,7 @@ def graphiql_from(Query, Mutation=None):
operation_name, operation_name,
) )
res.cork_end( res.cork_send(
{ {
"data": (data.data), "data": (data.data),
**({"errors": data.errors} if data.errors else {}), **({"errors": data.errors} if data.errors else {}),