Module: GraphQL::Client::Query::HasSelectionSet

Included in:
Field, Fragment, InlineFragment, Operation
Defined in:
lib/graphql_client/query/has_selection_set.rb

Constant Summary collapse

ID_FIELD_NAME =
'id'
INVALID_FIELD =
Class.new(StandardError)
UNDEFINED_FRAGMENT =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#selection_setObject

Returns the value of attribute selection_set.



11
12
13
# File 'lib/graphql_client/query/has_selection_set.rb', line 11

def selection_set
  @selection_set
end

Instance Method Details

#add_connection(connection_name, as: nil, **arguments) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql_client/query/has_selection_set.rb', line 13

def add_connection(connection_name, as: nil, **arguments)
  node_field = nil

  add_field(connection_name, as: as, **arguments) do |connection|
    connection.add_field('edges') do |edges|
      edges.add_field('cursor')
      edges.add_field('node') do |node|
        node_field = node
        yield node
      end
    end

    connection.add_field('pageInfo') do |page_info|
      page_info.add_field('hasPreviousPage')
      page_info.add_field('hasNextPage')
    end
  end

  node_field
end

#add_field(field_name, as: nil, **arguments) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphql_client/query/has_selection_set.rb', line 34

def add_field(field_name, as: nil, **arguments)
  field_defn = resolve(field_name)
  field = Field.new(field_defn, arguments: arguments, as: as, document: document)
  selection_set.add_field(field)

  field.add_field(ID_FIELD_NAME) if field.node?

  if block_given?
    yield field
  else
    field
  end
end

#add_fields(*field_names) ⇒ Object



48
49
50
51
52
# File 'lib/graphql_client/query/has_selection_set.rb', line 48

def add_fields(*field_names)
  field_names.each do |field_name|
    add_field(field_name)
  end
end

#add_fragment(fragment_name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/graphql_client/query/has_selection_set.rb', line 54

def add_fragment(fragment_name)
  fragment = document.fragments.fetch(fragment_name) do
    raise UNDEFINED_FRAGMENT, "a fragment named #{fragment_name} has not been defined"
  end

  selection_set.add_fragment(fragment)
end