diff --git a/lib/apollo_tracing.rb b/lib/apollo_tracing.rb index da5eef9..3405238 100644 --- a/lib/apollo_tracing.rb +++ b/lib/apollo_tracing.rb @@ -60,7 +60,7 @@ def before_query(query) def after_query(query) result = query.result - return unless result + return if result.to_h.nil? end_time = Time.now.utc duration_nanos = duration_nanos(start_time: query.context['apollo-tracing']['start_time'], end_time: end_time) diff --git a/spec/apollo_tracing_spec.rb b/spec/apollo_tracing_spec.rb index 0e9684e..d509bfa 100644 --- a/spec/apollo_tracing_spec.rb +++ b/spec/apollo_tracing_spec.rb @@ -3,6 +3,7 @@ require 'fixtures/user' require 'fixtures/post' require 'fixtures/schema' +require 'fixtures/broken_schema' RSpec.describe ApolloTracing do describe '.start_proxy' do @@ -29,6 +30,14 @@ end context 'introspection' do + it 'supports a nil result for failures' do + query = 'query($user_id: ID!) { posts(user_id: $user_id) { id title user_id } }' + + expect { + BrokenSchema.execute(query, variables: { 'user_id' => '1' }) + }.to raise_error(NoMethodError, /undefined method `title' for/) + end + it 'returns time in RFC 3339 format' do query = "query($user_id: ID!) { posts(user_id: $user_id) { id title user_id } }" now = Time.new(2017, 8, 25, 0, 0, 0, '+00:00') diff --git a/spec/fixtures/broken_schema.rb b/spec/fixtures/broken_schema.rb new file mode 100644 index 0000000..a4f89ec --- /dev/null +++ b/spec/fixtures/broken_schema.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'ostruct' + +BadPostType = GraphQL::ObjectType.define do + name 'Foo' + description 'See also PostType, for a working version of this' + + field :id, !types.String, hash_key: :id + field :user_id, !types.String, hash_key: :user_id + field :title, !types.String # This is the intended broken-ness: missing a hash_key setting +end + +BrokenQueryType = GraphQL::ObjectType.define do + name 'BrokenQuery' + description 'See also QueryType, for a working version of this' + field :posts, !types[!BadPostType] do + argument :user_id, !types.ID + resolve ->(_obj, _args, _ctx) { + [ { id: 'foo1', title: 'titel1', user_id: 'Sven'} ] + } + end +end + +BrokenSchema = GraphQL::Schema.define do + query BrokenQueryType + use ApolloTracing.new +end