Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting of operation name for query & mutation #1

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ group :test do
gem 'sinatra'
gem 'vcr'
gem 'webmock'
gem 'rspec-instrumentation-matcher'
end
22 changes: 20 additions & 2 deletions lib/graphlient/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def execute(query, variables = nil)
query_params = {}
query_params[:context] = @options if @options
query_params[:variables] = variables if variables
query = client.parse(query) if query.is_a?(String)
rc = client.query(query, **query_params)
rc = client.query(parse_query(query), **query_params)
raise Graphlient::Errors::GraphQLError, rc if rc.errors.any?
# see https://github.com/github/graphql-client/pull/132
# see https://github.com/exAspArk/graphql-errors/issues/2
Expand Down Expand Up @@ -69,5 +68,24 @@ def client
def errors_in_result?(response)
response.data && response.data.errors && response.data.errors.all.any?
end

def parse_query(query)
return query unless query.is_a?(String)

query = client.parse(query)
return query if query.is_a?(GraphQL::Client::OperationDefinition)

query = query.const_get(query.constants.first)
patch_operation_name(query)
query
end

def patch_operation_name(query)
query.instance_eval do
def name
super.gsub(/#<Module:(0x[0-9a-f]+)>/, 'Graphlient')
end
end
end
end
end
13 changes: 10 additions & 3 deletions lib/graphlient/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ def resolve_fragment_constant(value)
end

def append_node(node, args, arg_processor: nil, &block)
hash_arguments = hash_arg(args)

node = "...#{resolve_fragment_constant(node)}".to_sym if node.to_s.start_with?('___')

# add field
@query_str << "\n#{indent}#{node}"
# when using operation name
if ROOT_NODES.include?(node) && hash_arguments && hash_arguments.has_key?(:operation_name)
@query_str << "\n#{indent}#{node} #{hash_arguments.delete(:operation_name)} "
else
@query_str << "\n#{indent}#{node}"
end

# add filter
hash_arguments = hash_arg(args)
@query_str << "(#{args_str(hash_arguments, arg_processor: arg_processor)})" if hash_arguments
@query_str << "(#{args_str(hash_arguments, arg_processor: arg_processor)})" if hash_arguments&.any?

if block_given?
@indents += 1
Expand Down
12 changes: 12 additions & 0 deletions spec/graphlient/github_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
expect(rc.data.viewer.name).to eq 'Daniel Doubrovkine (dB.) @dblockdotorg'
end

it 'correctly sets the operation name & type', vcr: { cassette_name: 'github/viewer' } do
expect {
client.query <<-GRAPHQL
query MyViewerAsOperationName {
viewer {
name
}
}
GRAPHQL
}.to instrument("query.graphql").with(hash_including(:operation_name=>"Graphlient__MyViewerAsOperationName", :operation_type=>"query"))
end

it 'queries with a parameter', vcr: { cassette_name: 'github/user' } do
query = <<-GRAPHQL
query($login: String!) {
Expand Down
25 changes: 25 additions & 0 deletions spec/graphlient/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
describe Graphlient::Query do
describe '#initialize' do
context 'query' do
it 'returns the operation_name in the query' do
query = Graphlient::Query.new do
query(operation_name: "MyOperationName") do
invoice do
line_items
end
end
end

expect(query.to_s).to eq "query MyOperationName {\n invoice{\n line_items\n }\n }"
end

it 'returns expected query with block' do
query = Graphlient::Query.new do
query do
Expand Down Expand Up @@ -79,6 +91,19 @@
end

context 'mutation' do

it 'returns the operation_name in the query' do
mutation = Graphlient::Query.new do
mutation(operation_name: "MyUpdateMutationOperationName") do
invoice(type: 'test', fee_in_cents: 20_000, total_cents: 50_000, line_items: %w[li1 li2]) do
id
end
end
end

expect(mutation.to_s).to eq "mutation MyUpdateMutationOperationName {\n invoice(type: \"test\", fee_in_cents: 20000, total_cents: 50000, line_items: [\"li1\", \"li2\"]){\n id\n }\n }"
end

it 'returns proper mutation with arguments' do
mutation = Graphlient::Query.new do
mutation do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'webmock/rspec'
require 'vcr'
require 'faraday/rack'
require 'rspec-instrumentation-matcher'

Dir[File.join(File.dirname(__FILE__), 'support', '**/*.rb')].each do |file|
require file
Expand Down