Class: GraphQL::Client
- Inherits:
-
Object
- Object
- GraphQL::Client
- Defined in:
- lib/graphql/client.rb,
lib/graphql/client/query_result.rb
Defined Under Namespace
Modules: LazyName Classes: Definition, Error, FailedResponse, FragmentDefinition, OperationDefinition, PartialResponse, QueryResult, Response, ResponseError, SuccessfulResponse, ValidationError
Instance Attribute Summary collapse
-
#document_tracking_enabled ⇒ Object
Returns the value of attribute document_tracking_enabled.
-
#fetch ⇒ Object
readonly
Returns the value of attribute fetch.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #document ⇒ Object
-
#initialize(schema: nil, fetch: nil) ⇒ Client
constructor
A new instance of Client.
- #parse(str) ⇒ Object
- #query(definition, variables: {}) ⇒ Object
Constructor Details
#initialize(schema: nil, fetch: nil) ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 29 |
# File 'lib/graphql/client.rb', line 24 def initialize(schema: nil, fetch: nil) @schema = schema @fetch = fetch @document = GraphQL::Language::Nodes::Document.new(definitions: []) @document_tracking_enabled = false end |
Instance Attribute Details
#document_tracking_enabled ⇒ Object
Returns the value of attribute document_tracking_enabled.
22 23 24 |
# File 'lib/graphql/client.rb', line 22 def document_tracking_enabled @document_tracking_enabled end |
#fetch ⇒ Object (readonly)
Returns the value of attribute fetch.
20 21 22 |
# File 'lib/graphql/client.rb', line 20 def fetch @fetch end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
20 21 22 |
# File 'lib/graphql/client.rb', line 20 def schema @schema end |
Instance Method Details
#document ⇒ Object
163 164 165 |
# File 'lib/graphql/client.rb', line 163 def document @document end |
#parse(str) ⇒ Object
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/graphql/client.rb', line 90 def parse(str) definition_dependencies = Set.new str = str.gsub(/\.\.\.([a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)+)/) { |m| const_name = $1 case fragment = ActiveSupport::Inflector.safe_constantize(const_name) when FragmentDefinition definition_dependencies.merge(fragment.document.definitions) "...#{fragment.definition_name}" when nil raise NameError, "uninitialized constant #{const_name}\n#{str}" else raise TypeError, "expected #{const_name} to be a #{FragmentDefinition}, but was a #{fragment.class}" end } doc = GraphQL.parse(str) doc.definitions.each do |node| node.name ||= "__anonymous__" end definition_dependencies.merge(doc.definitions) document_dependencies = Language::Nodes::Document.new(definitions: definition_dependencies.to_a) if @schema rules = GraphQL::StaticValidation::ALL_RULES - [GraphQL::StaticValidation::FragmentsAreUsed] validator = GraphQL::StaticValidation::Validator.new(schema: @schema, rules: rules) query = Query.new(@schema, document: document_dependencies) errors = validator.validate(query) errors.fetch(:errors).each do |error| raise ValidationError, error["message"] + "\n\n" + str end end definitions = {} doc.definitions.each do |node| node.name = nil if node.name == "__anonymous__" sliced_document = Language::OperationSlice.slice(document_dependencies, node.name) definition = Definition.for(node: node, document: sliced_document) definitions[node.name] = definition end rename_node = -> (node, parent) { if definition = definitions[node.name] node.extend(LazyName) node.name = -> { definition.definition_name } end } visitor = Language::Visitor.new(doc) visitor[Language::Nodes::FragmentDefinition].leave << rename_node visitor[Language::Nodes::OperationDefinition].leave << rename_node visitor[Language::Nodes::FragmentSpread].leave << rename_node visitor.visit doc.deep_freeze if document_tracking_enabled self.document.definitions.concat(doc.definitions) end if definitions[nil] definitions[nil] else Module.new do definitions.each do |name, definition| const_set(name, definition) end end end end |
#query(definition, variables: {}) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/graphql/client.rb', line 202 def query(definition, variables: {}) unless fetch raise Error, "client network fetching not configured" end document = definition.document result = fetch.call(document, variables) data, errors, extensions = result.values_at("data", "errors", "extensions") if data && errors PartialResponse.new( data: definition.new(data), errors: errors.map { |error| ResponseError.new(definition, error) }, extensions: extensions ) elsif data && !errors SuccessfulResponse.new( data: definition.new(data), extensions: extensions ) elsif !data && errors FailedResponse.new( errors: errors.map { |error| ResponseError.new(definition, error) }, extensions: extensions ) else raise Error, "invalid GraphQL response, expected data or errors" end end |