Skip to content

Commit

Permalink
Merge pull request #531 from yzlin/graphql-fix-patching-for-derived-s…
Browse files Browse the repository at this point in the history
…chema

[graphql] Fix patching for derived schema
  • Loading branch information
pawelchcki authored Sep 5, 2018
2 parents dab4f5b + 8ab5edc commit e80a949
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 38 deletions.
12 changes: 10 additions & 2 deletions lib/ddtrace/contrib/graphql/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ def patch_schema!(schema)
tracer = get_option(:tracer)
service_name = get_option(:service_name)

schema.define do
use(
if schema.respond_to?(:use)
schema.use(
::GraphQL::Tracing::DataDogTracing,
tracer: tracer,
service: service_name
)
else
schema.define do
use(
::GraphQL::Tracing::DataDogTracing,
tracer: tracer,
service: service_name
)
end
end
end

Expand Down
9 changes: 8 additions & 1 deletion spec/ddtrace/contrib/graphql/test_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
end

RSpec.shared_context 'GraphQL test schema' do
let(:schema) do
let(:defined_schema) do
qt = query_type

::GraphQL::Schema.define do
query(qt)
end
end

let(:derived_schema) do
qt = query_type
Class.new(::GraphQL::Schema) do
query(qt)
end
end

let(:query_type_name) { 'Query' }
let(:query_type) do
qtn = query_type_name
Expand Down
83 changes: 48 additions & 35 deletions spec/ddtrace/contrib/graphql/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,63 @@ def pop_spans
let(:all_spans) { pop_spans }
let(:root_span) { all_spans.find { |s| s.parent.nil? } }

before(:each) do
Datadog.configure do |c|
c.use :graphql,
service_name: 'graphql-test',
tracer: tracer,
schemas: [schema]
RSpec.shared_examples 'Schema patcher' do
before(:each) do
Datadog.configuration[:graphql].instance_variable_get(:@integration).instance_variable_set(:@patched, false)
Datadog.configure do |c|
c.use :graphql,
service_name: 'graphql-test',
tracer: tracer,
schemas: [schema]
end
end
end

describe 'query trace' do
subject(:result) { schema.execute(query, variables: {}, context: {}, operation_name: nil) }
describe 'query trace' do
subject(:result) { schema.execute(query, variables: {}, context: {}, operation_name: nil) }

let(:query) { '{ foo(id: 1) { name } }' }
let(:variables) { {} }
let(:context) { {} }
let(:operation_name) { nil }
let(:query) { '{ foo(id: 1) { name } }' }
let(:variables) { {} }
let(:context) { {} }
let(:operation_name) { nil }

it do
# Expect no errors
expect(result.to_h['errors']).to be nil
it do
# Expect no errors
expect(result.to_h['errors']).to be nil

# Expect nine spans
expect(all_spans).to have(9).items
# Expect nine spans
expect(all_spans).to have(9).items

# List of valid resource names
# (If this is too brittle, revist later.)
valid_resource_names = [
'Query.foo',
'analyze.graphql',
'execute.graphql',
'lex.graphql',
'parse.graphql',
'validate.graphql'
]
# List of valid resource names
# (If this is too brittle, revist later.)
valid_resource_names = [
'Query.foo',
'analyze.graphql',
'execute.graphql',
'lex.graphql',
'parse.graphql',
'validate.graphql'
]

# Expect root span to be 'execute.graphql'
expect(root_span.name).to eq('execute.graphql')
expect(root_span.resource).to eq('execute.graphql')
# Expect root span to be 'execute.graphql'
expect(root_span.name).to eq('execute.graphql')
expect(root_span.resource).to eq('execute.graphql')

# Expect each span to be properly named
all_spans.each do |span|
expect(span.service).to eq('graphql-test')
expect(valid_resource_names).to include(span.resource.to_s)
# Expect each span to be properly named
all_spans.each do |span|
expect(span.service).to eq('graphql-test')
expect(valid_resource_names).to include(span.resource.to_s)
end
end
end
end

context 'defined schema' do
let(:schema) { defined_schema }
it_should_behave_like 'Schema patcher'
end

context 'derived schema' do
let(:schema) { derived_schema }
it_should_behave_like 'Schema patcher'
end
end

0 comments on commit e80a949

Please sign in to comment.