Skip to content

Commit

Permalink
Spec: a BrokenSchema presents easy-to-read errors instead of NoMethod…
Browse files Browse the repository at this point in the history
…Error on nil
  • Loading branch information
olleolleolle committed Mar 6, 2018
1 parent 02b8b86 commit 54a0f2f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/apollo_tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions spec/apollo_tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down
28 changes: 28 additions & 0 deletions spec/fixtures/broken_schema.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 54a0f2f

Please sign in to comment.