Skip to content

Commit

Permalink
fix: update pinning for graphql 2.3...3 (#21)
Browse files Browse the repository at this point in the history
* fix: update p

* fix:updated to ~>

* fix: printer

* fix: updated with cops and print statements

* fix: updated to 2-3

* Update version.rb

* fix: gemspec

* fix: gemspec

* fix: cops

* fix: updated graphql gemfile.lock

* fix: add print_string explicitly

* fix: update to 2.3

* fix: added fix and tests for usage_reporter

* fix: update second gemfile.lock

* Update spec/graphql/graphql-hive/usage_reporter_spec.rb

Co-authored-by: Cassidy Scheffer <[email protected]>

* fix: formatting

* fix: updated to >= 2.3

---------

Co-authored-by: Cassidy Scheffer <[email protected]>
  • Loading branch information
aryascripts and cassidycodes authored Jul 12, 2024
1 parent 25f080b commit 01407d8
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
PATH
remote: .
specs:
graphql-hive (0.3.3)
graphql (~> 2.0.9)
graphql-hive (0.3.4)
graphql (>= 2.3, < 3)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
base64 (0.2.0)
diff-lcs (1.5.1)
graphql (2.0.31)
graphql (2.3.7)
base64
json (2.7.2)
language_server-protocol (3.17.0.3)
Expand Down
2 changes: 1 addition & 1 deletion graphql-hive.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
end

spec.add_dependency 'graphql', '~> 2.0.9'
spec.add_dependency 'graphql', '>= 2.3', '< 3'

spec.add_development_dependency 'bundler', '~> 2'
spec.add_development_dependency 'rake', '~> 13'
Expand Down
6 changes: 3 additions & 3 deletions k6/graphql-api/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PATH
remote: ../..
specs:
graphql-hive (0.3.3)
graphql (~> 2.0.9)
graphql-hive (0.3.4)
graphql (>= 2.3, < 3)

GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
graphql (2.0.31)
graphql (2.3.7)
base64
multi_json (1.15.0)
mustermann (2.0.2)
Expand Down
69 changes: 46 additions & 23 deletions lib/graphql-hive/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,43 @@ class Hive < GraphQL::Tracing::PlatformTracing
# - removes aliases
# - sort nodes and directives (files, arguments, variables)
class Printer < GraphQL::Language::Printer
def print_string(str)
@out.append(str)
end

def print_node(node, indent: '')
case node
when Float, Integer
'0'
print_string '0'
when String
''
print_string ''
else
super
end
end

# rubocop:disable Style/RedundantInterpolation
# from GraphQL::Language::Printer with sort_by name
# ignores aliases
def print_field(field, indent: '')
out = "#{indent}".dup
out << "#{field.name}"
out << "(#{field.arguments.sort_by(&:name).map { |a| print_argument(a) }.join(', ')})" if field.arguments.any?
out << print_directives(field.directives)
out << print_selections(field.selections, indent: indent)
out
print_string(indent)
print_string(field.name)
if field.arguments.any?
print_string('(')
field.arguments.sort_by(&:name).each_with_index do |a, i|
print_argument(a)
print_string(', ') if i < field.arguments.size - 1
end
print_string(')')
end
print_directives(field.directives)
print_selections(field.selections, indent: indent)
end
# rubocop:enable Style/RedundantInterpolation

def print_directives(directives)
super(directives.sort_by(&:name))
end

# from GraphQL::Language::Printer with sort_by name
def print_selections(selections, indent: '')
sorted_nodes = selections.sort_by do |s|
next s.name if s.respond_to?(:name)
Expand All @@ -42,29 +53,41 @@ def print_selections(selections, indent: '')
super(sorted_nodes, indent: indent)
end

# from GraphQL::Language::Printer with sort_by name
def print_directive(directive)
out = "@#{directive.name}".dup
print_string('@')
print_string(directive.name)

if directive.arguments.any?
out << "(#{directive.arguments.sort_by(&:name).map { |a| print_argument(a) }.join(', ')})"
end
return if directive.arguments.blank?

out
print_string('(')
directive.arguments.sort_by(&:name).each_with_index do |a, i|
print_argument(a)
print_string(', ') if i < directive.arguments.size - 1
end
print_string(')')
end

# from GraphQL::Language::Printer with sort_by name
def print_operation_definition(operation_definition, indent: '')
out = "#{indent}#{operation_definition.operation_type}".dup
out << " #{operation_definition.name}" if operation_definition.name
print_string(indent)
print_string(operation_definition.operation_type)
if operation_definition.name
print_string(' ')
print_string(operation_definition.name)
end

# rubocop:disable Layout/LineLength
if operation_definition.variables.any?
out << "(#{operation_definition.variables.sort_by(&:name).map { |v| print_variable_definition(v) }.join(', ')})"
print_string('(')
operation_definition.variables.sort_by(&:name).each_with_index do |v, i|
print_variable_definition(v)
print_string(', ') if i < operation_definition.variables.size - 1
end
print_string(')')
end
# rubocop:enable Layout/LineLength

out << print_directives(operation_definition.directives)
out << print_selections(operation_definition.selections, indent: indent)
out
print_directives(operation_definition.directives)
print_selections(operation_definition.selections, indent: indent)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql-hive/usage_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ def add_operation_to_report(report, operation)
analyzers: [analyzer]
)

