@@ -216,54 +216,6 @@ class GraphiQLOptions(TypedDict):
216
216
should_persist_headers : Optional [bool ]
217
217
218
218
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
-
267
219
def _render_graphiql (
268
220
data : GraphiQLData ,
269
221
config : GraphiQLConfig ,
@@ -296,6 +248,9 @@ def _render_graphiql(
296
248
or "false" ,
297
249
}
298
250
251
+ if template_vars ["result" ] in ("null" , "undefined" ):
252
+ template_vars ["result" ] = None
253
+
299
254
return graphiql_template , template_vars
300
255
301
256
@@ -305,16 +260,17 @@ async def render_graphiql_async(
305
260
options : Optional [GraphiQLOptions ] = None ,
306
261
) -> str :
307
262
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 )
263
+
264
+ jinja_env = config .get ("jinja_env" ) or Environment ()
265
+
266
+ template = jinja_env .from_string (graphiql_template )
267
+
268
+ source = (
269
+ await template .render_async (** template_vars )
270
+ if jinja_env .is_async
271
+ else template .render (** template_vars )
272
+ )
273
+
318
274
return source
319
275
320
276
@@ -325,5 +281,6 @@ def render_graphiql_sync(
325
281
) -> str :
326
282
graphiql_template , template_vars = _render_graphiql (data , config , options )
327
283
328
- source = simple_renderer (graphiql_template , ** template_vars )
284
+ template = Environment ().from_string (graphiql_template )
285
+ source = template .render (** template_vars )
329
286
return source
0 commit comments