diff --git a/lib/graphql/execution/resolvable.ex b/lib/graphql/execution/resolvable.ex index bb673f9..deb0297 100644 --- a/lib/graphql/execution/resolvable.ex +++ b/lib/graphql/execution/resolvable.ex @@ -8,7 +8,11 @@ end defmodule GraphQL.Execution.ResolveWrapper do def wrap(fun) do try do - {:ok, fun.()} + case fun.() do + {:ok, result} -> {:ok, result} + {:error, message} -> {:error, message} + result -> {:ok, result} + end rescue e in RuntimeError -> {:error, e.message} _ in FunctionClauseError -> {:error, "Could not find a resolve function for this query."} diff --git a/test/graphql/execution/executor_test.exs b/test/graphql/execution/executor_test.exs index 5c16a0f..dfaab74 100644 --- a/test/graphql/execution/executor_test.exs +++ b/test/graphql/execution/executor_test.exs @@ -245,6 +245,37 @@ defmodule GraphQL.Execution.Executor.ExecutorTest do assert_data(result, %{b: "B"}) end + test "resolvers can return value or raise error, or :ok/:error tuples" do + schema = Schema.new(%{ + query: %ObjectType{ + name: "Q", + fields: %{ + a: %{ + type: %String{}, + resolve: fn(_) -> "A" end + }, + b: %{ + type: %String{}, + resolve: fn(_) -> {:ok, "B"} end + }, + c: %{ + type: %String{}, + resolve: fn(_) -> raise "C" end + }, + d: %{ + type: %String{}, + resolve: fn(_) -> {:error, "D"} end + } + } + } + }) + + {:ok, result} = execute(schema,~S[query Q { a b c d }]) + assert_data(result, %{a: "A", b: "B", c: nil, d: nil}) + assert_has_error(result, %{message: "C"}) + assert_has_error(result, %{message: "D"}) + end + test "lists of things" do book = %ObjectType{ name: "Book",