11from functools import partial
2+ from typing import List
23
3- from flask import Response , request
4+ from flask import Response , render_template_string , request
45from flask .views import View
56from graphql .error import GraphQLError
67from graphql .type .schema import GraphQLSchema
78
89from graphql_server import (
10+ GraphQLParams ,
911 HttpQueryError ,
1012 encode_execution_results ,
1113 format_error_default ,
1214 json_encode ,
1315 load_json_body ,
1416 run_http_query ,
1517)
16-
17- from .render_graphiql import render_graphiql
18+ from graphql_server .render_graphiql import (
19+ GraphiQLConfig ,
20+ GraphiQLData ,
21+ render_graphiql_sync ,
22+ )
1823
1924
2025class GraphQLView (View ):
@@ -27,6 +32,8 @@ class GraphQLView(View):
2732 graphiql_html_title = None
2833 middleware = None
2934 batch = False
35+ subscriptions = None
36+ headers = None
3037
3138 methods = ["GET" , "POST" , "PUT" , "DELETE" ]
3239
@@ -50,15 +57,6 @@ def get_context_value(self):
5057 def get_middleware (self ):
5158 return self .middleware
5259
53- def render_graphiql (self , params , result ):
54- return render_graphiql (
55- params = params ,
56- result = result ,
57- graphiql_version = self .graphiql_version ,
58- graphiql_template = self .graphiql_template ,
59- graphiql_html_title = self .graphiql_html_title ,
60- )
61-
6260 format_error = staticmethod (format_error_default )
6361 encode = staticmethod (json_encode )
6462
@@ -72,6 +70,7 @@ def dispatch_request(self):
7270
7371 pretty = self .pretty or show_graphiql or request .args .get ("pretty" )
7472
73+ all_params : List [GraphQLParams ]
7574 execution_results , all_params = run_http_query (
7675 self .schema ,
7776 request_method ,
@@ -88,11 +87,28 @@ def dispatch_request(self):
8887 execution_results ,
8988 is_batch = isinstance (data , list ),
9089 format_error = self .format_error ,
91- encode = partial (self .encode , pretty = pretty ),
90+ encode = partial (self .encode , pretty = pretty ), # noqa
9291 )
9392
9493 if show_graphiql :
95- return self .render_graphiql (params = all_params [0 ], result = result )
94+ graphiql_data = GraphiQLData (
95+ result = result ,
96+ query = getattr (all_params [0 ], "query" ),
97+ variables = getattr (all_params [0 ], "variables" ),
98+ operation_name = getattr (all_params [0 ], "operation_name" ),
99+ subscription_url = self .subscriptions ,
100+ headers = self .headers ,
101+ )
102+ graphiql_config = GraphiQLConfig (
103+ graphiql_version = self .graphiql_version ,
104+ graphiql_template = self .graphiql_template ,
105+ graphiql_html_title = self .graphiql_html_title ,
106+ jinja_env = None ,
107+ )
108+ source = render_graphiql_sync (
109+ data = graphiql_data , config = graphiql_config
110+ )
111+ return render_template_string (source )
96112
97113 return Response (result , status = status_code , content_type = "application/json" )
98114
0 commit comments