-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bernard Duggan
committed
Apr 15, 2019
1 parent
37c43d3
commit 1a57e68
Showing
15 changed files
with
481 additions
and
16 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Absinthe.Blueprint.Continuation do | ||
@moduledoc false | ||
|
||
# Continuations allow further resolutions after the initial result is | ||
# returned | ||
|
||
alias Absinthe.Pipeline | ||
|
||
defstruct [ | ||
:phase_input, | ||
:pipeline | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
phase_input: Pipeline.data_t, | ||
pipeline: Pipeline.t() | ||
} | ||
|
||
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Absinthe.Middleware.Defer do | ||
@moduledoc false | ||
|
||
# Suspends deferred fields so that they are not immediately processed | ||
|
||
@behaviour Absinthe.Middleware | ||
|
||
def call(%{state: :unresolved} = res, _), | ||
do: %{res | state: :suspended, acc: Map.put(res.acc, :deferred_res, res)} | ||
|
||
def call(res, _), do: res | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
defmodule Absinthe.Phase.Document.Execution.DeferFields do | ||
@moduledoc false | ||
|
||
# Strips out deferred fields from the current result and places them | ||
# in continuations. | ||
|
||
alias Absinthe.Phase.Document.Execution.Resolution | ||
alias Absinthe.{Blueprint, Phase, Resolution} | ||
alias Blueprint.Continuation | ||
alias Blueprint.Result.List | ||
|
||
use Absinthe.Phase | ||
|
||
@spec run(Blueprint.t(), Keyword.t()) :: Phase.result_t() | ||
def run(bp_root, _options \\ []) do | ||
result = strip_deferred(bp_root, bp_root.execution.result) | ||
|
||
{:ok, %{bp_root | execution: %{bp_root.execution | result: result}}} | ||
end | ||
|
||
defp strip_deferred(bp_root, %{fields: _} = object) do | ||
strip_nested(bp_root, object, :fields) | ||
end | ||
|
||
defp strip_deferred(bp_root, %List{} = object) do | ||
strip_nested(bp_root, object, :values) | ||
end | ||
|
||
defp strip_deferred(bp_root, fields) when is_list(fields) do | ||
fields | ||
|> Enum.reduce( | ||
{[], []}, | ||
fn f, acc -> do_strip_deferred(bp_root, f, acc) end | ||
) | ||
end | ||
|
||
defp strip_deferred(_bp_root, other), do: other | ||
|
||
defp strip_nested(bp_root, object, sub_object_field) do | ||
{continuations, remaining} = strip_deferred(bp_root, Map.get(object, sub_object_field)) | ||
|
||
object | ||
|> Map.put(sub_object_field, Enum.reverse(remaining)) | ||
|> Map.put(:continuations, object.continuations ++ Enum.reverse(continuations)) | ||
end | ||
|
||
defp do_strip_deferred( | ||
bp_root, | ||
%Resolution{state: :suspended, acc: %{deferred_res: res}}, | ||
{deferred, remaining} | ||
) do | ||
continuation = %Continuation{ | ||
phase_input: %{ | ||
resolution: %{res | state: :unresolved}, | ||
execution: bp_root.execution | ||
}, | ||
pipeline: [ | ||
Phase.Document.Execution.DeferredResolution, | ||
Phase.Document.Execution.DeferFields, | ||
Phase.Document.Result | ||
] | ||
} | ||
|
||
{[continuation | deferred], remaining} | ||
end | ||
|
||
defp do_strip_deferred(_bp_root, %Resolution{} = r, {deferred, remaining}) do | ||
{deferred, [r | remaining]} | ||
end | ||
|
||
defp do_strip_deferred(bp_root, %{fields: _} = object, acc) do | ||
do_strip_nested(bp_root, object, :fields, acc) | ||
end | ||
|
||
defp do_strip_deferred(bp_root, %List{} = object, acc) do | ||
do_strip_nested(bp_root, object, :values, acc) | ||
end | ||
|
||
defp do_strip_deferred(_bp_root, object, {deferred, remaining}) do | ||
{deferred, [object | remaining]} | ||
end | ||
|
||
defp do_strip_nested(bp_root, object, sub_object_field, {deferred, remaining}) do | ||
{d, r} = strip_deferred(bp_root, Map.get(object, sub_object_field)) | ||
object = Map.put(object, sub_object_field, Enum.reverse(r)) | ||
{d ++ deferred, [object | remaining]} | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
lib/absinthe/phase/document/execution/deferred_resolution.ex
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Absinthe.Phase.Document.Execution.DeferredResolution do | ||
@moduledoc false | ||
|
||
# Perform resolution on previously deferred fields | ||
|
||
alias Absinthe.{Blueprint, Phase} | ||
alias Phase.Document.Execution.Resolution | ||
|
||
use Absinthe.Phase | ||
|
||
@spec run(map(), Keyword.t()) :: Phase.result_t() | ||
def run(input, _options \\ []) do | ||
{:ok, resolve_field(input)} | ||
end | ||
|
||
defp resolve_field(input) do | ||
{result, _} = perform_deferred_resolution(input) | ||
|
||
%Blueprint{ | ||
execution: %{input.execution | result: result}, | ||
result: %{path: make_path(input.resolution.path)}} | ||
end | ||
|
||
defp perform_deferred_resolution(input) do | ||
# Perform resolution using the standard resolver pipeline functionality | ||
Resolution.do_resolve_field( | ||
input.resolution, | ||
input.resolution.source, | ||
input.resolution.path | ||
) | ||
end | ||
|
||
defp make_path(path) do | ||
path | ||
|> Enum.map(&to_path_field/1) | ||
|> Enum.filter(fn e -> e != nil end) | ||
|> Enum.reverse() | ||
end | ||
|
||
defp to_path_field(index) when is_integer(index), do: index | ||
defp to_path_field(%{name: name}), do: name | ||
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
Oops, something went wrong.