Commit ed4b0fe3 authored by Alex Kalderimis's avatar Alex Kalderimis

Move var helper to its own file

This moves GraphqlHelpers::Var to Graphql::Var and adds specific spec
for it.
parent 61d280e3
# frozen_string_literal: true
module Graphql
# Helper to pass variables around generated queries.
#
# e.g.:
# first = var('Int')
# after = var('String')
#
# query = with_signature(
# [first, after],
# query_graphql_path([
# [:project, { full_path: project.full_path }],
# [:issues, { after: after, first: first }]
# :nodes
# ], all_graphql_fields_for('Issue'))
# )
#
# post_graphql(query, variables: [first.with(2), after.with(some_cursor)])
#
class Var
attr_reader :name, :type
attr_accessor :value
def initialize(name, type)
@name = name
@type = type
end
def sig
"#{to_graphql_value}: #{type}"
end
def to_graphql_value
"$#{name}"
end
# We return a new object so that running the same query twice with
# different values does not risk re-using the value
#
# e.g.
#
# x = var('Int')
# expect { post_graphql(query, variables: x) }
# .to issue_same_number_of_queries_as { post_graphql(query, variables: x.with(1)) }
#
# Here we post the `x` variable once with the value set to 1, and once with
# the value set to `nil`.
def with(value)
copy = Var.new(name, type)
copy.value = value
copy
end
def to_h
{ name => value }
end
end
end
......@@ -275,64 +275,8 @@ module GraphqlHelpers
%Q[query(#{variables.map(&:sig).join(', ')}) #{query}]
end
# Helper to pass variables around generated queries.
#
# e.g.:
# first = var('Int')
# after = var('String')
#
# query = with_signature(
# [first, after],
# query_graphql_path([
# [:project, { full_path: project.full_path }],
# [:issues, { after: after, first: first }]
# :nodes
# ], all_graphql_fields_for('Issue'))
# )
#
# post_graphql(query, variables: [first.with(2), after.with(some_cursor)])
#
class Var
attr_reader :name, :type
attr_accessor :value
def initialize(name, type)
@name = name
@type = type
end
def sig
"$#{name}: #{type}"
end
def to_graphql_value
"$#{name}"
end
# We return a new object so that running the same query twice with
# different values does not risk re-using the value
#
# e.g.
#
# x = var('Int')
# expect { post_graphql(query, variables: x) }
# .to issue_same_number_of_queries_as { post_graphql(query, variables: x.with(1)) }
#
# Here we post the `x` variable once with the value set to 1, and once with
# the value set to `nil`.
def with(value)
copy = Var.new(name, type)
copy.value = value
copy
end
def to_h
{ name => value }
end
end
def var(type)
Var.new(generate(:variable), type)
::Graphql::Var.new(generate(:variable), type)
end
# Fairly dumb Ruby => GraphQL rendering function. Only suitable for testing.
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Graphql::Var do
subject(:var) { described_class.new('foo', 'Int') }
it 'associates a name with a type and an initially empty value' do
expect(var).to have_attributes(
name: 'foo',
type: 'Int',
value: be_nil
)
end
it 'has a correct signature' do
expect(var).to have_attributes(sig: '$foo: Int')
end
it 'implements to_graphql_value as $name' do
expect(var.to_graphql_value).to eq('$foo')
end
it 'can set a value using with, returning a new object' do
with_value = var.with(42)
expect(with_value).to have_attributes(name: 'foo', type: 'Int', value: 42)
expect(var).to have_attributes(value: be_nil)
end
it 'returns an object suitable for passing to post_graphql(variables:)' do
expect(var.with(17).to_h).to eq('foo' => 17)
end
end
......@@ -10,7 +10,7 @@ RSpec.describe GraphqlHelpers do
query.tr("\n", ' ').gsub(/\s+/, ' ').strip
end
describe ::GraphqlHelpers::Var do
describe 'var' do
it 'allocates a fresh name for each var' do
a = var('Int')
b = var('Int')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment