-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: GraphQL tests for basic tracing.
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |