Class: GraphQL::Client
- Inherits:
-
Object
- Object
- GraphQL::Client
- Defined in:
- lib/graphql/client.rb,
lib/graphql/client/http.rb,
lib/graphql/client/error.rb,
lib/graphql/client/query.rb,
lib/graphql/client/erubis.rb,
lib/graphql/client/railtie.rb,
lib/graphql/client/response.rb,
lib/graphql/client/view_module.rb,
lib/graphql/client/query_result.rb,
lib/graphql/client/log_subscriber.rb
Defined Under Namespace
Modules: LazyName, ViewModule Classes: Definition, Error, Erubis, FailedResponse, FragmentDefinition, HTTP, LogSubscriber, OperationDefinition, PartialResponse, Query, QueryResult, Railtie, Response, ResponseError, ResponseErrors, SuccessfulResponse, ValidationError
Constant Summary collapse
- IntrospectionDocument =
GraphQL.parse(GraphQL::Introspection::INTROSPECTION_QUERY).deep_freeze
- IntrospectionQuery =
Query.new(IntrospectionDocument)
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#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.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_schema ⇒ Object
-
#initialize(schema: nil, fetch: nil) ⇒ Client
constructor
A new instance of Client.
- #parse(str, filename = nil, lineno = nil) ⇒ Object
- #query(definition, variables: {}, context: {}) ⇒ Object
Constructor Details
#initialize(schema: nil, fetch: nil) ⇒ Client
Returns a new instance of Client.
34 35 36 37 38 39 |
# File 'lib/graphql/client.rb', line 34 def initialize(schema: nil, fetch: nil) @schema = self.class.load_schema(schema) @fetch = fetch @document = GraphQL::Language::Nodes::Document.new(definitions: []) @document_tracking_enabled = false end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
195 196 197 |
# File 'lib/graphql/client.rb', line 195 def document @document end |
#document_tracking_enabled ⇒ Object
Returns the value of attribute document_tracking_enabled.
17 18 19 |
# File 'lib/graphql/client.rb', line 17 def document_tracking_enabled @document_tracking_enabled end |
#fetch ⇒ Object (readonly)
Returns the value of attribute fetch.
15 16 17 |
# File 'lib/graphql/client.rb', line 15 def fetch @fetch end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
15 16 17 |
# File 'lib/graphql/client.rb', line 15 def schema @schema end |
Class Method Details
.load_schema(schema) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/graphql/client.rb', line 19 def self.load_schema(schema) case schema when GraphQL::Schema schema when Hash GraphQL::Schema::Loader.load(schema) when String if schema.end_with?(".json") load_schema(File.read(schema)) else load_schema(JSON.parse(schema)) end end end |
Instance Method Details
#fetch_schema ⇒ Object
235 236 237 |
# File 'lib/graphql/client.rb', line 235 def fetch_schema fetch.call(IntrospectionQuery) end |
#parse(str, filename = nil, lineno = nil) ⇒ Object
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/graphql/client.rb', line 107 def parse(str, filename = nil, lineno = nil) if filename.nil? || lineno.nil? filename, lineno, = caller(1, 1).first.split(":", 3) lineno = lineno.to_i end definition_dependencies = Set.new str = str.gsub(/\.\.\.([a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)+)/) do match = Regexp.last_match const_name = match[1] case fragment = ActiveSupport::Inflector.safe_constantize(const_name) when FragmentDefinition definition_dependencies.merge(fragment.document.definitions) "...#{fragment.definition_name}" else if fragment error = TypeError.new("expected #{const_name} to be a #{FragmentDefinition}, but was a #{fragment.class}") else error = NameError.new("uninitialized constant #{const_name}") end if filename && lineno error.set_backtrace(["#{filename}:#{lineno + match.pre_match.count("\n") + 1}"] + caller) end raise error end end doc = GraphQL.parse(str) doc.definitions.each do |node| node.name ||= "__anonymous__" end document_dependencies = Language::Nodes::Document.new(definitions: doc.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 = GraphQL::Query.new(@schema, document: document_dependencies) errors = validator.validate(query) errors.fetch(:errors).each do |error| validation_line = error["locations"][0]["line"] error = ValidationError.new(error["message"]) error.set_backtrace(["#{filename}:#{lineno + validation_line}"] + caller) if filename && lineno raise error 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) do definition = definitions[node.name] if definition node.extend(LazyName) node.name = -> { definition.definition_name } end 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 document.definitions.concat(doc.definitions) if document_tracking_enabled 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: {}, context: {}) ⇒ Object
197 198 199 200 201 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 197 def query(definition, variables: {}, context: {}) raise Error, "client network fetching not configured" unless fetch query = Query.new(definition.document, operation_name: definition.operation_name, variables: variables, context: context) result = ActiveSupport::Notifications.instrument("query.graphql", query.payload) do fetch.call(query) end data, errors, extensions = result.values_at("data", "errors", "extensions") if data && errors PartialResponse.new( data: definition.new(data), errors: ResponseErrors.new(definition, errors), extensions: extensions ) elsif data && !errors SuccessfulResponse.new( data: definition.new(data), extensions: extensions ) elsif !data && errors FailedResponse.new( errors: ResponseErrors.new(definition, errors), extensions: extensions ) else raise Error, "invalid GraphQL response, expected data or errors" end end |