Skip to content

Commit

Permalink
handle array arguments in the regex
Browse files Browse the repository at this point in the history
The regular expression for extracting a query name out of a query string
didn't work properly if an array argument was given. This meant that the
name wouldn't be extracted as expected.

With this change that should work properly, and a regression test has
been added.
  • Loading branch information
dnsbty committed Mar 31, 2020
1 parent bb4b17d commit ba06f0a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The package can be installed by adding `uinta` to your list of dependencies in
```elixir
def deps do
[
{:uinta, "~> 0.3"}
{:uinta, "~> 0.4"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion lib/uinta/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if Code.ensure_loaded?(Plug) do
@behaviour Plug

@default_filter ~w(password passwordConfirmation idToken refreshToken)
@query_name_regex ~r/^(?:(?:query|mutation)\s+(\w+)(?:\(\$\w+:\s+\w+!?(?:,\s+\$\w+:\s+\w+!?)*\))?\s*)?{/
@query_name_regex ~r/^(?:(?:query|mutation)\s+(\w+)(?:\(\$\w+:\s+\[?\w+\]?!?(?:,\s+\$\w+:\s+\[?\w+\]?!?)*\))?\s*)?{/

@type format :: :json | :string
@type graphql_info :: %{type: String.t(), operation: String.t(), variables: String.t() | nil}
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.4.0",
version: "0.4.1",
elixir: "~> 1.8",
source_url: @project_url,
homepage_url: @project_url,
Expand Down
23 changes: 22 additions & 1 deletion test/uinta/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ defmodule Uinta.PlugTest do

variables = %{
"ep" => "JEDI",
review: %{"stars" => 5, "commentary" => "This is a great movie!"}
"review" => %{"stars" => 5, "commentary" => "This is a great movie!"}
}

params = %{"query" => query, "variables" => variables}
Expand All @@ -221,6 +221,27 @@ defmodule Uinta.PlugTest do
assert message =~ "MUTATION CreateReviewForEpisode"
end

test "gets the GraphQL operation name from the query when there is an array parameter" do
query = """
mutation track($userId: String!, $event: String!, $properties: [String]) {
track(userId: $userId, event: $event, properties: $properties) {
status
}
}
"""

variables = %{
"userId" => "55203f63-0b79-426c-840e-ea68bdac765c",
"event" => "WEBSITE_WIDGET_PROMPT_SHOW",
"properties" => ["green", "firefox"]
}

params = %{"query" => query, "variables" => variables}

message = capture_log(fn -> MyPlug.call(conn(:post, "/graphql", params), []) end)
assert message =~ "MUTATION track"
end

test "includes the query when it isn't named" do
query = """
{
Expand Down

0 comments on commit ba06f0a

Please sign in to comment.