Skip to content

Commit f44d7ba

Browse files
authored
Merge pull request #5071 from rmosolgo/handle-execution-error-from-object-from-id
Handle GraphQL::ExecutionError from loading objects during analysis
2 parents 761191d + 2f9ac82 commit f44d7ba

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

lib/graphql/analysis.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def analyze_query(query, analyzers, multiplex_analyzers: [])
8181
end
8282
rescue Timeout::Error
8383
[GraphQL::AnalysisError.new("Timeout on validation of query")]
84-
rescue GraphQL::UnauthorizedError
84+
rescue GraphQL::UnauthorizedError, GraphQL::ExecutionError
8585
# This error was raised during analysis and will be returned the client before execution
8686
[]
8787
end

lib/graphql/schema/field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def calculate_complexity(query:, nodes:, child_complexity:)
490490
if arguments[:last] && (max_possible_page_size.nil? || arguments[:last] > max_possible_page_size)
491491
max_possible_page_size = arguments[:last]
492492
end
493-
elsif arguments.is_a?(GraphQL::UnauthorizedError)
493+
elsif arguments.is_a?(GraphQL::ExecutionError) || arguments.is_a?(GraphQL::UnauthorizedError)
494494
raise arguments
495495
end
496496

spec/graphql/analysis/max_query_complexity_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ def self.resolve_type(abs_type, object, ctx)
190190
end
191191

192192
def self.object_from_id(id, ctx)
193-
{ name: "Loaded thing #{id}" }
193+
if id == "13"
194+
raise GraphQL::ExecutionError, "No Thing ##{id}"
195+
else
196+
{ name: "Loaded thing #{id}" }
197+
end
194198
end
195199

196200
def self.unauthorized_object(err)
@@ -209,5 +213,11 @@ def self.unauthorized_object(err)
209213
res2 = AuthorizedTypeSchema.execute(query_str)
210214
assert_equal ["Unauthorized Object: \"Loaded thing 123\""], res2["errors"].map { |e| e["message"] }
211215
end
216+
217+
it "returns the right error when the loaded object raises an error" do
218+
query_str = "{ things(thingId: \"13\", first: 1) { nodes { name } } }"
219+
res = AuthorizedTypeSchema.execute(query_str, context: { authorized: true })
220+
assert_equal ["No Thing #13"], res["errors"].map { |e| e["message"] }
221+
end
212222
end
213223
end

spec/graphql/analysis/query_complexity_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,7 @@ def resolve
668668
it "handles the error" do
669669
res = GraphQL::Query.new(complexity_schema, query_string).result
670670
assert_equal ["count must be less than 50"], res["errors"].map { |e| e["message"] }
671-
complexity = reduce_result.first
672-
assert_equal 102, complexity, "It uses max page size"
671+
assert_equal [], reduce_result, "It doesn't finish calculation"
673672
end
674673
end
675674

0 commit comments

Comments
 (0)