diff --git a/docs/datastructures.md b/docs/datastructures.md new file mode 100644 index 00000000..e69de29b diff --git a/docs/graphql.md b/docs/graphql.md index f1fd582a..c3e96e5f 100644 --- a/docs/graphql.md +++ b/docs/graphql.md @@ -20,7 +20,7 @@ schema = graphene.Schema(query=Query) app = Starlette() -app.add_route('/', GraphQLApp(schema=schema)) +app.add_route('/', GraphQLApp(schema=schema), methods=['GET', 'POST']) ``` ## Sync or Async executors @@ -51,5 +51,5 @@ schema = graphene.Schema(query=Query) app = Starlette() -app.add_route('/', GraphQLApp(schema=schema, executor=AsyncioExecutor())) +app.add_route('/', GraphQLApp(schema=schema, executor=AsyncioExecutor()), methods=['GET', 'POST']) ``` diff --git a/starlette/graphql.py b/starlette/graphql.py index 38996a84..acd35389 100644 --- a/starlette/graphql.py +++ b/starlette/graphql.py @@ -1,9 +1,10 @@ from starlette import status -from starlette.responses import PlainTextResponse, Response, JSONResponse +from starlette.responses import PlainTextResponse, Response, JSONResponse, HTMLResponse from starlette.requests import Request from starlette.types import ASGIInstance, Receive, Scope, Send import asyncio import functools +import json import typing try: @@ -29,11 +30,14 @@ class GraphQLApp: async def asgi(self, receive: Receive, send: Send, scope: Scope) -> None: request = Request(scope, receive=receive) - response = await self.handler(request) + response = await self.handle_graphql(request) await response(receive, send) - async def handler(self, request: Request) -> Response: + async def handle_graphql(self, request: Request) -> Response: if request.method == "GET": + if "text/html" in request.headers.get("Accept", ""): + return await self.handle_graphiql(request) + data = request.query_params # type: typing.Mapping[str, typing.Any] elif request.method == "POST": @@ -95,3 +99,142 @@ class GraphQLApp: ) loop = asyncio.get_event_loop() return await loop.run_in_executor(None, func, query) + + async def handle_graphiql(self, request: Request) -> Response: + text = GRAPHIQL.replace("{{REQUEST_PATH}}", json.dumps(request.url.path)) + return HTMLResponse(text) + + +GRAPHIQL = """ + + + +
+ + + + + + + + + +