Skip to content

Commit 40a67ba

Browse files
committed
fix include_variables bug
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.
1 parent b12e9f4 commit 40a67ba

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

lib/uinta/plug.ex

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,24 @@ if Code.ensure_loaded?(Plug) do
144144
@spec graphql_info(Plug.Conn.t(), map()) :: graphql_info() | nil
145145
defp graphql_info(%{method: "POST", params: params}, opts) do
146146
operation = params["operationName"]
147-
variables = params["variables"]
148-
149-
encoded_variables =
150-
with true <- opts.include_variables,
151-
filtered = filter_variables(variables, opts.filter_variables),
152-
{:ok, encoded} <- Jason.encode(filtered) do
153-
encoded
154-
else
155-
_ -> nil
156-
end
157147

158148
type =
159149
params
160150
|> Map.get("query", "")
161151
|> String.trim()
162152
|> query_type()
163153

164-
if !is_nil(type) && !is_nil(operation) do
165-
%{type: type, operation: operation, variables: encoded_variables}
154+
info = %{type: type, operation: operation}
155+
156+
with {:is_nil, false} <- {:is_nil, is_nil(type) || is_nil(operation)},
157+
true <- opts.include_variables,
158+
variables when is_map(variables) <- params["variables"],
159+
filtered = filter_variables(variables, opts.filter_variables),
160+
{:ok, encoded} <- Jason.encode(filtered) do
161+
Map.put(info, :variables, encoded)
166162
else
167-
nil
163+
{:is_nil, true} -> nil
164+
_ -> info
168165
end
169166
end
170167

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Uinta.MixProject do
88
app: :uinta,
99
name: "Uinta",
1010
description: "Simpler structured logs and lower log volume for Elixir apps",
11-
version: "0.3.0",
11+
version: "0.3.1",
1212
elixir: "~> 1.8",
1313
source_url: @project_url,
1414
homepage_url: @project_url,

test/uinta/plug_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ defmodule Uinta.PlugTest do
158158
assert message =~ "with {\"user_uid\":\"b1641ddf-b7b0-445e-bcbb-96ef359eae81\"}"
159159
end
160160

161+
test "doesn't try to include variables on non-graphql requests" do
162+
message = capture_log(fn -> IncludeVariablesPlug.call(conn(:post, "/", %{}), []) end)
163+
refute message =~ "with"
164+
end
165+
166+
test "doesn't try to include variables when none were given" do
167+
params = %{"operationName" => "getUser", "query" => "query getUser"}
168+
169+
message =
170+
capture_log(fn -> IncludeVariablesPlug.call(conn(:post, "/graphql", params), []) end)
171+
172+
refute message =~ "with"
173+
end
174+
161175
test "filters variables when applicable" do
162176
variables = %{
163177
"user_uid" => "b1641ddf-b7b0-445e-bcbb-96ef359eae81",

0 commit comments

Comments
 (0)