Class: GraphQL::Hive

Inherits:
Tracing::PlatformTracing
  • Object
show all
Defined in:
lib/graphql-hive.rb,
lib/graphql-hive/client.rb,
lib/graphql-hive/printer.rb,
lib/graphql-hive/sampler.rb,
lib/graphql-hive/analyzer.rb,
lib/graphql-hive/bounded_queue.rb,
lib/graphql-hive/usage_reporter.rb,
lib/graphql-hive/sampling/basic_sampler.rb,
lib/graphql-hive/sampling/dynamic_sampler.rb,
lib/graphql-hive/sampling/sampling_context.rb

Overview

GraphQL Hive usage collector and schema reporter

Defined Under Namespace

Modules: Sampling Classes: Analyzer, BoundedQueue, Client, Printer, Sampler, UsageReporter

Constant Summary collapse

REPORT_SCHEMA_MUTATION =
<<~MUTATION
  mutation schemaPublish($input: SchemaPublishInput!) {
    schemaPublish(input: $input) {
      __typename
    }
  }
MUTATION
DEFAULT_OPTIONS =
{
  enabled: true,
  debug: false,
  port: "443",
  collect_usage: true,
  read_operations: true,
  report_schema: true,
  buffer_size: 50,
  logger: nil,
  collect_usage_sampling: 1.0
}.freeze
@@schema =
nil
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hive

Returns a new instance of Hive.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/graphql-hive.rb', line 54

def initialize(options = {})
  opts = DEFAULT_OPTIONS.merge(options)
  initialize_options!(opts)
  super(opts)

  @@instance = self

  @client = GraphQL::Hive::Client.new(opts)
  @usage_reporter = GraphQL::Hive::UsageReporter.new(opts, @client)

  # buffer
  @report = {
    size: 0,
    map: {},
    operations: []
  }

  send_report_schema(@@schema) if @@schema && opts[:report_schema] && @options[:enabled]
end

Class Method Details

.instanceObject



74
75
76
# File 'lib/graphql-hive.rb', line 74

def self.instance
  @@instance
end

.use(schema, **kwargs) ⇒ Object



78
79
80
81
# File 'lib/graphql-hive.rb', line 78

def self.use(schema, **kwargs)
  @@schema = schema
  super
end

Instance Method Details

#on_exitObject



122
123
124
# File 'lib/graphql-hive.rb', line 122

def on_exit
  @usage_reporter.on_exit
end

#on_startObject



126
127
128
# File 'lib/graphql-hive.rb', line 126

def on_start
  @usage_reporter.on_start
end

#platform_authorized_key(type) ⇒ Object

compat



108
109
110
# File 'lib/graphql-hive.rb', line 108

def platform_authorized_key(type)
  "#{type.graphql_name}.authorized.graphql"
end

#platform_field_key(type, field) ⇒ Object

compat



118
119
120
# File 'lib/graphql-hive.rb', line 118

def platform_field_key(type, field)
  "graphql.#{type.name}.#{field.name}"
end

#platform_resolve_type_key(type) ⇒ Object

compat



113
114
115
# File 'lib/graphql-hive.rb', line 113

def platform_resolve_type_key(type)
  "#{type.graphql_name}.resolve_type.graphql"
end

#platform_trace(platform_key, _key, data) ⇒ Object

called on trace events



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/graphql-hive.rb', line 84

def platform_trace(platform_key, _key, data)
  return yield unless @options[:enabled] && @options[:collect_usage]

  if platform_key == "execute_multiplex"
    if data[:multiplex]
      queries = data[:multiplex].queries
      timestamp = (Time.now.utc.to_f * 1000).to_i
      starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      results = yield
      ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      elapsed = ending - starting
      duration = (elapsed.to_f * (10**9)).to_i

      report_usage(timestamp, queries, results, duration) unless queries.empty?
      results
    else
      yield
    end
  else
    yield
  end
end