Skip to content

Commit

Permalink
Support recursive schema references by allowing quoted fields
Browse files Browse the repository at this point in the history
As reported in Issue graphql-elixir#38, Schemas weren't able to recursively refer to
one another. By quoting the offending references and only evaling them
during Execution, we can resolve everything on-demand.
  • Loading branch information
Mark Olson committed Jan 7, 2016
1 parent 0eccd5f commit 521f5a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/graphql/execution/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ defmodule GraphQL.Execution.Executor do
end
end

defp maybe_unwrap(item) when is_tuple(item) do
{result,_} = Code.eval_quoted(item)
result
end
defp maybe_unwrap(item), do: item

defp field_definition(_schema, parent_type, field_name) do
# TODO deal with introspection
parent_type.fields[field_name]
maybe_unwrap(parent_type.fields)[field_name]
end

defp argument_values(arg_defs, arg_asts, variable_values) do
Expand Down
30 changes: 30 additions & 0 deletions test/graphql/execution/executor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ defmodule GraphQL.Execution.Executor.ExecutorTest do
alias GraphQL.Execution.Executor

defmodule TestSchema do
def recursive_schema do
%GraphQL.Schema{
query: %GraphQL.ObjectType{
name: "Recursive1",
fields: quote do %{
id: %{type: "Integer", resolve: 1},
name: %{type: "String", resolve: "Mark"},
b: %{type: TestSchema.recursive_schema.query },
c: %{type: TestSchema.recursive_schema_2 }
} end
}
}
end

def recursive_schema_2 do
%GraphQL.ObjectType{
name: "Recursive2",
fields: quote do %{
id: %{type: "Integer", resolve: 2},
name: %{type: "String", resolve: "Kate"},
b: %{type: TestSchema.recursive_schema.query }
} end
}
end

def schema do
%GraphQL.Schema{
query: %GraphQL.ObjectType{
Expand Down Expand Up @@ -73,6 +98,11 @@ defmodule GraphQL.Execution.Executor.ExecutorTest do
assert_execute {"{ a }", TestSchema.schema}, %{}
end

test "Quoted fields are available" do
assert_execute({"{id, b { name, c{ id, name, b { name }}}}", TestSchema.recursive_schema},
%{id: 1, b: %{name: "Mark", c: %{id: 2, name: "Kate", b: %{name: "Mark"}}}})
end

test "simple selection set" do
schema = %GraphQL.Schema{
query: %GraphQL.ObjectType{
Expand Down

0 comments on commit 521f5a9

Please sign in to comment.