Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from joshprice/revert-23-object-fields
Browse files Browse the repository at this point in the history
Revert "Added support for object fields"
  • Loading branch information
Josh Price committed Dec 1, 2015
2 parents 2fc6deb + d4c484f commit 7a8a19b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 63 deletions.
52 changes: 18 additions & 34 deletions lib/graphql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ defmodule GraphQL do
# {:ok, %{hello: "world"}}
"""

alias GraphQL.Schema
alias GraphQL.SyntaxError

defmodule ObjectType do
defstruct name: "RootQueryType", description: "", fields: []
end
Expand Down Expand Up @@ -93,47 +96,28 @@ defmodule GraphQL do
"""
def execute(schema, query) do
case parse(query) do
{:ok, document} -> execute_definition(hd(document[:definitions]), schema)
{:error, error} -> {:error, error}
end
end
{:ok, document} ->
query_fields = hd(document[:definitions])[:selectionSet][:selections]

defp execute_definition(%{operation: :query}=definition, schema) do
{:ok, Enum.map(definition[:selectionSet][:selections], fn(selection) -> execute_field(selection, schema.query) end)
|> Enum.filter(fn(item) -> item != nil end)
|> Enum.into(%{})}
end
%Schema{
query: _query_root = %ObjectType{
name: "RootQueryType",
fields: fields
}
} = schema

defp execute_field(%{kind: :Field, selectionSet: selection_set}=field, schema) do
fields = Enum.map(selection_set[:selections], fn(selection) ->
schema_fragment = Enum.find(schema.fields, fn(fd) -> fd.name == field[:name] end)
execute_field(selection, schema_fragment)
end)
result = for fd <- fields, qf <- query_fields, qf[:name] == fd.name do
arguments = Map.get(qf, :arguments, [])
|> Enum.map(&parse_argument/1)

fields = Enum.filter(fields, fn(item) -> item != nil end)
{String.to_atom(fd.name), fd.resolve.(arguments)}
end

if Enum.count(fields) > 0 do
{String.to_atom(field[:name]), Enum.into(fields, %{})}
else
nil
end

end

defp execute_field(%{kind: :Field}=field, schema) do
arguments = Map.get(field, :arguments, []) |> Enum.map(&parse_argument/1)
schema_fragment = Enum.find(schema.fields, fn(fd) -> fd.name == field[:name] end)
case resolve(schema_fragment, arguments) do
{:ok, value} -> {String.to_atom(field[:name]), value}
{:error, _} -> nil
{:ok, Enum.into(result, %{})}
{:error, error} -> {:error, error}
end
end

defp resolve(nil, _), do: {:error, nil}
defp resolve(field, arguments) do
{:ok, field.resolve.(arguments)}
end

defp parse_argument(%{kind: :Argument, loc: _, name: name, value: %{kind: _, loc: _, value: value}}) do
{String.to_atom(name), value}
end
Expand Down
1 change: 0 additions & 1 deletion lib/graphql/exceptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ defmodule GraphQL.SyntaxError do
"#{exception.errors} on line #{exception.line}"
end
end

28 changes: 0 additions & 28 deletions test/graphql_executor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ defmodule GraphqlExecutorTest do
name: "greeting",
type: "String",
resolve: &greeting/1,
},
%GraphQL.ObjectType{
name: "person",
fields: [
%GraphQL.FieldDefinition{
name: "name",
type: "String",
resolve: &person_name/1
}
]
}
]
}
Expand All @@ -34,8 +24,6 @@ defmodule GraphqlExecutorTest do

def greeting(name: name), do: "Hello, #{name}!"
def greeting(_), do: greeting(name: "world")

def person_name(_), do: "Nick"
end

test "basic query execution" do
Expand All @@ -48,22 +36,6 @@ defmodule GraphqlExecutorTest do
assert GraphQL.execute(TestSchema.schema, query) == {:ok, %{greeting: "Hello, Elixir!"}}
end

test "query object type fields" do
query = "{ greeting, person { name } }"
assert GraphQL.execute(TestSchema.schema, query) == {:ok, %{greeting: "Hello, world!", person: %{name: "Nick"} }}
end

test "query undefined field" do
query = "{ undefined }"
assert GraphQL.execute(TestSchema.schema, query) == {:ok, %{}}
end

test "query nested undefined field" do
query = "{ greeting, person { title } }"
assert GraphQL.execute(TestSchema.schema, query) == {:ok, %{greeting: "Hello, world!"}}
end



# test "simple selection set" do
#
Expand Down

0 comments on commit 7a8a19b

Please sign in to comment.