Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the document only once when several field keys are supplied #1272

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/absinthe/phase/subscription/subscribe_self.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ defmodule Absinthe.Phase.Subscription.SubscribeSelf do
field_keys = get_field_keys(field, config)
subscription_id = get_subscription_id(config, blueprint, options)

for field_key <- field_keys,
do: Absinthe.Subscription.subscribe(pubsub, field_key, subscription_id, blueprint)
Absinthe.Subscription.subscribe(pubsub, field_keys, subscription_id, blueprint)

{:replace, blueprint,
[
Expand Down
16 changes: 11 additions & 5 deletions lib/absinthe/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,31 @@ defmodule Absinthe.Subscription do
defp fetch_fields(_, _), do: []

@doc false
def subscribe(pubsub, field_key, doc_id, doc) do
def subscribe(pubsub, field_keys, doc_id, doc) do
field_keys = List.wrap(field_keys)

registry = pubsub |> registry_name

doc_value = %{
initial_phases: PipelineSerializer.pack(doc.initial_phases),
source: doc.source
}

pdict_add_field(doc_id, field_key)
{:ok, _} = Registry.register(registry, field_key, doc_id)
pdict_add_fields(doc_id, field_keys)

for field_key <- field_keys do
{:ok, _} = Registry.register(registry, field_key, doc_id)
end

{:ok, _} = Registry.register(registry, doc_id, doc_value)
end

defp pdict_fields(doc_id) do
Process.get({__MODULE__, doc_id}, [])
end

defp pdict_add_field(doc_id, field) do
Process.put({__MODULE__, doc_id}, [field | pdict_fields(doc_id)])
defp pdict_add_fields(doc_id, field_keys) do
Process.put({__MODULE__, doc_id}, field_keys ++ pdict_fields(doc_id))
end

defp pdict_delete_fields(doc_id) do
Expand Down
Loading