visitor.visit
result = visitor.visit

fields.merge(analyzer.result)

operation += "\n" unless operation.empty?
operation += GraphQL::Hive::Printer.new.print(visitor.result)
operation += GraphQL::Hive::Printer.new.print(result)
end

md5 = Digest::MD5.new
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-hive/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Graphql
module Hive
VERSION = '0.3.3'
VERSION = '0.3.4'
end
end
127 changes: 127 additions & 0 deletions spec/graphql/graphql-hive/usage_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GraphQL::Hive::UsageReporter do
let(:subject) { described_class.instance }
let(:client) { instance_double('Hive::Client') }
let(:options) do
{
logger: logger,
buffer_size: 1
}
end
let(:logger) { instance_double('Logger') }

before do
allow(logger).to receive(:warn)
allow(logger).to receive(:debug)
end

describe '#initialize' do
it 'sets the instance' do
expect(described_class.instance).to eq(nil)
described_class.new(options, client)
expect(described_class.instance).to_not eq(nil)
end
end

describe '#add_operation' do
it 'adds an operation to the queue' do
operation = { operation: 'test' }
described_class.new(options, client)
subject.add_operation(operation)
expect(subject.instance_variable_get(:@queue).pop).to eq(operation)
end
end

describe '#on_exit' do
it 'closes the queue and joins the thread' do
described_class.new(options, client)
expect(subject.instance_variable_get(:@queue)).to receive(:close)
expect(subject.instance_variable_get(:@thread)).to receive(:join)
subject.on_exit
end
end

describe '#on_start' do
it 'starts the thread' do
described_class.new(options, client)
expect(subject).to receive(:start_thread)
subject.on_start
end
end

describe '#start_thread' do
it 'logs a warning if the thread is already alive' do
described_class.new(options, client)
subject.instance_variable_set(:@thread, Thread.new { p 'test' })
expect(logger).to receive(:warn)
subject.on_start
end
end

describe '#add_operation' do
let(:timestamp) { 1_720_705_946_333 }
let(:queries) { [] }
let(:results) { [] }
let(:duration) { 100_000 }
let(:operation) do
[timestamp, queries, results, duration]
end
let(:schema) do
GraphQL::Schema.from_definition('type Query { test: String }')
end
let(:query_string) { 'query TestingHive { test }' }

it 'adds an operation to the buffer' do
described_class.new(options, client)
subject.add_operation(operation)
expect(subject.instance_variable_get(:@queue).pop).to eq(operation)
end

context 'successful operation' do
let(:options) do
{ logger: logger, buffer_size: 2 }
end
let(:queries) do
[GraphQL::Query.new(schema, query_string, variables: {})]
end
let(:results) do
[GraphQL::Query::Result.new(query: queries.first, values: { 'data' => { 'test' => 'test' } })]
end

before do
allow(client).to receive(:send)
end

it 'processes the operations if the buffer is full' do
described_class.new(options, client)
subject.add_operation(operation)

# call process_operation with send
subject.send(:process_operations, [operation])
expect(client).to have_received(:send).with(
'/usage',
{
map: { '8b8412ce86f3ea7accb931b1a5de335d' =>
{
fields: %w[Query Query.test],
operation: "query TestingHive {\n test\n}",
operationName: 'TestingHive'
} },
operations: [
{
execution: { duration: 100_000, errors: [], errorsTotal: 0, ok: true },
operationMapKey: '8b8412ce86f3ea7accb931b1a5de335d',
timestamp: 1_720_705_946_333
}
],
size: 1
},
:usage
)
end
end
end
end

0 comments on commit 01407d8

Please sign in to comment.