File: notifications_trace_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (75 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

require "spec_helper"

describe GraphQL::Tracing::NotificationsTrace do
  module NotificationsTraceTest
    class Query < GraphQL::Schema::Object
      field :int, Integer, null: false

      def int
        1
      end
    end

    class DummyEngine < GraphQL::Tracing::NotificationsTrace::Adapter
      class << self
        def dispatched_events
          @dispatched_events ||= []
        end
      end

      def instrument(event, payload = nil)
        self.class.dispatched_events << [event, payload]
        yield
      end

      class Event < GraphQL::Tracing::NotificationsTrace::Adapter::Event
        def start; end
        def finish
          DummyEngine.dispatched_events << [@name, @payload]
        end
      end
    end

    module OtherTrace
      def execute_query(query:)
        query.context[:other_trace_ran] = true
        super
      end
    end

    class Schema < GraphQL::Schema
      query Query
      trace_with OtherTrace
      trace_with GraphQL::Tracing::NotificationsTrace, engine: DummyEngine
    end
  end

  before do
    NotificationsTraceTest::DummyEngine.dispatched_events.clear
  end

  describe "Observing" do
    it "dispatches the event to the notifications engine with a suffix" do
      NotificationsTraceTest::Schema.execute "query X { int }"
      dispatched_events = NotificationsTraceTest::DummyEngine.dispatched_events.to_h
      expected_event_keys = [
        "execute.graphql",
        (USING_C_PARSER ? "lex.graphql" : nil),
        "parse.graphql",
        "validate.graphql",
        "analyze.graphql",
        "authorized.graphql",
        "execute_field.graphql"
      ].compact

      assert_equal expected_event_keys, dispatched_events.keys
    end

    it "works with other tracers" do
      res = NotificationsTraceTest::Schema.execute "query X { int }"
      assert res.context[:other_trace_ran]
    end
  end
end