-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer call to prime function until continuation
This allows the caller to complete their pubsub call before the prime function, avoiding data races.
- Loading branch information
Showing
5 changed files
with
79 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,46 @@ | ||
defmodule Absinthe.Phase.Subscription.Prime do | ||
@moduledoc false | ||
|
||
alias Absinthe.Blueprint.Continuation | ||
alias Absinthe.Phase | ||
|
||
@spec run(any(), Keyword.t()) :: Absinthe.Phase.result_t() | ||
def run(blueprint, prime_result: cr) do | ||
{:ok, put_in(blueprint.execution.root_value, cr)} | ||
def run(blueprint, prime_result: prime_result) do | ||
{:ok, put_in(blueprint.execution.root_value, prime_result)} | ||
end | ||
|
||
def run(blueprint, prime_fun: prime_fun, resolution_options: options) do | ||
{:ok, prime_results} = prime_fun.(blueprint.execution) | ||
|
||
case prime_results do | ||
[first | rest] -> | ||
blueprint = put_in(blueprint.execution.root_value, first) | ||
blueprint = maybe_add_continuations(blueprint, rest, options) | ||
{:ok, blueprint} | ||
|
||
[] -> | ||
blueprint = put_in(blueprint.result, :no_more_results) | ||
{:replace, blueprint, []} | ||
end | ||
end | ||
|
||
defp maybe_add_continuations(blueprint, [], _options), do: blueprint | ||
|
||
defp maybe_add_continuations(blueprint, remaining_results, options) do | ||
continuations = | ||
Enum.map( | ||
remaining_results, | ||
&%Continuation{ | ||
phase_input: blueprint, | ||
pipeline: [ | ||
{__MODULE__, [prime_result: &1]}, | ||
{Phase.Document.Execution.Resolution, options}, | ||
Phase.Subscription.GetOrdinal, | ||
Phase.Document.Result | ||
] | ||
} | ||
) | ||
|
||
put_in(blueprint.result, %{continuation: continuations}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters