Module: GraphWeaver::Hints

Includes:
Kernel
Defined in:
lib/graph_weaver/hints.rb

Overview

Included in generated response structs. GraphQL's camelCase fields become snake_case props, and reaching for the wire name is a classic stumble — result.nameWithOwner instead of result.name_with_owner. Catch the miss and point at the prop that does exist ("use ..." when the mapping is exact, "did you mean ...?" for a near-miss typo). (Typed call sites get this hint earlier, from srb tc.)

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/graph_weaver/hints.rb', line 38

def method_missing(name, *args, &block)
  if args.empty? && (hint = prop_hint(name.to_s))
    raise NoMethodError, "undefined method '#{name}' for #{self.class}#{hint}"
  end

  super
end

Class Method Details

.validate_keys!(struct, hash) ⇒ Object

Guard for generated input-struct .coerce: a hash key that matches no prop raises (spellchecked) instead of silently dropping — a typo'd filter key must not become "match everything" on the wire.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graph_weaver/hints.rb', line 21

def self.validate_keys!(struct, hash)
  known = struct.props.keys.map(&:to_s)
  unknown = hash.keys.map(&:to_s) - known
  return if unknown.empty?

  hints = unknown.map do |key|
    prop = GraphWeaver::Inflect.underscore(key)
    suggestion = if known.include?(prop)
      prop # a wire-cased key — the exact snake_case prop exists
    elsif defined?(DidYouMean::SpellChecker)
      DidYouMean::SpellChecker.new(dictionary: known).correct(prop).first
    end
    suggestion ? "#{key} (did you mean '#{suggestion}'?)" : key
  end
  raise ArgumentError, "unknown key(s) for #{struct}: #{hints.join(", ")}"
end