Class: GraphQL::Client::Definition

Inherits:
Module
  • Object
show all
Defined in:
lib/graphql/client.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(node:, document:, schema:, document_types:) ⇒ Definition

Returns a new instance of Definition.



100
101
102
103
104
105
# File 'lib/graphql/client.rb', line 100

def initialize(node:, document:, schema:, document_types:)
  @definition_node = node
  @document = document
  @schema = schema
  @document_types = document_types
end

Instance Attribute Details

#definition_nodeObject (readonly)

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

Returns OperationDefinition or FragmentDefinition object.



111
112
113
# File 'lib/graphql/client.rb', line 111

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.



135
136
137
# File 'lib/graphql/client.rb', line 135

def document
  @document
end

#schemaObject (readonly)

Returns the value of attribute schema.



137
138
139
# File 'lib/graphql/client.rb', line 137

def schema
  @schema
end

Class Method Details

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/graphql/client.rb', line 89

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



120
121
122
123
124
125
126
127
128
# File 'lib/graphql/client.rb', line 120

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

#new(*args) ⇒ Object



139
140
141
# File 'lib/graphql/client.rb', line 139

def new(*args)
  type.new(*args)
end

#typeObject



143
144
145
146
# File 'lib/graphql/client.rb', line 143

def type
  # TODO: Fix type indirection
  @type ||= GraphQL::Client::QueryResult.wrap(definition_node, name: "#{name}.type", types: @document_types)
end