kopia lustrzana https://github.com/cirospaciari/socketify.py
add graphiql helper
rodzic
0ed62ed134
commit
e000162277
|
@ -5,6 +5,7 @@ import strawberry.utils.graphiql
|
||||||
|
|
||||||
from socketify import App
|
from socketify import App
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
from helpers.graphiql import graphiql_from
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class User:
|
class User:
|
||||||
|
@ -17,41 +18,8 @@ class Query:
|
||||||
return User(name="Hello")
|
return User(name="Hello")
|
||||||
|
|
||||||
|
|
||||||
schema = strawberry.Schema(Query)
|
|
||||||
|
|
||||||
|
|
||||||
async def graphiql_post(res, req):
|
|
||||||
# we can pass whatever we want to context, query, headers or params, cookies etc
|
|
||||||
context_value = {
|
|
||||||
"query": req.get_queries(),
|
|
||||||
"headers": req.get_headers(),
|
|
||||||
"params": req.get_parameters()
|
|
||||||
}
|
|
||||||
|
|
||||||
# get all incomming data and parses as json
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
res.cork_end({
|
|
||||||
"data": ( data.data ),
|
|
||||||
**({"errors": data.errors} if data.errors else {}),
|
|
||||||
**({"extensions": data.extensions} if data.extensions else {})
|
|
||||||
})
|
|
||||||
|
|
||||||
app = App()
|
app = App()
|
||||||
app.get("/", lambda res, req: res.end(strawberry.utils.graphiql.get_graphiql_html()))
|
app.get("/", lambda res, req: res.end(strawberry.utils.graphiql.get_graphiql_html()))
|
||||||
app.post("/", graphiql_post)
|
app.post("/", graphiql_from(Query))
|
||||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||||
app.run()
|
app.run()
|
|
@ -0,0 +1,57 @@
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
import strawberry
|
||||||
|
import strawberry.utils.graphiql
|
||||||
|
|
||||||
|
from socketify import App
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
|
class User:
|
||||||
|
name: str
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
|
class Query:
|
||||||
|
@strawberry.field
|
||||||
|
def user(self) -> Optional[User]:
|
||||||
|
return User(name="Hello")
|
||||||
|
|
||||||
|
|
||||||
|
schema = strawberry.Schema(Query)
|
||||||
|
|
||||||
|
|
||||||
|
async def graphiql_post(res, req):
|
||||||
|
# we can pass whatever we want to context, query, headers or params, cookies etc
|
||||||
|
context_value = {
|
||||||
|
"query": req.get_queries(),
|
||||||
|
"headers": req.get_headers(),
|
||||||
|
"params": req.get_parameters()
|
||||||
|
}
|
||||||
|
|
||||||
|
# get all incomming data and parses as json
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
res.cork_end({
|
||||||
|
"data": ( data.data ),
|
||||||
|
**({"errors": data.errors} if data.errors else {}),
|
||||||
|
**({"extensions": data.extensions} if data.extensions else {})
|
||||||
|
})
|
||||||
|
|
||||||
|
app = App()
|
||||||
|
app.get("/", lambda res, req: res.end(strawberry.utils.graphiql.get_graphiql_html()))
|
||||||
|
app.post("/", graphiql_post)
|
||||||
|
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||||
|
app.run()
|
|
@ -0,0 +1,36 @@
|
||||||
|
import strawberry
|
||||||
|
import strawberry.utils.graphiql
|
||||||
|
|
||||||
|
def graphiql_from(Query):
|
||||||
|
schema = strawberry.Schema(Query)
|
||||||
|
|
||||||
|
async def post(res, req):
|
||||||
|
# we can pass whatever we want to context, query, headers or params, cookies etc
|
||||||
|
context_value = {
|
||||||
|
"query": req.get_queries(),
|
||||||
|
"headers": req.get_headers(),
|
||||||
|
"params": req.get_parameters()
|
||||||
|
}
|
||||||
|
|
||||||
|
# get all incomming data and parses as json
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
res.cork_end({
|
||||||
|
"data": ( data.data ),
|
||||||
|
**({"errors": data.errors} if data.errors else {}),
|
||||||
|
**({"extensions": data.extensions} if data.extensions else {})
|
||||||
|
})
|
||||||
|
return post
|
|
@ -114,7 +114,16 @@ class Loop:
|
||||||
|
|
||||||
|
|
||||||
#see ./native/uv_selector.txt
|
#see ./native/uv_selector.txt
|
||||||
|
# will only work on linux and macos
|
||||||
# class UVSelector(asyncio.SelectorEventLoop):
|
# class UVSelector(asyncio.SelectorEventLoop):
|
||||||
|
# def register(self, fileobj, events, data=None):
|
||||||
|
# fd = fileobj.fileno()
|
||||||
|
# if fd == -1:
|
||||||
|
# return None
|
||||||
|
# mask = int(events)
|
||||||
|
# selector_key = (fs, mask, data)
|
||||||
|
# pass
|
||||||
|
|
||||||
# def tick(self):
|
# def tick(self):
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue