Class: Graphlyte::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlyte/selector.rb

Overview

A tool for simple editing of GraphQL queries using a path-based API

Usage:

editor = Selector.new
editor.at('project.pipelines.nodes.status', &:remove)
editor.at('project.pipelines.nodes') do |node|
  node.append do
    downstream do
      nodes { active }
    end
  end

editor.edit(doc)

Defined Under Namespace

Classes: SelectAction

Instance Method Summary collapse

Constructor Details

#initializeSelector

Returns a new instance of Selector.



23
24
25
# File 'lib/graphlyte/selector.rb', line 23

def initialize
  @actions = {}
end

Instance Method Details

#at(path, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/graphlyte/selector.rb', line 27

def at(path, &block)
  raise ArgumentError 'block not given' unless block_given?

  @actions[path] = block

  self
end

#edit(doc) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/graphlyte/selector.rb', line 35

def edit(doc)
  editor = Editor.new.on_field do |field, action|
    edit_field(field, action)
  end

  editor.edit(doc)

  doc
end

#edit_field(field, action) ⇒ Object



45
46
47
48
49
50
# File 'lib/graphlyte/selector.rb', line 45

def edit_field(field, action)
  key = action.path.map { _1.name if _1.is_a?(Syntax::Field) }.compact.join('.')
  block = @actions[key]

  block&.call(SelectAction.new(field, action))
end