-
Notifications
You must be signed in to change notification settings - Fork 0
[GRAPH-1102] Customizable doc storage with default implementation #10
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
Open
bryanjos
wants to merge
15
commits into
main
Choose a base branch
from
bryanj/GRAPH-1102/customizable_document_storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5d76177
[GRAPH-1102] Customizable doc storage with default implementation
bryanjos 7cc430c
Fix dialyzer error
bryanjos e2f05c4
Some refactoring
bryanjos 44ea718
Fix code error caught by dialzyer
bryanjos f4db5d9
telemetry
bryanjos bec0e4a
split up operations
bryanjos bfc4657
change telemetry id
bryanjos 583c46f
Refactoring
bryanjos 6e1255f
Refactoring
bryanjos 10a17ae
Update documentation
bryanjos 6f8dbdd
Add wrapper functions in DocumentStorage to add telemetry
bryanjos 079e3c5
Documentation update
bryanjos 12979d7
Update documentation
bryanjos 793ed69
Let user start their own storage if needed
bryanjos a55d6a3
Update docs
bryanjos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
| defmodule Absinthe.Subscription.DefaultDocumentStorage do | ||
| @behaviour Absinthe.Subscription.DocumentStorage | ||
|
|
||
| @moduledoc """ | ||
| Default document storage for Absinthe. Stores subscription | ||
| documents and field keys in a Registry process. | ||
| """ | ||
|
|
||
| @impl Absinthe.Subscription.DocumentStorage | ||
| def child_spec(opts) do | ||
| Registry.child_spec(opts) | ||
| end | ||
|
|
||
| @impl Absinthe.Subscription.DocumentStorage | ||
| def put(storage_process_name, doc_id, doc_value, field_keys) do | ||
| pdict_add_fields(doc_id, field_keys) | ||
|
|
||
| for field_key <- field_keys do | ||
| {:ok, _} = Registry.register(storage_process_name, field_key, doc_id) | ||
| end | ||
|
|
||
| {:ok, _} = Registry.register(storage_process_name, doc_id, doc_value) | ||
| end | ||
|
|
||
| @impl Absinthe.Subscription.DocumentStorage | ||
| def delete(storage_process_name, doc_id) do | ||
| for field_key <- pdict_fields(doc_id) do | ||
| Registry.unregister(storage_process_name, field_key) | ||
| end | ||
|
|
||
| pdict_delete_fields(doc_id) | ||
|
|
||
| Registry.unregister(storage_process_name, doc_id) | ||
|
|
||
| :ok | ||
| end | ||
|
|
||
| @impl Absinthe.Subscription.DocumentStorage | ||
| def get_docs_by_field_key(storage_process_name, field_key) do | ||
| storage_process_name | ||
| |> Registry.lookup(field_key) | ||
| |> MapSet.new(fn {_pid, doc_id} -> doc_id end) | ||
| |> Enum.reduce(%{}, fn doc_id, acc -> | ||
| case Registry.lookup(storage_process_name, doc_id) do | ||
| [] -> | ||
| acc | ||
|
|
||
| [{_pid, doc} | _rest] -> | ||
| Map.put_new(acc, doc_id, doc) | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| defp pdict_fields(doc_id) do | ||
| Process.get({__MODULE__, doc_id}, []) | ||
| end | ||
|
|
||
| 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 | ||
| Process.delete({__MODULE__, doc_id}) | ||
| end | ||
| end | ||
This file contains hidden or 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,44 @@ | ||
| defmodule Absinthe.Subscription.DocumentStorage do | ||
| @moduledoc """ | ||
| Behaviour for storing subscription documents. Used to tell | ||
| Absinthe how to store documents and the field keys associated with those | ||
| documents. | ||
| """ | ||
|
|
||
| @doc """ | ||
| Child spec to determine how to start the | ||
| Document storage process. This will be supervised. Absinthe will give | ||
| the process a name and that name will be passed in the other callbacks | ||
| in order to reference it there. | ||
| """ | ||
| @callback child_spec(opts :: Keyword.t()) :: Supervisor.child_spec() | ||
|
|
||
| @doc """ | ||
| Adds `doc` to storage with `doc_id` as the key. Associates the given | ||
| `field_keys` with `doc_id`. | ||
| """ | ||
| @callback put( | ||
| storage_process_name :: atom, | ||
| doc_id :: term, | ||
| doc :: %{ | ||
| initial_phases: Absinthe.Subscription.PipelineSerializer.packed_pipeline(), | ||
| source: binary() | ||
| }, | ||
| field_keys :: [{field :: term, key :: term}] | ||
| ) :: | ||
| {:ok, term} | {:error, :reason} | ||
|
|
||
| @doc """ | ||
| Removes the document. Along with any field_keys associated with it | ||
| """ | ||
| @callback delete(storage_process_name :: atom, doc_id :: term) :: :ok | ||
|
|
||
| @doc """ | ||
| Get all docs associated with `field_key` | ||
| """ | ||
| @callback get_docs_by_field_key( | ||
| storage_process_name :: atom, | ||
| field_key :: {field :: term, key :: term} | ||
| ) :: | ||
| map() | ||
| end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.