Skip to content

Commit

Permalink
fix include_variables bug
Browse files Browse the repository at this point in the history
When the `include_variables` flag was passed to the plug, any time a
post request was received without variables in the request body, an
error would occur.

I have changed the logic so that variables will only be looked for if
the request has both an `operationName` and `type`. Additionally it will
now only return variables if they exist, whether the request be a
GraphQL one or not. And appropriate regression tests have been added.
  • Loading branch information
dnsbty committed Mar 18, 2020
1 parent b12e9f4 commit 40a67ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
23 changes: 10 additions & 13 deletions lib/uinta/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,24 @@ if Code.ensure_loaded?(Plug) do
@spec graphql_info(Plug.Conn.t(), map()) :: graphql_info() | nil
defp graphql_info(%{method: "POST", params: params}, opts) do
operation = params["operationName"]
variables = params["variables"]

encoded_variables =
with true <- opts.include_variables,
filtered = filter_variables(variables, opts.filter_variables),
{:ok, encoded} <- Jason.encode(filtered) do
encoded
else
_ -> nil
end

type =
params
|> Map.get("query", "")
|> String.trim()
|> query_type()

if !is_nil(type) && !is_nil(operation) do
%{type: type, operation: operation, variables: encoded_variables}
info = %{type: type, operation: operation}

with {:is_nil, false} <- {:is_nil, is_nil(type) || is_nil(operation)},
true <- opts.include_variables,
variables when is_map(variables) <- params["variables"],
filtered = filter_variables(variables, opts.filter_variables),
{:ok, encoded} <- Jason.encode(filtered) do
Map.put(info, :variables, encoded)
else
nil
{:is_nil, true} -> nil
_ -> info
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Uinta.MixProject do
app: :uinta,
name: "Uinta",
description: "Simpler structured logs and lower log volume for Elixir apps",
version: "0.3.0",
version: "0.3.1",
elixir: "~> 1.8",
source_url: @project_url,
homepage_url: @project_url,
Expand Down
14 changes: 14 additions & 0 deletions test/uinta/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ defmodule Uinta.PlugTest do
assert message =~ "with {\"user_uid\":\"b1641ddf-b7b0-445e-bcbb-96ef359eae81\"}"
end

test "doesn't try to include variables on non-graphql requests" do
message = capture_log(fn -> IncludeVariablesPlug.call(conn(:post, "/", %{}), []) end)
refute message =~ "with"
end

test "doesn't try to include variables when none were given" do
params = %{"operationName" => "getUser", "query" => "query getUser"}

message =
capture_log(fn -> IncludeVariablesPlug.call(conn(:post, "/graphql", params), []) end)

refute message =~ "with"
end

test "filters variables when applicable" do
variables = %{
"user_uid" => "b1641ddf-b7b0-445e-bcbb-96ef359eae81",
Expand Down

0 comments on commit 40a67ba

Please sign in to comment.