11"""Based on (express-graphql)[https://github.com/graphql/express-graphql/blob/main/src/renderGraphiQL.ts] and
22(subscriptions-transport-ws)[https://github.com/apollographql/subscriptions-transport-ws]"""
3- import json
4- import re
53from typing import Any , Dict , Optional , Tuple
64
75from jinja2 import Environment
@@ -216,54 +214,6 @@ class GraphiQLOptions(TypedDict):
216214 should_persist_headers : Optional [bool ]
217215
218216
219- def escape_js_value (value : Any ) -> Any :
220- quotation = False
221- if value .startswith ('"' ) and value .endswith ('"' ):
222- quotation = True
223- value = value [1 : len (value ) - 1 ]
224-
225- value = value .replace ("\\ \\ n" , "\\ \\ \\ n" ).replace ("\\ n" , "\\ \\ n" )
226- if quotation :
227- value = '"' + value .replace ('\\ \\ "' , '"' ).replace ('"' , '\\ "' ) + '"'
228-
229- return value
230-
231-
232- def process_var (template : str , name : str , value : Any , jsonify = False ) -> str :
233- pattern = r"{{\s*" + name + r"(\s*|[^}]+)*\s*}}"
234- if jsonify and value not in ["null" , "undefined" ]:
235- value = json .dumps (value )
236- value = escape_js_value (value )
237-
238- return re .sub (pattern , value , template )
239-
240-
241- def simple_renderer (template : str , ** values : Dict [str , Any ]) -> str :
242- replace = [
243- "graphiql_version" ,
244- "graphiql_html_title" ,
245- "subscription_url" ,
246- "header_editor_enabled" ,
247- "should_persist_headers" ,
248- ]
249- replace_jsonify = [
250- "query" ,
251- "result" ,
252- "variables" ,
253- "operation_name" ,
254- "default_query" ,
255- "headers" ,
256- ]
257-
258- for r in replace :
259- template = process_var (template , r , values .get (r , "" ))
260-
261- for r in replace_jsonify :
262- template = process_var (template , r , values .get (r , "" ), True )
263-
264- return template
265-
266-
267217def _render_graphiql (
268218 data : GraphiQLData ,
269219 config : GraphiQLConfig ,
@@ -296,6 +246,9 @@ def _render_graphiql(
296246 or "false" ,
297247 }
298248
249+ if template_vars ["result" ] in ("null" , "undefined" ):
250+ template_vars ["result" ] = None
251+
299252 return graphiql_template , template_vars
300253
301254
@@ -305,16 +258,17 @@ async def render_graphiql_async(
305258 options : Optional [GraphiQLOptions ] = None ,
306259) -> str :
307260 graphiql_template , template_vars = _render_graphiql (data , config , options )
308- jinja_env : Optional [Environment ] = config .get ("jinja_env" )
309-
310- if jinja_env :
311- template = jinja_env .from_string (graphiql_template )
312- if jinja_env .is_async :
313- source = await template .render_async (** template_vars )
314- else :
315- source = template .render (** template_vars )
316- else :
317- source = simple_renderer (graphiql_template , ** template_vars )
261+
262+ jinja_env = config .get ("jinja_env" ) or Environment ()
263+
264+ template = jinja_env .from_string (graphiql_template )
265+
266+ source = (
267+ await template .render_async (** template_vars )
268+ if jinja_env .is_async
269+ else template .render (** template_vars )
270+ )
271+
318272 return source
319273
320274
@@ -325,5 +279,6 @@ def render_graphiql_sync(
325279) -> str :
326280 graphiql_template , template_vars = _render_graphiql (data , config , options )
327281
328- source = simple_renderer (graphiql_template , ** template_vars )
282+ template = Environment ().from_string (graphiql_template )
283+ source = template .render (** template_vars )
329284 return source
0 commit comments