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/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, 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,
  queue_size: 1000,
  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.



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

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



76
77
78
# File 'lib/graphql-hive.rb', line 76

def self.instance
  @@instance
end

.use(schema, **kwargs) ⇒ Object



80
81
82
83
# File 'lib/graphql-hive.rb', line 80

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

Instance Method Details

#on_exitObject



124
125
126
# File 'lib/graphql-hive.rb', line 124

def on_exit
  @usage_reporter.on_exit
end

#on_startObject



128
129
130
# File 'lib/graphql-hive.rb', line 128

def on_start
  @usage_reporter.on_start
end

#platform_authorized_key(type) ⇒ Object

compat



110
111
112
# File 'lib/graphql-hive.rb', line 110

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

#platform_field_key(type, field) ⇒ Object

compat



120
121
122
# File 'lib/graphql-hive.rb', line 120

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

#platform_resolve_type_key(type) ⇒ Object

compat



115
116
117
# File 'lib/graphql-hive.rb', line 115

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

#platform_trace(platform_key, _key, data) ⇒ Object

called on trace events



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

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