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,32 +8,44 @@ 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()
buffer = BytesIO()
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)
# get all incoming data and parses as json async def graph_ql(res, body, context_value):
body = await res.get_json() query = body["query"]
variables = body.get("variables", None)
root_value = body.get("rootValue", None)
operation_name = body.get("operationName", None)
query = body["query"] data = await schema.execute(
variables = body.get("variables", None) query,
root_value = body.get("root_value", None) variables,
operation_name = body.get("operation_name", None) context_value,
root_value,
operation_name,
)
data = await schema.execute( res.cork_send(
query, {
variables, "data": (data.data),
context_value, **({"errors": data.errors} if data.errors else {}),
root_value, **({"extensions": data.extensions} if data.extensions else {}),
operation_name, }
) )
res.cork_end(
{
"data": (data.data),
**({"errors": data.errors} if data.errors else {}),
**({"extensions": data.extensions} if data.extensions else {}),
}
)
return post return post