Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def parse(query_string:, &block)
def validate(query:, validate:, &block)
tracer.in_span('graphql.validate') do |span|
super.tap do |response|
errors = response[:errors]&.compact&.map(&:to_h)&.to_json
unless errors.nil?
errors = response[:errors]&.compact&.map(&:to_h) || []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might still be overly defensive but the root cause is that the graphql-ruby gem always returns an array: https://github.com/rmosolgo/graphql-ruby/blob/bde9b64d7df0cdc28045a9231d3219a800052ffc/lib/graphql/static_validation/validator.rb#L54-L56


unless errors.empty?
span.add_event(
'graphql.validation.error',
attributes: {
'message' => errors
'exception.message' => errors.to_json
}
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ def platform_trace(platform_key, key, data) # rubocop:disable Metrics/Cyclomatic

tracer.in_span(platform_key, attributes: attributes_for(key, data)) do |span|
yield.tap do |response|
errors = response[:errors]&.compact&.map(&:to_h)&.to_json if key == 'validate'
unless errors.nil?
next unless key == 'validate'

errors = response[:errors]&.compact&.map(&:to_h) || []

unless errors.empty?
span.add_event(
'graphql.validation.error',
attributes: {
'message' => errors
'exception.message' => errors.to_json
}
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,30 @@
end
end

it 'traces validate with events' do
SomeGraphQLAppSchema.execute(
<<-GRAPHQL
{
nonExistentField
}
GRAPHQL
)
span = spans.find { |s| s.name == 'graphql.validate' }
event = span.events.find { |e| e.name == 'graphql.validation.error' }
# rubocop:disable Layout/LineLength
_(event.attributes['message']).must_equal(
"[{\"message\":\"Field 'nonExistentField' doesn't exist on type 'Query'\",\"locations\":[{\"line\":2,\"column\":13}],\"path\":[\"query\",\"nonExistentField\"],\"extensions\":{\"code\":\"undefinedField\",\"typeName\":\"Query\",\"fieldName\":\"nonExistentField\"}}]"
)
# rubocop:enable Layout/LineLength
describe 'validate spans' do
it 'adds events for validation errors' do
SomeGraphQLAppSchema.execute(
<<-GRAPHQL
{
nonExistentField
}
GRAPHQL
)
span = spans.find { |s| s.name == 'graphql.validate' }
event = span.events.find { |e| e.name == 'graphql.validation.error' }
# rubocop:disable Layout/LineLength
_(event.attributes['exception.message']).must_equal(
"[{\"message\":\"Field 'nonExistentField' doesn't exist on type 'Query'\",\"locations\":[{\"line\":2,\"column\":15}],\"path\":[\"query\",\"nonExistentField\"],\"extensions\":{\"code\":\"undefinedField\",\"typeName\":\"Query\",\"fieldName\":\"nonExistentField\"}}]"
)
# rubocop:enable Layout/LineLength
end

it 'does not add events for valid documents' do
SomeGraphQLAppSchema.execute('{ vehicle { __typename } }')

span = spans.find { |s| s.name == 'graphql.validate' }
_(span.events).must_be_nil
end
end
end

Expand Down