Skip to content

Commit 1447c46

Browse files
committed
Move LoadApplicationObjectFailedError up
1 parent 268a784 commit 1447c46

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

lib/graphql.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ def self.scan_with_ragel(graphql_string)
106106
require "graphql/authorization"
107107
require "graphql/unauthorized_error"
108108
require "graphql/unauthorized_field_error"
109+
require "graphql/load_application_object_failed_error"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL
4+
# Raised when a argument is configured with `loads:` and the client provides an `ID`,
5+
# but no object is loaded for that ID.
6+
#
7+
# @see GraphQL::Schema::Member::HasArguments::ArgumentObjectLoader#load_application_object_failed, A hook which you can override in resolvers, mutations and input objects.
8+
class LoadApplicationObjectFailedError < GraphQL::ExecutionError
9+
# @return [GraphQL::Schema::Argument] the argument definition for the argument that was looked up
10+
attr_reader :argument
11+
# @return [String] The ID provided by the client
12+
attr_reader :id
13+
# @return [Object] The value found with this ID
14+
attr_reader :object
15+
def initialize(argument:, id:, object:)
16+
@id = id
17+
@argument = argument
18+
@object = object
19+
super("No object found for `#{argument.graphql_name}: #{id.inspect}`")
20+
end
21+
end
22+
end

lib/graphql/schema/member/has_arguments.rb

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,6 @@ def argument_class(new_arg_class = nil)
7777
end
7878

7979
module ArgumentObjectLoader
80-
class LoadApplicationObjectFailedError < GraphQL::ExecutionError
81-
# @return [GraphQL::Schema::Argument] the argument definition for the argument that was looked up
82-
attr_reader :argument
83-
# @return [String] The ID provided by the client
84-
attr_reader :id
85-
# @return [Object] The value found with this ID
86-
attr_reader :object
87-
def initialize(argument:, id:, object:)
88-
@id = id
89-
@argument = argument
90-
@object = object
91-
super("No object found for `#{argument.graphql_name}: #{id.inspect}`")
92-
end
93-
end
94-
9580
# Look up the corresponding object for a provided ID.
9681
# By default, it uses Relay-style {Schema.object_from_id},
9782
# override this to find objects another way.
@@ -108,7 +93,7 @@ def load_application_object(argument, lookup_as_type, id)
10893
loaded_application_object = object_from_id(lookup_as_type, id, context)
10994
context.schema.after_lazy(loaded_application_object) do |application_object|
11095
if application_object.nil?
111-
err = LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
96+
err = GraphQL::LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
11297
load_application_object_failed(err)
11398
end
11499
# Double-check that the located object is actually of this type
@@ -117,7 +102,7 @@ def load_application_object(argument, lookup_as_type, id)
117102
context.schema.after_lazy(resolved_application_object_type) do |application_object_type|
118103
possible_object_types = context.schema.possible_types(lookup_as_type)
119104
if !possible_object_types.include?(application_object_type)
120-
err = LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
105+
err = GraphQL::LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
121106
load_application_object_failed(err)
122107
else
123108
# This object was loaded successfully

lib/graphql/schema/resolver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,4 @@ def own_arguments_loads_as_type
300300
end
301301
end
302302
end
303-
end
303+
end

spec/graphql/schema/resolver_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def exec_query(*args)
485485

486486
res = exec_query(query_str)
487487
assert_nil res["data"].fetch("resolverWithErrorHandler")
488-
expected_err = "ResolverWithErrorHandler failed for id: \"failed_to_find\" (nil) (GraphQL::Schema::Member::HasArguments::ArgumentObjectLoader::LoadApplicationObjectFailedError)"
488+
expected_err = "ResolverWithErrorHandler failed for id: \"failed_to_find\" (nil) (GraphQL::LoadApplicationObjectFailedError)"
489489
assert_equal [expected_err], res["errors"].map { |e| e["message"] }
490490
end
491491
end
@@ -500,7 +500,7 @@ def exec_query(*args)
500500

501501
res = exec_query(query_str)
502502
assert_nil res["data"].fetch("resolverWithErrorHandler")
503-
expected_err = "ResolverWithErrorHandler failed for id: \"resolve_type_as_wrong_type\" (:resolve_type_as_wrong_type) (GraphQL::Schema::Member::HasArguments::ArgumentObjectLoader::LoadApplicationObjectFailedError)"
503+
expected_err = "ResolverWithErrorHandler failed for id: \"resolve_type_as_wrong_type\" (:resolve_type_as_wrong_type) (GraphQL::LoadApplicationObjectFailedError)"
504504
assert_equal [expected_err], res["errors"].map { |e| e["message"] }
505505
end
506506
end

0 commit comments

Comments
 (0)