-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MOTOR-1353]: [Bburago] Support for auth0 (#49)
- Loading branch information
1 parent
15fd2b1
commit 2d6f3f9
Showing
8 changed files
with
136 additions
and
3 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
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,64 @@ | ||
defmodule BridgeEx.Extensions.ExternalResources do | ||
@moduledoc """ | ||
Preload a set of resources marking them as "external" and provide the related getter functions. | ||
## Options | ||
* `resources` (required): enumerable of resource names (atoms) and their paths (strings). | ||
Each path is assumed to be relative to the module directory. | ||
## Examples | ||
```elixir | ||
defmodule MyBridge do | ||
use BridgeEx.Extensions.ExternalResources, | ||
resources: [ | ||
my_query: "queries/query.graphql", | ||
my_mutation: "mutations/mutation.graphql" | ||
] | ||
# it generates the following code: | ||
@external_resource "\#{__DIR__}/queries/query.graphql" | ||
@external_resource "\#{__DIR__}/mutations/mutation.graphql" | ||
@spec my_query() :: String.t() | ||
def my_query, do: Map.fetch!(external_resources(), :my_query) | ||
@spec my_mutation() :: String.t() | ||
def my_mutation, do: Map.fetch!(external_resources(), :my_mutation) | ||
defp external_resources do | ||
%{ | ||
my_query: "contents of query.graphql", | ||
my_mutation: "contents of mutation.graphql" | ||
} | ||
end | ||
end | ||
``` | ||
""" | ||
|
||
defmacro __using__(resources: resources) do | ||
dir = Path.dirname(__CALLER__.file) | ||
resources = for {name, path} <- resources, do: {name, Path.join(dir, path)} | ||
contents = for {name, path} <- resources, into: %{}, do: {name, File.read!(path)} | ||
|
||
getters = | ||
for {name, _path} <- resources do | ||
quote do | ||
@spec unquote(name)() :: String.t() | ||
def unquote(name)(), do: Map.fetch!(external_resources(), unquote(name)) | ||
end | ||
end | ||
|
||
external_resource_attributes = | ||
for {_name, path} <- resources do | ||
quote do: @external_resource(unquote(path)) | ||
end | ||
|
||
quote generated: true do | ||
unquote_splicing(external_resource_attributes) | ||
unquote_splicing(getters) | ||
defp external_resources, do: unquote(Macro.escape(contents)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule BridgeEx.Extensions.ExternalResourcesTest do | ||
use ExUnit.Case | ||
|
||
defmodule Example do | ||
use BridgeEx.Extensions.ExternalResources, | ||
resources: [ | ||
question: "resources/question.txt", | ||
answer: "resources/answer.txt" | ||
] | ||
end | ||
|
||
test "it generates getter functions" do | ||
assert "How many roads must a man walk down?" == Example.question() | ||
assert "42" == Example.answer() | ||
end | ||
|
||
test "it marks files as external resources" do | ||
paths = | ||
for {:external_resource, [path]} <- Example.__info__(:attributes), | ||
into: MapSet.new(), | ||
do: Path.relative_to(path, __DIR__) | ||
|
||
assert MapSet.new(["resources/question.txt", "resources/answer.txt"]) == paths | ||
end | ||
|
||
test "it fails to compile if no resources are given" do | ||
quoted = | ||
quote do | ||
use BridgeEx.Extensions.ExternalResources | ||
end | ||
|
||
assert_raise FunctionClauseError, fn -> Code.eval_quoted(quoted) end | ||
end | ||
|
||
test "it fails to compile if a file is missing" do | ||
quoted = | ||
quote do | ||
use BridgeEx.Extensions.ExternalResources, | ||
resources: [ | ||
missing: "missing.txt" | ||
] | ||
end | ||
|
||
assert_raise File.Error, fn -> Code.eval_quoted(quoted) 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
42 |
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 @@ | ||
How many roads must a man walk down? |