1+ import copy
2+ from collections .abc import MutableMapping
13from functools import partial
24from typing import List
35
1820from graphql_server .render_graphiql import (
1921 GraphiQLConfig ,
2022 GraphiQLData ,
23+ GraphiQLOptions ,
2124 render_graphiql_sync ,
2225)
2326
2427
2528class GraphQLView (View ):
2629 schema = None
2730 root_value = None
31+ context = None
2832 pretty = False
2933 graphiql = False
3034 graphiql_version = None
@@ -34,6 +38,9 @@ class GraphQLView(View):
3438 batch = False
3539 subscriptions = None
3640 headers = None
41+ default_query = None
42+ header_editor_enabled = None
43+ should_persist_headers = None
3744
3845 methods = ["GET" , "POST" , "PUT" , "DELETE" ]
3946
@@ -50,12 +57,18 @@ def __init__(self, **kwargs):
5057 self .schema , GraphQLSchema
5158 ), "A Schema is required to be provided to GraphQLView."
5259
53- # noinspection PyUnusedLocal
5460 def get_root_value (self ):
5561 return self .root_value
5662
57- def get_context_value (self ):
58- return request
63+ def get_context (self ):
64+ context = (
65+ copy .copy (self .context )
66+ if self .context and isinstance (self .context , MutableMapping )
67+ else {}
68+ )
69+ if isinstance (context , MutableMapping ) and "request" not in context :
70+ context .update ({"request" : request })
71+ return context
5972
6073 def get_middleware (self ):
6174 return self .middleware
@@ -80,7 +93,7 @@ def dispatch_request(self):
8093 catch = catch ,
8194 # Execute options
8295 root_value = self .get_root_value (),
83- context_value = self .get_context_value (),
96+ context_value = self .get_context (),
8497 middleware = self .get_middleware (),
8598 )
8699 result , status_code = encode_execution_results (
@@ -105,8 +118,13 @@ def dispatch_request(self):
105118 graphiql_html_title = self .graphiql_html_title ,
106119 jinja_env = None ,
107120 )
121+ graphiql_options = GraphiQLOptions (
122+ default_query = self .default_query ,
123+ header_editor_enabled = self .header_editor_enabled ,
124+ should_persist_headers = self .should_persist_headers ,
125+ )
108126 source = render_graphiql_sync (
109- data = graphiql_data , config = graphiql_config
127+ data = graphiql_data , config = graphiql_config , options = graphiql_options
110128 )
111129 return render_template_string (source )
112130
0 commit comments