Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 50cdbd8

Browse files
author
Josh Price
committed
Escape query, variables and results correctly for GraphiQL JS template
1 parent 0a3246c commit 50cdbd8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/graphql/plug/endpoint.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ defmodule GraphQL.Plug.Endpoint do
4747
|> execute(schema, root_value, query, variables, operation_name)
4848
end
4949

50+
defp escape_newlines(s) do
51+
String.replace(s, ~r/\n/, "\\n")
52+
end
53+
5054
defp handle_graphiql_call(conn, schema, root_value, query, variables, operation_name) do
5155
{:ok, data} = GraphQL.execute(schema, query, root_value, variables, operation_name)
52-
{:ok, result} = Poison.encode(data)
56+
{:ok, variables} = Poison.encode(variables, pretty: true)
57+
{:ok, result} = Poison.encode(data, pretty: true)
58+
graphiql = graphiql_html("0.4.5", escape_newlines(query), escape_newlines(variables), escape_newlines(result))
5359
conn
5460
|> put_resp_content_type("text/html")
55-
|> send_resp(200, graphiql_html("0.4.4", query, nil, result))
61+
|> send_resp(200, graphiql)
5662
end
5763

5864
defp handle_error(conn, message) do

0 commit comments

Comments
 (0)