graphql-client 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/graphql/client.rb +65 -9
- data/lib/graphql/client/http.rb +41 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca22da5b31b85d955d2a209f48e5767c2b953848
|
4
|
+
data.tar.gz: 0150a203bb5fe856b6ea6c15bd941056f86002ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c41fc037d158823686bf372addbc0b6592a87f2e9b470f0fb7302393cc89ad42f452bcb87bfdb142887437c42438e95d20bd236045088e11077cb48dba99ee38
|
7
|
+
data.tar.gz: 99f425fee8b8e70e876cc8d262befa594f2a2b182cad5b4b6b27a392139e1ecf84a5c6726f0719c96292afe17053ad16060be7ea73e8d414c931315cbddd0fab
|
data/lib/graphql/client.rb
CHANGED
@@ -38,8 +38,25 @@ module GraphQL
|
|
38
38
|
|
39
39
|
attr_accessor :document_tracking_enabled
|
40
40
|
|
41
|
+
def self.load_schema(schema)
|
42
|
+
case schema
|
43
|
+
when GraphQL::Schema
|
44
|
+
schema
|
45
|
+
when Hash
|
46
|
+
GraphQL::Schema::Loader.load(schema)
|
47
|
+
when String
|
48
|
+
if schema.end_with?(".json")
|
49
|
+
load_schema(File.read(schema))
|
50
|
+
else
|
51
|
+
load_schema(JSON.parse(schema))
|
52
|
+
end
|
53
|
+
else
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
41
58
|
def initialize(schema: nil, fetch: nil)
|
42
|
-
@schema = schema
|
59
|
+
@schema = self.class.load_schema(schema)
|
43
60
|
@fetch = fetch
|
44
61
|
@document = GraphQL::Language::Nodes::Document.new(definitions: [])
|
45
62
|
@document_tracking_enabled = false
|
@@ -138,7 +155,7 @@ module GraphQL
|
|
138
155
|
if @schema
|
139
156
|
rules = GraphQL::StaticValidation::ALL_RULES - [GraphQL::StaticValidation::FragmentsAreUsed]
|
140
157
|
validator = GraphQL::StaticValidation::Validator.new(schema: @schema, rules: rules)
|
141
|
-
query = Query.new(@schema, document: document_dependencies)
|
158
|
+
query = GraphQL::Query.new(@schema, document: document_dependencies)
|
142
159
|
|
143
160
|
errors = validator.validate(query)
|
144
161
|
errors.fetch(:errors).each do |error|
|
@@ -222,19 +239,51 @@ module GraphQL
|
|
222
239
|
end
|
223
240
|
end
|
224
241
|
|
242
|
+
class Query
|
243
|
+
attr_reader :document, :operation_name, :variables, :context
|
244
|
+
|
245
|
+
def initialize(document, operation_name: nil, variables: {}, context: {})
|
246
|
+
@document = document
|
247
|
+
@operation_name = operation_name
|
248
|
+
@variables = variables
|
249
|
+
@context = context
|
250
|
+
end
|
251
|
+
|
252
|
+
def to_s
|
253
|
+
document.to_query_string
|
254
|
+
end
|
255
|
+
|
256
|
+
def operation
|
257
|
+
document.definitions.find { |node| node.name == operation_name }
|
258
|
+
end
|
259
|
+
|
260
|
+
def operation_type
|
261
|
+
operation.operation_type
|
262
|
+
end
|
263
|
+
|
264
|
+
def payload
|
265
|
+
{
|
266
|
+
document: document,
|
267
|
+
operation_name: operation_name,
|
268
|
+
operation_type: operation_type,
|
269
|
+
variables: variables
|
270
|
+
}
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
225
274
|
def query(definition, variables: {}, context: {})
|
226
275
|
unless fetch
|
227
276
|
raise Error, "client network fetching not configured"
|
228
277
|
end
|
229
278
|
|
230
|
-
|
231
|
-
document: definition.document,
|
279
|
+
query = Query.new(definition.document,
|
232
280
|
operation_name: definition.operation_name,
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
281
|
+
variables: variables,
|
282
|
+
context: context
|
283
|
+
)
|
284
|
+
|
285
|
+
result = ActiveSupport::Notifications.instrument("query.graphql", query.payload) do
|
286
|
+
fetch.call(query)
|
238
287
|
end
|
239
288
|
|
240
289
|
data, errors, extensions = result.values_at("data", "errors", "extensions")
|
@@ -260,6 +309,13 @@ module GraphQL
|
|
260
309
|
end
|
261
310
|
end
|
262
311
|
|
312
|
+
IntrospectionDocument = GraphQL.parse(GraphQL::Introspection::INTROSPECTION_QUERY).deep_freeze
|
313
|
+
IntrospectionQuery = Query.new(IntrospectionDocument)
|
314
|
+
|
315
|
+
def fetch_schema
|
316
|
+
fetch.call(IntrospectionQuery)
|
317
|
+
end
|
318
|
+
|
263
319
|
private
|
264
320
|
module LazyName
|
265
321
|
def name
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "json"
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
module GraphQL
|
6
|
+
class Client
|
7
|
+
class HTTP
|
8
|
+
attr_reader :uri
|
9
|
+
|
10
|
+
# GraphQL::Client::HTTP.new("http://graphql-swapi.parseapp.com/")
|
11
|
+
def initialize(uri, &block)
|
12
|
+
@uri = ::URI.parse(uri)
|
13
|
+
class_eval(&block) if block_given?
|
14
|
+
end
|
15
|
+
|
16
|
+
def headers(query)
|
17
|
+
{}
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(query)
|
21
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
22
|
+
http.use_ssl = uri.scheme == "https"
|
23
|
+
|
24
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
25
|
+
|
26
|
+
request["Accept"] = "application/json"
|
27
|
+
request["Content-Type"] = "application/json"
|
28
|
+
headers(query).each { |name, value| request[name] = value }
|
29
|
+
|
30
|
+
body = {}
|
31
|
+
body["query"] = query.to_s
|
32
|
+
body["variables"] = JSON.generate(query.variables) if query.variables.any?
|
33
|
+
body["operationName"] = query.operation_name if query.operation_name
|
34
|
+
request.body = JSON.generate(body)
|
35
|
+
|
36
|
+
response = http.request(request)
|
37
|
+
JSON.parse(response.body)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- LICENSE
|
102
102
|
- lib/graphql/client.rb
|
103
103
|
- lib/graphql/client/erubis.rb
|
104
|
+
- lib/graphql/client/http.rb
|
104
105
|
- lib/graphql/client/log_subscriber.rb
|
105
106
|
- lib/graphql/client/query_result.rb
|
106
107
|
- lib/graphql/client/railtie.rb
|