class RSpec::GraphqlMatchers::HaveAField

Public Class Methods

new(expected_field_name, fields = :fields) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 13
def initialize(expected_field_name, fields = :fields)
  @expected_field_name = expected_field_name.to_s
  @expected_camel_field_name = GraphQL::Schema::Member::BuildType.camelize(
    @expected_field_name
  )
  @fields = fields.to_sym
  @expectations = []
end

Public Instance Methods

description() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 72
def description
  ["define field `#{@expected_field_name}`"].concat(descriptions).join(', ')
end
failure_message() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 63
def failure_message
  base_msg = "expected #{member_name(@graph_object)} " \
    "to define field `#{@expected_field_name}`" \

  return "#{base_msg} #{failure_messages.join(', ')}" if actual_field

  "#{base_msg} but no field was found with that name"
end
matches?(graph_object) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 22
def matches?(graph_object)
  @graph_object = graph_object

  return false if actual_field.nil?

  @results = @expectations.reject do |matcher|
    matcher.matches?(actual_field)
  end

  @results.empty?
end
of_type(expected_field_type)
Alias for: that_returns
returning(expected_field_type)
Alias for: that_returns
that_returns(expected_field_type) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 34
def that_returns(expected_field_type)
  @expectations << HaveAFieldMatchers::OfType.new(expected_field_type)
  self
end
Also aliased as: returning, of_type
with_deprecation_reason(expected_reason = nil) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 58
def with_deprecation_reason(expected_reason = nil)
  @expectations << HaveAFieldMatchers::WithDeprecationReason.new(expected_reason)
  self
end
with_hash_key(expected_hash_key) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 47
def with_hash_key(expected_hash_key)
  @expectations << HaveAFieldMatchers::WithHashKey.new(expected_hash_key)

  self
end
with_metadata(expected_metadata) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 53
def with_metadata(expected_metadata)
  @expectations << HaveAFieldMatchers::WithMetadata.new(expected_metadata)
  self
end
with_property(expected_property_name) click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 42
def with_property(expected_property_name)
  @expectations << HaveAFieldMatchers::WithProperty.new(expected_property_name)
  self
end

Private Instance Methods

actual_field() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 78
def actual_field
  @actual_field ||= begin
    field = field_collection[@expected_field_name]
    field ||= field_collection[@expected_camel_field_name]

    field.respond_to?(:to_graphql) ? field.to_graphql : field
  end
end
descriptions() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 87
def descriptions
  @results.map(&:description)
end
failure_messages() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 91
def failure_messages
  @results.map(&:failure_message)
end
field_collection() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 95
def field_collection
  if @graph_object.respond_to?(@fields)
    @graph_object.public_send(@fields)
  else
    raise "Invalid object #{@graph_object} provided to #{matcher_name} " \
      'matcher. It does not seem to be a valid GraphQL object type.'
  end
end
matcher_name() click to toggle source
# File lib/rspec/graphql_matchers/have_a_field.rb, line 104
def matcher_name
  case @fields
  when :fields        then 'have_a_field'
  when :input_fields  then 'have_an_input_field'
  when :return_fields then 'have_a_return_field'
  end
end