Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from graphql import GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString
schema = GraphQLSchema(
query=GraphQLObjectType(
name="RootQueryType",
fields={
"hello": GraphQLField(GraphQLString, resolve=lambda obj, info: "world")
},
)
)
def main() -> None:
from graphql import graphql_sync
query = "{ hello }"
print(graphql_sync(schema, query))
if __name__ == "__main__":
main()
import asyncio
from graphql import GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString
async def resolve_hello(obj, info):
print("start resolve hello")
await asyncio.sleep(1)
print("finish resolve hello")
return "world"
schema = GraphQLSchema(
query=GraphQLObjectType(
name="RootQueryType",
fields={"hello": GraphQLField(GraphQLString, resolve=resolve_hello)},
)
)
def main() -> None:
from graphql import graphql
async def run():
query = "{ hello }"
print(await graphql(schema, query))
asyncio.run(run(), debug=True)
if __name__ == "__main__":
main()
import asyncio
import logging
from ariadne import ObjectType, gql, make_executable_schema, graphql
# Ariadne uses dedicated objects
query = ObjectType("Query")
# Map resolvers to fields in Query type using decorator syntax...
@query.field("hello")
def resolve_hello(_, info):
request = info.context["request"]
user_agent = request.get("HTTP_USER_AGENT", "guest")
return "Hello, %s!" % user_agent
type_defs = gql(
"""
type Query {
hello: String!
}
"""
)
logging.basicConfig(level=logging.DEBUG)
schema = make_executable_schema(type_defs, query)
query = {"query": "{ hello }"}
atask = graphql(
schema, query, debug=True, context_value={"request": {"HTTP_USER_AGENT": "World"}}
)
print(asyncio.run(atask, debug=True))
import pytest
import async_asgi_testclient as aat
async def app(scope, receive, send):
# assert scope["type"] == "http"
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)
await send({"type": "http.response.body", "body": b"Hello, world!"})
@pytest.mark.asyncio
async def test_app():
async with aat.TestClient(app) as client:
resp = await client.get("/")
assert resp.status_code == 200
assert resp.text == "Hello, world!"
import pytest
from async_asgi_testclient import TestClient
from ariadne import ObjectType, gql, make_executable_schema
from ariadne.asgi import GraphQL
TestClient.__test__ = False # prevent PytestCollectionWarning
# Ariadne uses dedicated objects
query = ObjectType("Query")
# Map resolvers to fields in Query type using decorator syntax...
@query.field("hello")
def resolve_hello(_, info):
request = info.context["request"]
user_agent = request.get("HTTP_USER_AGENT", "guest")
return "Hello, %s!" % user_agent
type_defs = gql(
"""
type Query {
hello: String!
}
"""
)
schema = make_executable_schema(type_defs, query)
# As standalone ASGI or WSGI app...
app = GraphQL(schema, debug=True)
@pytest.mark.asyncio
async def test_app():
client = TestClient(app)
resp = await client.post("/", json={"query": "{ hello }"})
assert resp.status_code == 200
assert resp.json() == {"data": {"hello": "Hello, guest!"}}
00:
python $@*.py
01:
python $@*.py
02:
python $@*.py
03:
pytest -vv $@*.py
04:
pytest -vv $@*.py
aniso8601==7.0.0
ariadne==0.7.0
async-asgi-testclient==1.1.1
atomicwrites==1.3.0
attrs==19.3.0
certifi==2019.9.11
chardet==3.0.4
Click==7.0
graphene==2.1.8
graphql-core==3.0.0b1
GraphQL-core-next==1.1.1
graphql-relay==2.0.0
h11==0.8.1
httptools==0.0.13
idna==2.8
importlib-metadata==0.23
more-itertools==7.2.0
multidict==4.5.2
packaging==19.2
pluggy==0.13.0
promise==2.2.1
py==1.8.0
pyparsing==2.4.2
pytest==5.2.1
pytest-asyncio==0.10.0
python-multipart==0.0.5
requests==2.22.0
six==1.12.0
starlette==0.12.10
typing-extensions==3.7.4
urllib3==1.25.6
uvicorn==0.9.0
uvloop==0.13.0
wcwidth==0.1.7
websockets==8.0.2
zipp==0.6.0
graphql-core>=3a
graphene
ariadne
uvicorn
async-asgi-testclient
pytest
pytest-asyncio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.