Class: GraphQL::Client::Query::Field

Inherits:
Object
  • Object
show all
Includes:
AddInlineFragment, HasSelectionSet
Defined in:
lib/graphql_client/query/field.rb

Constant Summary collapse

INVALID_ARGUMENTS =
Class.new(StandardError)

Constants included from HasSelectionSet

HasSelectionSet::ID_FIELD_NAME, HasSelectionSet::INVALID_FIELD, HasSelectionSet::UNDEFINED_FRAGMENT

Constants included from AddInlineFragment

AddInlineFragment::INVALID_FRAGMENT_TARGET

Instance Attribute Summary collapse

Attributes included from HasSelectionSet

#selection_set

Instance Method Summary collapse

Methods included from HasSelectionSet

#add_connection, #add_field, #add_fields, #add_fragment

Methods included from AddInlineFragment

#add_inline_fragment

Constructor Details

#initialize(field_defn, document:, arguments: {}, as: nil) ⇒ Field

Returns a new instance of Field.



14
15
16
17
18
19
20
# File 'lib/graphql_client/query/field.rb', line 14

def initialize(field_defn, document:, arguments: {}, as: nil)
  @field_defn = field_defn
  @document = document
  @arguments = validate_arguments(arguments)
  @as = as
  @selection_set = SelectionSet.new
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



12
13
14
# File 'lib/graphql_client/query/field.rb', line 12

def arguments
  @arguments
end

#asObject (readonly)

Returns the value of attribute as.



12
13
14
# File 'lib/graphql_client/query/field.rb', line 12

def as
  @as
end

#documentObject (readonly)

Returns the value of attribute document.



12
13
14
# File 'lib/graphql_client/query/field.rb', line 12

def document
  @document
end

#field_defnObject (readonly)

Returns the value of attribute field_defn.



12
13
14
# File 'lib/graphql_client/query/field.rb', line 12

def field_defn
  @field_defn
end

Instance Method Details

#add_arguments(**arguments) ⇒ Object



22
23
24
25
# File 'lib/graphql_client/query/field.rb', line 22

def add_arguments(**arguments)
  new_arguments = validate_arguments(arguments)
  @arguments.merge!(new_arguments)
end

#aliased?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/graphql_client/query/field.rb', line 27

def aliased?
  name != field_defn.name
end

#connection?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/graphql_client/query/field.rb', line 35

def connection?
  resolver_type.name.to_s.end_with?('Connection')
end

#nameObject



39
40
41
# File 'lib/graphql_client/query/field.rb', line 39

def name
  as || field_defn.name
end

#node?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/graphql_client/query/field.rb', line 43

def node?
  field_defn.name == 'Node' || (resolver_type.object? && resolver_type.implement?('Node'))
end

#resolver_typeObject



47
48
49
# File 'lib/graphql_client/query/field.rb', line 47

def resolver_type
  @resolver_type ||= schema.type(field_defn.type.unwrap.name)
end

#schemaObject



51
52
53
# File 'lib/graphql_client/query/field.rb', line 51

def schema
  document.schema
end

#to_query(indent: '') ⇒ Object Also known as: to_s



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/graphql_client/query/field.rb', line 55

def to_query(indent: '')
  indent.dup.tap do |query_string|
    query_string << "#{as}: " if aliased?
    query_string << field_defn.name
    query_string << "(#{arguments_string.join(', ')})" if arguments.any?

    unless selection_set.empty?
      query_string << " {\n"
      query_string << selection_set.to_query(indent)
      query_string << "\n#{indent}}"
    end
  end
end