1
1
"""Based on (express-graphql)[https://github.com/graphql/express-graphql/blob/main/src/renderGraphiQL.ts] and
2
2
(subscriptions-transport-ws)[https://github.com/apollographql/subscriptions-transport-ws]"""
3
- import json
4
- import re
5
3
from typing import Any , Dict , Optional , Tuple
6
4
7
5
from jinja2 import Environment
@@ -216,54 +214,6 @@ class GraphiQLOptions(TypedDict):
216
214
should_persist_headers : Optional [bool ]
217
215
218
216
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
217
def _render_graphiql (
268
218
data : GraphiQLData ,
269
219
config : GraphiQLConfig ,
@@ -296,6 +246,9 @@ def _render_graphiql(
296
246
or "false" ,
297
247
}
298
248
249
+ if template_vars ["result" ] in ("null" , "undefined" ):
250
+ template_vars ["result" ] = None
251
+
299
252
return graphiql_template , template_vars
300
253
301
254
@@ -305,16 +258,17 @@ async def render_graphiql_async(
305
258
options : Optional [GraphiQLOptions ] = None ,
306
259
) -> str :
307
260
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
+
318
272
return source
319
273
320
274
@@ -325,5 +279,6 @@ def render_graphiql_sync(
325
279
) -> str :
326
280
graphiql_template , template_vars = _render_graphiql (data , config , options )
327
281
328
- source = simple_renderer (graphiql_template , ** template_vars )
282
+ template = Environment ().from_string (graphiql_template )
283
+ source = template .render (** template_vars )
329
284
return source
0 commit comments