Class: GraphQL::Client::Definition

Inherits:
Module
  • Object
show all
Defined in:
lib/graphql/client/definition.rb

Overview

Definitions are constructed by Client.parse and wrap a parsed AST of the query string as well as hold references to any external query definition dependencies.

Definitions MUST be assigned to a constant.

Direct Known Subclasses

FragmentDefinition, OperationDefinition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, document:, source_document:, irep_node:, source_location:) ⇒ Definition

Returns a new instance of Definition.



28
29
30
31
32
33
34
35
# File 'lib/graphql/client/definition.rb', line 28

def initialize(client:, document:, source_document:, irep_node:, source_location:)
  @client = client
  @document = document
  @source_document = source_document
  @definition_node = irep_node.ast_node
  @source_location = source_location
  @schema_class = client.types.define_class(self, irep_node, irep_node.return_type)
end

Instance Attribute Details

#clientObject (readonly)

Internal: Get associated owner GraphQL::Client instance.



38
39
40
# File 'lib/graphql/client/definition.rb', line 38

def client
  @client
end

#definition_nodeObject (readonly)

Internal: Get underlying operation or fragment definition AST node for definition.

Returns OperationDefinition or FragmentDefinition object.



49
50
51
# File 'lib/graphql/client/definition.rb', line 49

def definition_node
  @definition_node
end

#documentObject (readonly)

Public: Get document with only the definitions needed to perform this operation.

Returns GraphQL::Language::Nodes::Document with one OperationDefinition and any FragmentDefinition dependencies.



79
80
81
# File 'lib/graphql/client/definition.rb', line 79

def document
  @document
end

#schema_classObject (readonly)

Internal root schema class for definition. Returns GraphQL::Client::Schema::ObjectType or GraphQL::Client::Schema::PossibleTypes.



43
44
45
# File 'lib/graphql/client/definition.rb', line 43

def schema_class
  @schema_class
end

#source_documentObject (readonly)

Internal: Get original document that created this definition, without any additional dependencies.

Returns GraphQL::Language::Nodes::Document.



55
56
57
# File 'lib/graphql/client/definition.rb', line 55

def source_document
  @source_document
end

#source_locationObject (readonly)

Public: Returns the Ruby source filename and line number containing this definition was not defined in Ruby.

Returns Array pair of [String, Fixnum].



85
86
87
# File 'lib/graphql/client/definition.rb', line 85

def source_location
  @source_location
end

Class Method Details

.for(irep_node:, **kargs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql/client/definition.rb', line 17

def self.for(irep_node:, **kargs)
  case irep_node.ast_node
  when Language::Nodes::OperationDefinition
    OperationDefinition.new(irep_node: irep_node, **kargs)
  when Language::Nodes::FragmentDefinition
    FragmentDefinition.new(irep_node: irep_node, **kargs)
  else
    raise TypeError, "expected node to be a definition type, but was #{irep_node.ast_node.class}"
  end
end

Instance Method Details

#definition_nameObject

Public: Global name of definition in client document.

Returns a GraphQL safe name of the Ruby constant String.

"Users::UserQuery" #=> "Users__UserQuery"

Returns String.



64
65
66
67
68
69
70
71
72
# File 'lib/graphql/client/definition.rb', line 64

def definition_name
  return @definition_name if defined?(@definition_name)

  if name
    @definition_name = name.gsub("::", "__").freeze
  else
    "#{self.class.name}_#{object_id}".gsub("::", "__").freeze
  end
end

#indexesObject

Internal: Nodes AST indexes.



118
119
120
121
122
123
124
125
126
# File 'lib/graphql/client/definition.rb', line 118

def indexes
  @indexes ||= begin
    visitor = GraphQL::Language::Visitor.new(document)
    definitions = index_node_definitions(visitor)
    spreads = index_spreads(visitor)
    visitor.visit
    { definitions: definitions, spreads: spreads }
  end
end

#new(obj, errors = Errors.new) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graphql/client/definition.rb', line 87

def new(obj, errors = Errors.new)
  case schema_class
  when GraphQL::Client::Schema::PossibleTypes
    case obj
    when NilClass
      nil
    else
      schema_class.cast(obj.to_h, obj.errors)
    end
  when GraphQL::Client::Schema::ObjectType
    case obj
    when NilClass, schema_class
      obj
    when Hash
      schema_class.new(obj, errors)
    else
      if obj.class.is_a?(GraphQL::Client::Schema::ObjectType)
        unless obj.class._spreads.include?(definition_node.name)
          raise TypeError, "#{definition_node.name} is not included in #{obj.class.source_definition.name}"
        end
        schema_class.cast(obj.to_h, obj.errors)
      else
        raise TypeError, "unexpected #{obj.class}"
      end
    end
  else
    raise TypeError, "unexpected #{schema_class}"
  end
end