Skip to content

Commit

Permalink
Do not include ordinal in result when no function is set
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardd committed Aug 17, 2022
1 parent b0cd04e commit 6601cc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
32 changes: 10 additions & 22 deletions lib/absinthe/phase/subscription/get_ordinal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,22 @@ defmodule Absinthe.Phase.Subscription.GetOrdinal do

@spec run(any, Keyword.t()) :: {:ok, Blueprint.t()}
def run(blueprint, _options \\ []) do
op = Blueprint.current_operation(blueprint)

if op.type == :subscription do
{:ok,
%{blueprint | result: Map.put(blueprint.result, :ordinal, get_ordinal(op, blueprint))}}
with %{type: :subscription, selections: [field]} <- Blueprint.current_operation(blueprint),
{:ok, config} = SubscribeSelf.get_config(field, blueprint.execution.context, blueprint),
ordinal_fun when is_function(ordinal_fun, 1) <- config[:ordinal] do
result = ordinal_fun.(blueprint.execution.root_value)
{:ok, %{blueprint | result: Map.put(blueprint.result, :ordinal, result)}}
else
{:ok, blueprint}
end
end

defp get_ordinal(op, blueprint) do
%{selections: [field]} = op
{:ok, config} = SubscribeSelf.get_config(field, blueprint.execution.context, blueprint)

case config[:ordinal] do
nil ->
nil

fun when is_function(fun, 1) ->
fun.(blueprint.execution.root_value)

_fun ->
f when is_function(f) ->
IO.write(
:stderr,
"Ordinal function must be 1-arity"
)

nil
{:ok, blueprint}

_ ->
{:ok, blueprint}
end
end
end
12 changes: 6 additions & 6 deletions test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ defmodule Absinthe.Execution.SubscriptionTest do

assert %{
event: "subscription:data",
result: %{data: %{"thing" => "foo"}, ordinal: nil},
result: %{data: %{"thing" => "foo"}},
topic: topic
} == msg
end
Expand Down Expand Up @@ -259,7 +259,7 @@ defmodule Absinthe.Execution.SubscriptionTest do

msg = %{
event: "subscription:data",
result: %{data: %{"multipleTopics" => "foo"}, ordinal: nil},
result: %{data: %{"multipleTopics" => "foo"}},
topic: topic
}

Expand Down Expand Up @@ -361,7 +361,7 @@ defmodule Absinthe.Execution.SubscriptionTest do

assert %{
event: "subscription:data",
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}, ordinal: nil},
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}},
topic: topic
} == msg
end
Expand Down Expand Up @@ -409,7 +409,7 @@ defmodule Absinthe.Execution.SubscriptionTest do

assert %{
event: "subscription:data",
result: %{data: %{"thing" => "foo"}, ordinal: nil},
result: %{data: %{"thing" => "foo"}},
topic: topic
} == msg
end
Expand All @@ -428,7 +428,7 @@ defmodule Absinthe.Execution.SubscriptionTest do

assert %{
event: "subscription:data",
result: %{data: %{"thing" => "foo"}, ordinal: nil},
result: %{data: %{"thing" => "foo"}},
topic: topic
} == msg
end)
Expand Down Expand Up @@ -640,7 +640,7 @@ defmodule Absinthe.Execution.SubscriptionTest do

assert %{
event: "subscription:data",
result: %{data: %{"thing" => "foo"}, ordinal: nil},
result: %{data: %{"thing" => "foo"}},
topic: topic
} == msg

Expand Down

0 comments on commit 6601cc5

Please sign in to comment.