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

Commit

Permalink
Escape query, variables and results correctly for GraphiQL JS template
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Price committed Jan 25, 2016
1 parent 0a3246c commit 50cdbd8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/graphql/plug/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ defmodule GraphQL.Plug.Endpoint do
|> execute(schema, root_value, query, variables, operation_name)
end

defp escape_newlines(s) do
String.replace(s, ~r/\n/, "\\n")
end

defp handle_graphiql_call(conn, schema, root_value, query, variables, operation_name) do
{:ok, data} = GraphQL.execute(schema, query, root_value, variables, operation_name)
{:ok, result} = Poison.encode(data)
{:ok, variables} = Poison.encode(variables, pretty: true)
{:ok, result} = Poison.encode(data, pretty: true)
graphiql = graphiql_html("0.4.5", escape_newlines(query), escape_newlines(variables), escape_newlines(result))
conn
|> put_resp_content_type("text/html")
|> send_resp(200, graphiql_html("0.4.4", query, nil, result))
|> send_resp(200, graphiql)
end

defp handle_error(conn, message) do
Expand Down

0 comments on commit 50cdbd8

Please sign in to comment.