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:, ast_node:, source_location:) ⇒ Definition

Returns a new instance of Definition.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/graphql/client/definition.rb', line 28

def initialize(client:, document:, source_document:, ast_node:, source_location:)
  @client = client
  @document = document
  @source_document = source_document
  @definition_node = ast_node
  @source_location = source_location

  definition_type = case ast_node
  when GraphQL::Language::Nodes::OperationDefinition
    case ast_node.operation_type
    when "mutation"
      @client.schema.mutation
    when "subscription"
      @client.schema.subscription
    when "query", nil
      @client.schema.query
    else
      raise "Unexpected operation_type: #{ast_node.operation_type}"
    end
  when GraphQL::Language::Nodes::FragmentDefinition
    @client.schema.types[ast_node.type.name]
  else
    raise "Unexpected ast_node: #{ast_node}"
  end

  @schema_class = client.types.define_class(self, [ast_node], definition_type)

  # Clear cache only needed during initialization
  @indexes = nil
end

Instance Attribute Details

#clientObject (readonly)

Internal: Get associated owner GraphQL::Client instance.


60
61
62
# File 'lib/graphql/client/definition.rb', line 60

def client
  @client
end

#definition_nodeObject (readonly)

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

Returns OperationDefinition or FragmentDefinition object.


71
72
73
# File 'lib/graphql/client/definition.rb', line 71

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.


101
102
103
# File 'lib/graphql/client/definition.rb', line 101

def document
  @document
end

#schema_classObject (readonly)

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


65
66
67
# File 'lib/graphql/client/definition.rb', line 65

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.


77
78
79
# File 'lib/graphql/client/definition.rb', line 77

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].


107
108
109
# File 'lib/graphql/client/definition.rb', line 107

def source_location
  @source_location
end

Class Method Details

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


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

def self.for(ast_node:, **kargs)
  case ast_node
  when Language::Nodes::OperationDefinition
    OperationDefinition.new(ast_node: ast_node, **kargs)
  when Language::Nodes::FragmentDefinition
    FragmentDefinition.new(ast_node: ast_node, **kargs)
  else
    raise TypeError, "expected node to be a definition type, but was #{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.


86
87
88
89
90
91
92
93
94
# File 'lib/graphql/client/definition.rb', line 86

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.


133
134
135
136
137
138
139
140
141
# File 'lib/graphql/client/definition.rb', line 133

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


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/graphql/client/definition.rb', line 109

def new(obj, errors = Errors.new)
  case schema_class
  when GraphQL::Client::Schema::PossibleTypes
    case obj
    when NilClass
      obj
    else
      cast_object(obj)
    end
  when GraphQL::Client::Schema::ObjectType
    case obj
    when NilClass, schema_class
      obj
    when Hash
      schema_class.new(obj, errors)
    else
      cast_object(obj)
    end
  else
    raise TypeError, "unexpected #{schema_class}"
  end
end