Skip to content

Add graphiql options and missing flask context #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion graphql_server/aiohttp/graphqlview.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
from graphql_server.render_graphiql import (
GraphiQLConfig,
GraphiQLData,
GraphiQLOptions,
render_graphiql_async,
)

@@ -39,6 +40,9 @@ class GraphQLView:
enable_async = False
subscriptions = None
headers = None
default_query = None
header_editor_enabled = None
should_persist_headers = None

accepted_methods = ["GET", "POST", "PUT", "DELETE"]

@@ -174,8 +178,13 @@ async def __call__(self, request):
graphiql_html_title=self.graphiql_html_title,
jinja_env=self.jinja_env,
)
graphiql_options = GraphiQLOptions(
default_query=self.default_query,
header_editor_enabled=self.header_editor_enabled,
should_persist_headers=self.should_persist_headers,
)
source = await render_graphiql_async(
data=graphiql_data, config=graphiql_config
data=graphiql_data, config=graphiql_config, options=graphiql_options
)
return web.Response(text=source, content_type="text/html")

28 changes: 23 additions & 5 deletions graphql_server/flask/graphqlview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy
from collections.abc import MutableMapping
from functools import partial
from typing import List

@@ -18,13 +20,15 @@
from graphql_server.render_graphiql import (
GraphiQLConfig,
GraphiQLData,
GraphiQLOptions,
render_graphiql_sync,
)


class GraphQLView(View):
schema = None
root_value = None
context = None
pretty = False
graphiql = False
graphiql_version = None
@@ -34,6 +38,9 @@ class GraphQLView(View):
batch = False
subscriptions = None
headers = None
default_query = None
header_editor_enabled = None
should_persist_headers = None

methods = ["GET", "POST", "PUT", "DELETE"]

@@ -50,12 +57,18 @@ def __init__(self, **kwargs):
self.schema, GraphQLSchema
), "A Schema is required to be provided to GraphQLView."

# noinspection PyUnusedLocal
def get_root_value(self):
return self.root_value

def get_context_value(self):
return request
def get_context(self):
context = (
copy.copy(self.context)
if self.context and isinstance(self.context, MutableMapping)
else {}
)
if isinstance(context, MutableMapping) and "request" not in context:
context.update({"request": request})
return context

def get_middleware(self):
return self.middleware
@@ -80,7 +93,7 @@ def dispatch_request(self):
catch=catch,
# Execute options
root_value=self.get_root_value(),
context_value=self.get_context_value(),
context_value=self.get_context(),
middleware=self.get_middleware(),
)
result, status_code = encode_execution_results(
@@ -105,8 +118,13 @@ def dispatch_request(self):
graphiql_html_title=self.graphiql_html_title,
jinja_env=None,
)
graphiql_options = GraphiQLOptions(
default_query=self.default_query,
header_editor_enabled=self.header_editor_enabled,
should_persist_headers=self.should_persist_headers,
)
source = render_graphiql_sync(
data=graphiql_data, config=graphiql_config
data=graphiql_data, config=graphiql_config, options=graphiql_options
)
return render_template_string(source)

2 changes: 1 addition & 1 deletion graphql_server/render_graphiql.py
Original file line number Diff line number Diff line change
@@ -201,7 +201,7 @@ class GraphiQLOptions(TypedDict):

default_query
An optional GraphQL string to use when no query is provided and no stored
query exists from a previous session. If undefined is provided, GraphiQL
query exists from a previous session. If None is provided, GraphiQL
will use its own default query.
header_editor_enabled
An optional boolean which enables the header editor when true.
13 changes: 12 additions & 1 deletion graphql_server/sanic/graphqlview.py
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
from graphql_server.render_graphiql import (
GraphiQLConfig,
GraphiQLData,
GraphiQLOptions,
render_graphiql_async,
)

@@ -41,6 +42,9 @@ class GraphQLView(HTTPMethodView):
enable_async = False
subscriptions = None
headers = None
default_query = None
header_editor_enabled = None
should_persist_headers = None

methods = ["GET", "POST", "PUT", "DELETE"]

@@ -127,8 +131,15 @@ async def dispatch_request(self, request, *args, **kwargs):
graphiql_html_title=self.graphiql_html_title,
jinja_env=self.jinja_env,
)
graphiql_options = GraphiQLOptions(
default_query=self.default_query,
header_editor_enabled=self.header_editor_enabled,
should_persist_headers=self.should_persist_headers,
)
source = await render_graphiql_async(
data=graphiql_data, config=graphiql_config
data=graphiql_data,
config=graphiql_config,
options=graphiql_options,
)
return html(source)

15 changes: 14 additions & 1 deletion graphql_server/webob/graphqlview.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
from graphql_server.render_graphiql import (
GraphiQLConfig,
GraphiQLData,
GraphiQLOptions,
render_graphiql_sync,
)

@@ -38,6 +39,9 @@ class GraphQLView:
enable_async = False
subscriptions = None
headers = None
default_query = None
header_editor_enabled = None
should_persist_headers = None
charset = "UTF-8"

format_error = staticmethod(format_error_default)
@@ -117,8 +121,17 @@ def dispatch_request(self, request):
graphiql_html_title=self.graphiql_html_title,
jinja_env=None,
)
graphiql_options = GraphiQLOptions(
default_query=self.default_query,
header_editor_enabled=self.header_editor_enabled,
should_persist_headers=self.should_persist_headers,
)
return Response(
render_graphiql_sync(data=graphiql_data, config=graphiql_config),
render_graphiql_sync(
data=graphiql_data,
config=graphiql_config,
options=graphiql_options,
),
charset=self.charset,
content_type="text/html",
)
13 changes: 11 additions & 2 deletions tests/aiohttp/schema.py
Original file line number Diff line number Diff line change
@@ -24,8 +24,17 @@ def resolve_raises(*_):
resolve=lambda obj, info, *args: info.context["request"].query.get("q"),
),
"context": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info, *args: info.context["request"],
GraphQLObjectType(
name="context",
fields={
"session": GraphQLField(GraphQLString),
"request": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info: info.context["request"],
),
},
),
resolve=lambda obj, info: info.context,
),
"test": GraphQLField(
type_=GraphQLString,
Loading