Skip to content

Commit

Permalink
Align bridge_ex with improvements made on Peano (#43)
Browse files Browse the repository at this point in the history
* Align bridge_ex with improvements made on Peano

this update allows the library to be in sync with the Peano version of `graphql_utils.ex`, so that it can be implemented there more easily in any use case.

More specifically, `normalize_inner_fields` now handle cases where the root value object is a number or a boolean. Before it would break ad the map pattern match

* fix spec and pattern match

* format

* format a mano che dio non so come entrarci

* test
  • Loading branch information
macciomauro authored Apr 5, 2022
1 parent fdc899b commit bfc2b27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/graphql/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ defmodule BridgeEx.Graphql.Utils do

def parse_response({:ok, %{data: data}}), do: {:ok, data}

@spec normalize_inner_fields(%{atom() => any()} | String.t()) :: %{atom() => any()} | String.t()
def normalize_inner_fields(binary) when is_binary(binary), do: binary
def normalize_inner_fields(map = %{}), do: Enum.reduce(map, %{}, &do_normalize_inner_fields/2)
@spec do_normalize_inner_fields({atom(), any()}, map()) :: %{atom() => any()}
@spec normalize_inner_fields(any()) :: any()
def normalize_inner_fields(map) when is_map(map),
do: Enum.reduce(map, %{}, &do_normalize_inner_fields/2)

def normalize_inner_fields(value), do: value

@spec do_normalize_inner_fields({atom() | String.t(), any()}, map()) :: %{atom() => any()}
defp do_normalize_inner_fields({key, value}, acc) when is_map(value) do
Map.merge(acc, %{to_snake_case(key) => normalize_inner_fields(value)})
end
Expand Down
8 changes: 8 additions & 0 deletions test/graphql/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ defmodule BridgeEx.Graphql.UtilsTest do
assert "some-string" == Utils.normalize_inner_fields("some-string")
end

test "does not change integers" do
assert 5 == Utils.normalize_inner_fields(5)
end

test "does not change booleans" do
assert true == Utils.normalize_inner_fields(true)
end

test "does not change empty maps" do
assert %{} == Utils.normalize_inner_fields(%{})
end
Expand Down

0 comments on commit bfc2b27

Please sign in to comment.