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

Commit 6db6d6e

Browse files
author
Josh Price
committed
Allow resolve fns to be {mod, fun, args} tuples
1 parent c61a645 commit 6db6d6e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/graphql/execution/executor.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ defmodule GraphQL.Execution.Executor do
7878
field_def = field_definition(context.schema, parent_type, field_name)
7979
return_type = field_def.type
8080

81-
resolve_fn = Map.get(field_def, :resolve, &default_resolve_fn/3)
8281
args = argument_values(Map.get(field_def, :args, %{}), Map.get(field_ast, :arguments, %{}), context.variable_values)
8382
info = %{
8483
field_name: field_name,
@@ -91,7 +90,13 @@ defmodule GraphQL.Execution.Executor do
9190
operation: context.operation,
9291
variable_values: context.variable_values
9392
}
94-
result = resolve_fn.(source, args, info)
93+
resolve_fn = Map.get(field_def, :resolve, &default_resolve_fn/3)
94+
result = case resolve_fn do
95+
{mod, fun} -> apply(mod, fun, [source, args, info])
96+
{mod, fun, _} -> apply(mod, fun, [source, args, info])
97+
resolve when is_function(resolve) -> resolve.(source, args, info)
98+
_ -> {:error, "Resolve function must be a {module, function} tuple (or a lambda)"}
99+
end
95100
complete_value_catching_error(context, return_type, field_asts, info, result)
96101
end
97102

test/graphql/execution/executor_test.exs

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ defmodule GraphQL.Execution.Executor.ExecutorTest do
4545
assert_execute {~S[{ greeting(name: "Elixir") }], TestSchema.schema}, %{"greeting" => "Hello, Elixir!"}
4646
end
4747

48+
test "allow {module, function, args} style of resolve" do
49+
schema = %GraphQL.Schema{
50+
query: %GraphQL.ObjectType{
51+
name: "Q",
52+
fields: %{ g: %{ type: "String", resolve: {TestSchema, :greeting, []} }}
53+
}
54+
}
55+
assert_execute {"query Q { g }", schema}, %{"g" => "Hello, world!"}
56+
end
57+
4858
test "simple selection set" do
4959
schema = %GraphQL.Schema{
5060
query: %GraphQL.ObjectType{

0 commit comments

Comments
 (0)