-
Notifications
You must be signed in to change notification settings - Fork 89
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
Implement overridable global ID translator #93
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
785352b
Merge pull request #1 from absinthe-graphql/master
avitex 19cedce
Implement global_id_translator
avitex b7d9888
Fix environment config
avitex 137fd6f
Document default implementation
avitex ab5421a
Provide convenience wrapper functions
avitex 0cf86f2
Document behaviour
avitex f1e08e4
Piggyback off parse_ids test
avitex dc7d3f5
Improve typespecs and backwards compatibility
avitex 7ee9b4e
Document `from_global_id!/2` and `to_global_id!/3`
avitex b642193
Document configuration methods
avitex d155cf8
Fix grammar
avitex 9f0bef7
Refactor ID Translator
avitex d7a0a93
Backwards compatibility for v1.4
avitex 1169387
Remove to_global_id!/2 and from_global_id!/2
avitex ab0ca11
Merge upstream and fix conflict
avitex 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 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,77 @@ | ||
defmodule Absinthe.Relay.Node.IDTranslator do | ||
@moduledoc """ | ||
An ID translator handles encoding and decoding a global ID | ||
used in a Relay node. | ||
|
||
This module provides the behaviour for implementing an ID Translator. | ||
An example use case of this module would be a translator that encrypts the | ||
global ID. | ||
|
||
To use an ID Translator in your schema there are two methods. | ||
|
||
#### Inline Config | ||
``` | ||
defmodule MyApp.Schema do | ||
use Absinthe.Schema | ||
use Absinthe.Relay.Schema, [ | ||
flavor: :modern, | ||
global_id_translator: MyApp.Absinthe.IDTranslator | ||
] | ||
|
||
# ... | ||
|
||
end | ||
``` | ||
|
||
#### Mix Config | ||
``` | ||
config Absinthe.Relay, MyApp.Schema, | ||
global_id_translator: MyApp.Absinthe.IDTranslator | ||
``` | ||
|
||
## Example ID Translator | ||
|
||
A basic example that encodes the global ID by joining the `type_name` and | ||
`source_id` with `":"`. | ||
|
||
``` | ||
defmodule MyApp.Absinthe.IDTranslator do | ||
@behaviour Absinthe.Relay.Node.IDTranslator | ||
|
||
def to_global_id(type_name, source_id, _schema) do | ||
{:ok, "\#{type_name}:\#{source_id}"} | ||
end | ||
|
||
def from_global_id(global_id, _schema) do | ||
case String.split(global_id, ":", parts: 2) do | ||
[type_name, source_id] -> | ||
{:ok, type_name, source_id} | ||
_ -> | ||
{:error, "Could not extract value from ID `\#{inspect global_id}`"} | ||
end | ||
end | ||
end | ||
``` | ||
""" | ||
|
||
@doc """ | ||
Converts a node's type name and ID to a globally unique ID. | ||
|
||
Returns `{:ok, global_id}` on success. | ||
|
||
Returns `{:error, binary}` on failure. | ||
""" | ||
@callback to_global_id(type_name :: binary, source_id :: binary | integer, schema :: Absinthe.Schema.t) :: | ||
{:ok, global_id :: Absinthe.Relay.Node.global_id} | {:error, binary} | ||
|
||
@doc """ | ||
Converts a globally unique ID to a node's type name and ID. | ||
|
||
Returns `{:ok, type_name, source_id}` on success. | ||
|
||
Returns `{:error, binary}` on failure. | ||
""" | ||
@callback from_global_id(global_id :: Absinthe.Relay.Node.global_id, schema :: Absinthe.Schema.t | nil) :: | ||
{:ok, type_name :: binary, source_id :: binary} | {:error, binary} | ||
|
||
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,27 @@ | ||
defmodule Absinthe.Relay.Node.IDTranslator.Base64 do | ||
@behaviour Absinthe.Relay.Node.IDTranslator | ||
|
||
@moduledoc """ | ||
A basic implementation of `Absinthe.Relay.Node.IDTranslator` using Base64 encoding. | ||
""" | ||
|
||
@impl true | ||
def to_global_id(type_name, source_id, _schema) do | ||
{:ok, Base.encode64("#{type_name}:#{source_id}")} | ||
end | ||
|
||
@impl true | ||
def from_global_id(global_id, _schema) do | ||
case Base.decode64(global_id) do | ||
{:ok, decoded} -> | ||
case String.split(decoded, ":", parts: 2) do | ||
[type_name, source_id] when byte_size(type_name) > 0 and byte_size(source_id) > 0 -> | ||
{:ok, type_name, source_id} | ||
_ -> | ||
{:error, "Could not extract value from decoded ID `#{inspect decoded}`"} | ||
end | ||
:error -> | ||
{:error, "Could not decode ID value `#{global_id}'"} | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callback docs?