Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support early-return of falsey values from ready/authorized #4585

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/graphql/schema/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,43 @@ def arguments
# @api private
def resolve_with_support(**args)
# First call the ready? hook which may raise
ready_val = if args.any?
raw_ready_val = if args.any?
ready?(**args)
else
ready?
end
context.query.after_lazy(ready_val) do |is_ready, ready_early_return|
if ready_early_return
context.query.after_lazy(raw_ready_val) do |ready_val|
if ready_val.is_a?(Array)
is_ready, ready_early_return = ready_val
if is_ready != false
raise "Unexpected result from #ready? (expected `true`, `false` or `[false, {...}]`): [#{is_ready.inspect}, #{ready_early_return.inspect}]"
else
ready_early_return
end
elsif is_ready
elsif ready_val
# Then call each prepare hook, which may return a different value
# for that argument, or may return a lazy object
load_arguments_val = load_arguments(args)
context.query.after_lazy(load_arguments_val) do |loaded_args|
@prepared_arguments = loaded_args
Schema::Validator.validate!(self.class.validators, object, context, loaded_args, as: @field)
# Then call `authorized?`, which may raise or may return a lazy object
authorized_val = if loaded_args.any?
raw_authorized_val = if loaded_args.any?
authorized?(**loaded_args)
else
authorized?
end
context.query.after_lazy(authorized_val) do |(authorized_result, early_return)|
context.query.after_lazy(raw_authorized_val) do |authorized_val|
# If the `authorized?` returned two values, `false, early_return`,
# then use the early return value instead of continuing
if early_return
if authorized_val.is_a?(Array)
authorized_result, early_return = authorized_val
if authorized_result == false
early_return
else
raise "Unexpected result from #authorized? (expected `true`, `false` or `[false, {...}]`): [#{authorized_result.inspect}, #{early_return.inspect}]"
end
elsif authorized_result
elsif authorized_val
# Finally, all the hooks have passed, so resolve it
if loaded_args.any?
public_send(self.class.resolve_method, **loaded_args)
Expand Down
38 changes: 38 additions & 0 deletions spec/graphql/schema/resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ def resolve(int:)
end
end

class ResolverWithFalseyValueReady < BaseResolver
type Boolean, null: true

def ready?
return false, false
end

def resolve
true
end
end

class ResolverWithInvalidReady < GraphQL::Schema::Mutation
argument :int, Integer
field :int, Integer
Expand Down Expand Up @@ -317,6 +329,18 @@ def authorized?(int1:, integer_2:)
end
end

class ResolverWithFalseyValueAuthorized < BaseResolver
type Boolean, null: true

def authorized?
return false, false
end

def resolve
true
end
end

class PrepResolver12 < GraphQL::Schema::Mutation
argument :int1, Integer
argument :int2, Integer
Expand Down Expand Up @@ -451,6 +475,8 @@ def resolve(*)
field :prep_resolver_12, resolver: PrepResolver12
field :prep_resolver_13, resolver: PrepResolver13
field :prep_resolver_14, resolver: PrepResolver14
field :resolver_with_falsey_value_ready, resolver: ResolverWithFalseyValueReady
field :resolver_with_falsey_value_authorized, resolver: ResolverWithFalseyValueAuthorized
field :resolver_with_auth_args, resolver: ResolverWithAuthArgs
field :resolver_with_error_handler, resolver: ResolverWithErrorHandler
end
Expand Down Expand Up @@ -655,6 +681,12 @@ def add_error_assertions(field_name, description)
assert_equal 213, res["data"]["int"]["int"]
end

it "can return false and falsey data" do
res = exec_query("{ result: resolverWithFalseyValueReady }")
assert_equal false, res["data"]["result"] # must be `false`, not just falsey
assert_nil res["errors"]
end

it 'raises the correct error on invalid return type' do
err = assert_raises(RuntimeError) do
exec_query("mutation { resolverWithInvalidReady(int: 2) { int } }")
Expand Down Expand Up @@ -719,6 +751,12 @@ def add_error_assertions(field_name, description)
assert_equal 7, res["data"]["prepResolver12"]["value"]
end

it "can return falsey data early" do
res = exec_query("{ result: resolverWithFalseyValueAuthorized }")
assert_equal false, res["data"]["result"] # must be actual `false`, not just a falsey `nil`
assert_nil res["errors"]
end

it "can return data early in a promise" do
# This is too big because it's modified in the overridden authorized? hook:
res = exec_query("{ prepResolver13(int1: 4, int2: 4) { errorMessages } }", context: { max_int: 9 })
Expand Down