Skip to content

Commit 6601cc5

Browse files
committed
Do not include ordinal in result when no function is set
1 parent b0cd04e commit 6601cc5

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

lib/absinthe/phase/subscription/get_ordinal.ex

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,22 @@ defmodule Absinthe.Phase.Subscription.GetOrdinal do
99

1010
@spec run(any, Keyword.t()) :: {:ok, Blueprint.t()}
1111
def run(blueprint, _options \\ []) do
12-
op = Blueprint.current_operation(blueprint)
13-
14-
if op.type == :subscription do
15-
{:ok,
16-
%{blueprint | result: Map.put(blueprint.result, :ordinal, get_ordinal(op, blueprint))}}
12+
with %{type: :subscription, selections: [field]} <- Blueprint.current_operation(blueprint),
13+
{:ok, config} = SubscribeSelf.get_config(field, blueprint.execution.context, blueprint),
14+
ordinal_fun when is_function(ordinal_fun, 1) <- config[:ordinal] do
15+
result = ordinal_fun.(blueprint.execution.root_value)
16+
{:ok, %{blueprint | result: Map.put(blueprint.result, :ordinal, result)}}
1717
else
18-
{:ok, blueprint}
19-
end
20-
end
21-
22-
defp get_ordinal(op, blueprint) do
23-
%{selections: [field]} = op
24-
{:ok, config} = SubscribeSelf.get_config(field, blueprint.execution.context, blueprint)
25-
26-
case config[:ordinal] do
27-
nil ->
28-
nil
29-
30-
fun when is_function(fun, 1) ->
31-
fun.(blueprint.execution.root_value)
32-
33-
_fun ->
18+
f when is_function(f) ->
3419
IO.write(
3520
:stderr,
3621
"Ordinal function must be 1-arity"
3722
)
3823

39-
nil
24+
{:ok, blueprint}
25+
26+
_ ->
27+
{:ok, blueprint}
4028
end
4129
end
4230
end

test/absinthe/execution/subscription_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
215215

216216
assert %{
217217
event: "subscription:data",
218-
result: %{data: %{"thing" => "foo"}, ordinal: nil},
218+
result: %{data: %{"thing" => "foo"}},
219219
topic: topic
220220
} == msg
221221
end
@@ -259,7 +259,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
259259

260260
msg = %{
261261
event: "subscription:data",
262-
result: %{data: %{"multipleTopics" => "foo"}, ordinal: nil},
262+
result: %{data: %{"multipleTopics" => "foo"}},
263263
topic: topic
264264
}
265265

@@ -361,7 +361,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
361361

362362
assert %{
363363
event: "subscription:data",
364-
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}, ordinal: nil},
364+
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}},
365365
topic: topic
366366
} == msg
367367
end
@@ -409,7 +409,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
409409

410410
assert %{
411411
event: "subscription:data",
412-
result: %{data: %{"thing" => "foo"}, ordinal: nil},
412+
result: %{data: %{"thing" => "foo"}},
413413
topic: topic
414414
} == msg
415415
end
@@ -428,7 +428,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
428428

429429
assert %{
430430
event: "subscription:data",
431-
result: %{data: %{"thing" => "foo"}, ordinal: nil},
431+
result: %{data: %{"thing" => "foo"}},
432432
topic: topic
433433
} == msg
434434
end)
@@ -640,7 +640,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
640640

641641
assert %{
642642
event: "subscription:data",
643-
result: %{data: %{"thing" => "foo"}, ordinal: nil},
643+
result: %{data: %{"thing" => "foo"}},
644644
topic: topic
645645
} == msg
646646

0 commit comments

Comments
 (0)