socketify.py/examples/helpers/graphiql.py

40 wiersze
1.1 KiB
Python
Czysty Zwykły widok Historia

2022-11-15 13:17:09 +00:00
import strawberry
import strawberry.utils.graphiql
2022-11-16 19:28:46 +00:00
2022-11-15 13:30:01 +00:00
def graphiql_from(Query, Mutation=None):
if Mutation:
schema = strawberry.Schema(query=Query, mutation=Mutation)
else:
schema = strawberry.Schema(Query)
2022-11-15 13:17:09 +00:00
async def post(res, req):
# we can pass whatever we want to context, query, headers or params, cookies etc
context_value = req.preserve()
2022-11-15 13:17:09 +00:00
# get all incoming data and parses as json
2022-11-15 13:17:09 +00:00
body = await res.get_json()
query = body["query"]
variables = body.get("variables", None)
root_value = body.get("root_value", None)
operation_name = body.get("operation_name", None)
data = await schema.execute(
query,
variables,
context_value,
root_value,
operation_name,
)
2022-11-16 19:28:46 +00:00
res.cork_end(
{
"data": (data.data),
**({"errors": data.errors} if data.errors else {}),
**({"extensions": data.extensions} if data.extensions else {}),
}
)
return post