1
1
"""Based on (express-graphql)[https://github.com/graphql/express-graphql/blob/main/src/renderGraphiQL.ts] and
2
- (subscriptions-transport- ws)[https://github.com/apollographql/subscriptions-transport -ws]"""
2
+ (graphql- ws)[https://github.com/enisdenjo/graphql -ws]"""
3
3
import json
4
4
import re
5
5
from typing import Any , Dict , Optional , Tuple
@@ -216,24 +216,12 @@ 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
219
def process_var (template : str , name : str , value : Any , jsonify = False ) -> str :
233
- pattern = r"{{\s*" + name + r"(\s*|[^}]+)*\s*}}"
220
+ pattern = r"{{\s*" + name . replace ( " \\ " , r"\\" ) + r"(\s*|[^}]+)*\s*}}"
234
221
if jsonify and value not in ["null" , "undefined" ]:
235
222
value = json .dumps (value )
236
- value = escape_js_value (value )
223
+
224
+ value = value .replace ("\\ " , r"\\" )
237
225
238
226
return re .sub (pattern , value , template )
239
227
@@ -296,6 +284,9 @@ def _render_graphiql(
296
284
or "false" ,
297
285
}
298
286
287
+ if template_vars ["result" ] in ("null" ):
288
+ template_vars ["result" ] = None
289
+
299
290
return graphiql_template , template_vars
300
291
301
292
@@ -305,7 +296,7 @@ async def render_graphiql_async(
305
296
options : Optional [GraphiQLOptions ] = None ,
306
297
) -> str :
307
298
graphiql_template , template_vars = _render_graphiql (data , config , options )
308
- jinja_env : Optional [ Environment ] = config .get ("jinja_env" )
299
+ jinja_env = config .get ("jinja_env" )
309
300
310
301
if jinja_env :
311
302
template = jinja_env .from_string (graphiql_template )
@@ -324,6 +315,11 @@ def render_graphiql_sync(
324
315
options : Optional [GraphiQLOptions ] = None ,
325
316
) -> str :
326
317
graphiql_template , template_vars = _render_graphiql (data , config , options )
318
+ jinja_env = config .get ("jinja_env" )
327
319
328
- source = simple_renderer (graphiql_template , ** template_vars )
320
+ if jinja_env :
321
+ template = jinja_env .from_string (graphiql_template )
322
+ source = template .render (** template_vars )
323
+ else :
324
+ source = simple_renderer (graphiql_template , ** template_vars )
329
325
return source
0 commit comments