-
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.
Add support for interface implementing other interfaces (#1012)
* Add support in parser for interfaces implementing interfaces * Walk interfaces on interfaces in AST * Validate interface implementations on interfaces * Add interfaces on interfaces support in macro schema * Add interface info on interface type * Add validation disallowing interface cycles * Add no interface cycle validation to schema pipeline * Test SDL interfaces * Add interface implements to sdl render * Improve tests for interfaces implementing interfaces * Fix comment
- Loading branch information
1 parent
b4e946c
commit c707638
Showing
15 changed files
with
277 additions
and
12 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
78 changes: 78 additions & 0 deletions
78
lib/absinthe/phase/schema/validation/no_interface_cycles.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,78 @@ | ||
defmodule Absinthe.Phase.Schema.Validation.NoInterfaceCyles do | ||
@moduledoc false | ||
|
||
use Absinthe.Phase | ||
alias Absinthe.Blueprint | ||
alias Absinthe.Blueprint.Schema | ||
|
||
def run(blueprint, _opts) do | ||
blueprint = check(blueprint) | ||
|
||
{:ok, blueprint} | ||
end | ||
|
||
defp check(blueprint) do | ||
graph = :digraph.new([:cyclic]) | ||
|
||
try do | ||
_ = build_interface_graph(blueprint, graph) | ||
|
||
Blueprint.prewalk(blueprint, &validate_schema(&1, graph)) | ||
after | ||
:digraph.delete(graph) | ||
end | ||
end | ||
|
||
defp validate_schema(%Schema.InterfaceTypeDefinition{} = interface, graph) do | ||
if cycle = :digraph.get_cycle(graph, interface.identifier) do | ||
interface |> put_error(error(interface, cycle)) | ||
else | ||
interface | ||
end | ||
end | ||
|
||
defp validate_schema(node, _graph) do | ||
node | ||
end | ||
|
||
defp build_interface_graph(blueprint, graph) do | ||
_ = Blueprint.prewalk(blueprint, &vertex(&1, graph)) | ||
end | ||
|
||
defp vertex(%Schema.InterfaceTypeDefinition{} = implementor, graph) do | ||
:digraph.add_vertex(graph, implementor.identifier) | ||
|
||
for interface <- implementor.interfaces do | ||
edge(implementor, interface, graph) | ||
end | ||
|
||
implementor | ||
end | ||
|
||
defp vertex(implementor, _graph) do | ||
implementor | ||
end | ||
|
||
# Add an edge, modeling the relationship between two interfaces | ||
defp edge(implementor, interface, graph) do | ||
:digraph.add_vertex(graph, interface) | ||
|
||
:digraph.add_edge(graph, implementor.identifier, interface) | ||
|
||
true | ||
end | ||
|
||
defp error(type, deps) do | ||
%Absinthe.Phase.Error{ | ||
message: | ||
String.trim(""" | ||
Interface Cycle Error | ||
Interface `#{type.identifier}' forms a cycle via: (#{inspect(deps)}) | ||
"""), | ||
locations: [type.__reference__.location], | ||
phase: __MODULE__, | ||
extra: type.identifier | ||
} | ||
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
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,36 @@ | ||
defmodule Absinthe.Schema.Rule.NoInterfacecyclesTest do | ||
use Absinthe.Case, async: true | ||
|
||
describe "rule" do | ||
test "is enforced" do | ||
assert_schema_error("interface_cycle_schema", [ | ||
%{ | ||
extra: :named, | ||
locations: [ | ||
%{ | ||
file: "test/support/fixtures/dynamic/interface_cycle_schema.exs", | ||
line: 24 | ||
} | ||
], | ||
message: | ||
"Interface Cycle Error\n\nInterface `named' forms a cycle via: ([:named, :node, :named])", | ||
path: [], | ||
phase: Absinthe.Phase.Schema.Validation.NoInterfaceCyles | ||
}, | ||
%{ | ||
extra: :node, | ||
locations: [ | ||
%{ | ||
file: "test/support/fixtures/dynamic/interface_cycle_schema.exs", | ||
line: 24 | ||
} | ||
], | ||
message: | ||
"Interface Cycle Error\n\nInterface `node' forms a cycle via: ([:node, :named, :node])", | ||
path: [], | ||
phase: Absinthe.Phase.Schema.Validation.NoInterfaceCyles | ||
} | ||
]) | ||
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
Oops, something went wrong.