Class: Graphlient::Query::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlient/query/directive.rb

Overview

Value object returned by _directive_name(args) in the DSL.

Rather than writing directly to the query string (which would produce the wrong position -- before the field name), _skip / _include etc. return a Directive instance. append_node / spread / on detect Directive objects in their argument list and place them AFTER the field name, producing correct GraphQL output:

feeInCents _skip(if: :skip_fee)
-> feeInCents @skip(if: $skip_fee)

spread :InvoiceFields, _skip(if: :x)
-> ...InvoiceFields @skip(if: $x)

spread(_skip(if: :x), on: :DraftInvoice) { draft_id }
-> ... on DraftInvoice @skip(if: $x) { draftId }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args = {}) ⇒ Directive

Returns a new instance of Directive.



22
23
24
25
# File 'lib/graphlient/query/directive.rb', line 22

def initialize(name, args = {})
  @name = name.to_s.delete_prefix('_').freeze
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



20
21
22
# File 'lib/graphlient/query/directive.rb', line 20

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/graphlient/query/directive.rb', line 20

def name
  @name
end

Instance Method Details

#to_sObject



27
28
29
30
31
32
# File 'lib/graphlient/query/directive.rb', line 27

def to_s
  return "@#{name}" if args.empty?

  formatted = args.map { |k, v| "#{k}: #{format_value(v)}" }.join(', ')
  "@#{name}(#{formatted})"
end