Skip to content

Commit

Permalink
Added: GraphQL tests for basic tracing.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jan 5, 2018
1 parent 0c31901 commit 9fb04d5
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/contrib/graphql/test_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Datadog
module Contrib
module GraphQL
class Foo
attr_accessor :id, :name

def initialize(id, name = 'bar')
@id = id
@name = name
end
end

FooType = ::GraphQL::ObjectType.define do
name 'Foo'
field :id, !types.ID
field :name, types.String
field :created_at, !types.String
field :updated_at, !types.String
end

QueryType = ::GraphQL::ObjectType.define do
name 'Query'
# Add root-level fields here.
# They will be entry points for queries on your schema.

field :foo do
type FooType
argument :id, !types.ID
description 'Find a Foo by ID'
resolve ->(_obj, args, _ctx) { Foo.new(args['id']) }
end
end

TestSchema = ::GraphQL::Schema.define do
query(QueryType)
# TODO: Remove this line if we can get the patcher
# To auto-enable tracing.
use(::GraphQL::Tracing::DataDogTracing)
end
end
end
end
35 changes: 35 additions & 0 deletions test/contrib/graphql/test_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Datadog
module Contrib
module GraphQL
class Foo
attr_accessor :id, :name

def initialize(id, name = 'bar')
@id = id
@name = name
end
end

FooType = ::GraphQL::ObjectType.define do
name 'Foo'
field :id, !types.ID
field :name, types.String
field :created_at, !types.String
field :updated_at, !types.String
end

QueryType = ::GraphQL::ObjectType.define do
name 'Query'
# Add root-level fields here.
# They will be entry points for queries on your schema.

field :foo do
type FooType
argument :id, !types.ID
description 'Find a Foo by ID'
resolve ->(_obj, args, _ctx) { Foo.new(args['id']) }
end
end
end
end
end
56 changes: 56 additions & 0 deletions test/contrib/graphql/tracer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'helper'
require 'ddtrace'
require 'graphql'
require_relative 'test_types'

module Datadog
module Contrib
module GraphQL
class TracerTest < Minitest::Test
def setup
@tracer = get_test_tracer
@schema = build_schema

Datadog.configure do |c|
c.use :graphql,
service_name: 'graphql-test',
tracer: tracer,
schemas: [schema]
end
end

def test_trace
# Perform query
query = '{ foo(id: 1) { name } }'
result = schema.execute(query, variables: {}, context: {}, operation_name: nil)

# Expect no errors
assert_nil(result.to_h['errors'])

# Expect nine spans
assert_equal(9, all_spans.length)

# Expect each span to be properly named
all_spans.each do |span|
assert_equal('graphql-test', span.service)
assert_equal(true, !span.resource.to_s.empty?)
end
end

def build_schema
::GraphQL::Schema.define do
query(QueryType)
end
end

private

attr_reader :tracer, :schema

def all_spans
tracer.writer.spans(:keep)
end
end
end
end
end

0 comments on commit 9fb04d5

Please sign in to comment.