Class: GraphQL::Client::Definition
- Inherits:
-
Module
- Object
- Module
- GraphQL::Client::Definition
- 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
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Internal: Get associated owner GraphQL::Client instance.
-
#definition_node ⇒ Object
readonly
Internal: Get underlying operation or fragment defintion AST node for definition.
-
#document ⇒ Object
readonly
Public: Get document with only the definitions needed to perform this operation.
-
#schema_class ⇒ Object
readonly
Internal root schema class for defintion.
-
#source_location ⇒ Object
readonly
Public: Returns the Ruby source filename and line number containing this definition was not defined in Ruby.
Class Method Summary collapse
Instance Method Summary collapse
-
#definition_name ⇒ Object
Public: Global name of definition in client document.
-
#indexes ⇒ Object
Internal: Nodes AST indexes.
-
#initialize(client:, document:, irep_node:, source_location:) ⇒ Definition
constructor
A new instance of Definition.
- #new(obj, errors = Errors.new) ⇒ Object
Constructor Details
#initialize(client:, document:, irep_node:, source_location:) ⇒ Definition
Returns a new instance of Definition.
28 29 30 31 32 33 34 |
# File 'lib/graphql/client/definition.rb', line 28 def initialize(client:, document:, irep_node:, source_location:) @client = client @document = 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
#client ⇒ Object (readonly)
Internal: Get associated owner GraphQL::Client instance.
37 38 39 |
# File 'lib/graphql/client/definition.rb', line 37 def client @client end |
#definition_node ⇒ Object (readonly)
Internal: Get underlying operation or fragment defintion AST node for definition.
Returns OperationDefinition or FragmentDefinition object.
48 49 50 |
# File 'lib/graphql/client/definition.rb', line 48 def definition_node @definition_node end |
#document ⇒ Object (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.
72 73 74 |
# File 'lib/graphql/client/definition.rb', line 72 def document @document end |
#schema_class ⇒ Object (readonly)
Internal root schema class for defintion. Returns GraphQL::Client::Schema::ObjectType or GraphQL::Client::Schema::PossibleTypes.
42 43 44 |
# File 'lib/graphql/client/definition.rb', line 42 def schema_class @schema_class end |
#source_location ⇒ Object (readonly)
Public: Returns the Ruby source filename and line number containing this definition was not defined in Ruby.
Returns Array pair of [String, Fixnum].
78 79 80 |
# File 'lib/graphql/client/definition.rb', line 78 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_name ⇒ Object
Public: Global name of definition in client document.
Returns a GraphQL safe name of the Ruby constant String.
"Users::UserQuery" #=> "Users__UserQuery"
Returns String.
57 58 59 60 61 62 63 64 65 |
# File 'lib/graphql/client/definition.rb', line 57 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 |
#indexes ⇒ Object
Internal: Nodes AST indexes.
111 112 113 114 115 116 117 118 119 |
# File 'lib/graphql/client/definition.rb', line 111 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/graphql/client/definition.rb', line 80 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 |