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
-
.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.
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/graph_weaver/hints.rb', line 43 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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/graph_weaver/hints.rb', line 22 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 else GraphWeaver.did_you_mean(known, prop) end suggestion ? "#{key} (did you mean '#{suggestion}'?)" : key end raise GraphWeaver::InputError.new( "unknown key(s) for #{struct}: #{hints.join(", ")}", field: unknown.join(", "), struct: struct, ) end |