-
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
Jun 19, 2018
1 parent
36fa631
commit 2a13b40
Showing
14 changed files
with
463 additions
and
13 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
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
defmodule Absinthe.Phase.Document.Execution.DeferredResolution do | ||
alias Absinthe.{Blueprint, Phase} | ||
alias Absinthe.Phase.Document.Execution.Resolution | ||
alias Absinthe.Resolution.DeferredField | ||
|
||
use Absinthe.Phase | ||
|
||
@spec run(Blueprint.t(), Keyword.t()) :: Phase.result_t() | ||
def run(blueprint, options \\ []) | ||
|
||
def run(%Blueprint{execution: %{deferred_fields: []}} = blueprint, _options) do | ||
{:ok, blueprint} | ||
end | ||
|
||
def run(%Blueprint{execution: %{deferred_fields: fields}} = blueprint, _options) do | ||
context = blueprint.execution.context | ||
pubsub = ensure_pubsub!(context) | ||
|
||
topic = "__absinthe__:deferred_fields-" <> to_string(:erlang.unique_integer([:positive])) | ||
pubsub.subscribe(topic) | ||
|
||
self = self() | ||
|
||
Task.start(fn -> do_defers(fields, blueprint, pubsub, topic, self) end) | ||
|
||
{:ok, put_in(blueprint.execution.defer_topic, topic)} | ||
end | ||
|
||
defp do_defers(fields, blueprint, pubsub, topic, pid) do | ||
fields | ||
|> Enum.map(fn f -> Task.async(fn -> resolve_field(f, blueprint, pubsub, topic) end) end) | ||
|> Task.yield_many(Application.get_env(:absinthe, :max_defer_time, 60000)) | ||
|> Enum.map(fn {task, res} -> | ||
# Shutdown the tasks that did not reply nor exit | ||
res || Task.shutdown(task) | ||
end) | ||
|
||
pubsub.unsubscribe(topic, pid) | ||
end | ||
|
||
defp resolve_field(f, blueprint, pubsub, topic) do | ||
{result, _exec} = perform_deferred_resolution(f) | ||
|
||
data = | ||
blueprint.execution.result | ||
|> put_in(result) | ||
|> Absinthe.Phase.Document.Result.process() | ||
|> add_path(f.path) | ||
|
||
pubsub.publish_deferred(topic, data) | ||
end | ||
|
||
defp perform_deferred_resolution(%DeferredField{} = field) do | ||
Resolution.do_resolve_field( | ||
field.resolution, | ||
%{field.exec | deferring: true}, | ||
field.source, | ||
field.path | ||
) | ||
end | ||
|
||
defp add_path(result, path) do | ||
Map.put(result, :path, Enum.reverse(make_path(path))) | ||
end | ||
|
||
defp make_path(path) do | ||
path | ||
|> Enum.map(&to_path_field/1) | ||
|> Enum.filter(fn e -> e != nil end) | ||
end | ||
|
||
defp to_path_field(index) when is_integer(index), do: index | ||
defp to_path_field(%{name: name}), do: name | ||
|
||
defp ensure_pubsub!(context) do | ||
case Absinthe.Subscription.extract_pubsub(context) do | ||
{:ok, pubsub} -> | ||
pubsub | ||
|
||
_ -> | ||
raise """ | ||
Pubsub not configured! | ||
Deferred fields require a configured pubsub module. | ||
""" | ||
end | ||
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
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.Resolution.DeferredField do | ||
|
||
defstruct [ | ||
:field, | ||
:resolution, | ||
:exec, | ||
:source, | ||
:parent_type, | ||
:path | ||
] | ||
|
||
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.