graphql 1.9.0 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql/schema/resolver.rb +4 -5
- data/lib/graphql/version.rb +1 -1
- data/spec/graphql/schema/resolver_spec.rb +79 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31138f73214b24b895b8717ef50dcc39c61c6e45
|
4
|
+
data.tar.gz: 1c105034b3b21b0dd732e1143acbf54538fc54ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc6e63617dc34dd096a3b279ec3f2a2f476f3a8032bfcf4008bccc9e6c3f1c3f45e23d0d43e2dc0b7798376424e2f196ee71e65cd365cb89c9384a89b6e1c39e
|
7
|
+
data.tar.gz: 07cbfa84015057a980b90820470e721352db0f42f85ab985312e01787328f0fef448ac6d774e08a625c88388246f5276352da72514be18f1e24c6468f5438f9d
|
@@ -201,7 +201,8 @@ module GraphQL
|
|
201
201
|
loaded_application_object = object_from_id(lookup_as_type, id, context)
|
202
202
|
context.schema.after_lazy(loaded_application_object) do |application_object|
|
203
203
|
if application_object.nil?
|
204
|
-
|
204
|
+
err = LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
|
205
|
+
load_application_object_failed(err)
|
205
206
|
end
|
206
207
|
# Double-check that the located object is actually of this type
|
207
208
|
# (Don't want to allow arbitrary access to objects this way)
|
@@ -209,7 +210,8 @@ module GraphQL
|
|
209
210
|
context.schema.after_lazy(resolved_application_object_type) do |application_object_type|
|
210
211
|
possible_object_types = context.schema.possible_types(lookup_as_type)
|
211
212
|
if !possible_object_types.include?(application_object_type)
|
212
|
-
|
213
|
+
err = LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
|
214
|
+
load_application_object_failed(err)
|
213
215
|
else
|
214
216
|
# This object was loaded successfully
|
215
217
|
# and resolved to the right type,
|
@@ -232,9 +234,6 @@ module GraphQL
|
|
232
234
|
end
|
233
235
|
end
|
234
236
|
end
|
235
|
-
rescue LoadApplicationObjectFailedError => err
|
236
|
-
# pass it to a handler
|
237
|
-
load_application_object_failed(err)
|
238
237
|
end
|
239
238
|
|
240
239
|
def load_application_object_failed(err)
|
data/lib/graphql/version.rb
CHANGED
@@ -171,11 +171,15 @@ describe GraphQL::Schema::Resolver do
|
|
171
171
|
include GraphQL::Schema::Interface
|
172
172
|
field :value, Integer, null: false
|
173
173
|
def self.resolve_type(obj, ctx)
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
174
|
+
LazyBlock.new {
|
175
|
+
if obj.is_a?(Integer)
|
176
|
+
IntegerWrapper
|
177
|
+
elsif obj == :resolve_type_as_wrong_type
|
178
|
+
GraphQL::Types::String
|
179
|
+
else
|
180
|
+
raise "Unexpected: #{obj.inspect}"
|
181
|
+
end
|
182
|
+
}
|
179
183
|
end
|
180
184
|
end
|
181
185
|
|
@@ -218,6 +222,30 @@ describe GraphQL::Schema::Resolver do
|
|
218
222
|
end
|
219
223
|
end
|
220
224
|
|
225
|
+
class ResolverWithErrorHandler < BaseResolver
|
226
|
+
argument :int, ID, required: true, loads: HasValue
|
227
|
+
type HasValue, null: true
|
228
|
+
def object_from_id(type, id, ctx)
|
229
|
+
LazyBlock.new {
|
230
|
+
if id == "failed_to_find"
|
231
|
+
nil
|
232
|
+
elsif id == "resolve_type_as_wrong_type"
|
233
|
+
:resolve_type_as_wrong_type
|
234
|
+
else
|
235
|
+
id.length
|
236
|
+
end
|
237
|
+
}
|
238
|
+
end
|
239
|
+
|
240
|
+
def resolve(int:)
|
241
|
+
int * 4
|
242
|
+
end
|
243
|
+
|
244
|
+
def load_application_object_failed(err)
|
245
|
+
raise GraphQL::ExecutionError.new("ResolverWithErrorHandler failed for id: #{err.id.inspect} (#{err.object.inspect}) (#{err.class.name})")
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
221
249
|
class PrepResolver10 < BaseResolver
|
222
250
|
argument :int1, Integer, required: true
|
223
251
|
argument :int2, Integer, required: true, as: :integer_2
|
@@ -356,6 +384,7 @@ describe GraphQL::Schema::Resolver do
|
|
356
384
|
field :prep_resolver_12, resolver: PrepResolver12
|
357
385
|
field :prep_resolver_13, resolver: PrepResolver13
|
358
386
|
field :prep_resolver_14, resolver: PrepResolver14
|
387
|
+
field :resolver_with_error_handler, resolver: ResolverWithErrorHandler
|
359
388
|
end
|
360
389
|
|
361
390
|
class Schema < GraphQL::Schema
|
@@ -433,6 +462,51 @@ describe GraphQL::Schema::Resolver do
|
|
433
462
|
end
|
434
463
|
end
|
435
464
|
|
465
|
+
describe "load_application_object_failed hook" do
|
466
|
+
it "isn't called for successful queries" do
|
467
|
+
query_str = <<-GRAPHQL
|
468
|
+
{
|
469
|
+
resolverWithErrorHandler(int: "abcd") { value }
|
470
|
+
}
|
471
|
+
GRAPHQL
|
472
|
+
|
473
|
+
res = exec_query(query_str)
|
474
|
+
assert_equal 16, res["data"]["resolverWithErrorHandler"]["value"]
|
475
|
+
refute res.key?("errors")
|
476
|
+
end
|
477
|
+
|
478
|
+
describe "when the id doesn't find anything" do
|
479
|
+
it "passes an error to the handler" do
|
480
|
+
query_str = <<-GRAPHQL
|
481
|
+
{
|
482
|
+
resolverWithErrorHandler(int: "failed_to_find") { value }
|
483
|
+
}
|
484
|
+
GRAPHQL
|
485
|
+
|
486
|
+
res = exec_query(query_str)
|
487
|
+
assert_nil res["data"].fetch("resolverWithErrorHandler")
|
488
|
+
expected_err = "ResolverWithErrorHandler failed for id: \"failed_to_find\" (nil) (GraphQL::Schema::Resolver::LoadApplicationObjectFailedError)"
|
489
|
+
assert_equal [expected_err], res["errors"].map { |e| e["message"] }
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
describe "when resolve_type returns a no-good type" do
|
494
|
+
it "calls the handler" do
|
495
|
+
query_str = <<-GRAPHQL
|
496
|
+
{
|
497
|
+
resolverWithErrorHandler(int: "resolve_type_as_wrong_type") { value }
|
498
|
+
}
|
499
|
+
GRAPHQL
|
500
|
+
|
501
|
+
res = exec_query(query_str)
|
502
|
+
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::Resolver::LoadApplicationObjectFailedError)"
|
504
|
+
assert_equal [expected_err], res["errors"].map { |e| e["message"] }
|
505
|
+
end
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
|
436
510
|
describe "extras" do
|
437
511
|
it "is inherited" do
|
438
512
|
res = exec_query " { resolver4 resolver5 } ", root_value: OpenStruct.new(value: 0)
|