graphql-client 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql/client.rb +83 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aea0a7a43074a4ae819e473986778336afe5bbb4
|
4
|
+
data.tar.gz: 3d20ddd2ab550d0bf819ddb456384055ce57de64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fca77d34cbe78cba96432c777204d147405640a73502473c68c250ecc5b22304a64981ade4202cbacc9b1fa46f16c0fe4517f5eafed439e826d0a3c3a9cf543f
|
7
|
+
data.tar.gz: 6031bf53f6d4b1cfc3e3e8419695325ada554f56cbec39d57976be7ce8ab5d01408c71ec6292360c7586869b23f4981e20b86cf2fae69a169a93ecca7dbfe615
|
data/lib/graphql/client.rb
CHANGED
@@ -9,11 +9,23 @@ module GraphQL
|
|
9
9
|
class Error < StandardError; end
|
10
10
|
class ValidationError < Error; end
|
11
11
|
|
12
|
-
|
12
|
+
class ResponseError < Error
|
13
|
+
def initialize(definition, error)
|
14
|
+
@request_definition = definition
|
15
|
+
@locations = error["locations"]
|
16
|
+
super error["message"]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :schema, :fetch
|
21
|
+
|
22
|
+
attr_accessor :document_tracking_enabled
|
13
23
|
|
14
|
-
def initialize(schema: nil)
|
24
|
+
def initialize(schema: nil, fetch: nil)
|
15
25
|
@schema = schema
|
26
|
+
@fetch = fetch
|
16
27
|
@document = GraphQL::Language::Nodes::Document.new(definitions: [])
|
28
|
+
@document_tracking_enabled = false
|
17
29
|
end
|
18
30
|
|
19
31
|
class Definition < Module
|
@@ -39,13 +51,6 @@ module GraphQL
|
|
39
51
|
# Returns OperationDefinition or FragmentDefinition object.
|
40
52
|
attr_reader :definition_node
|
41
53
|
|
42
|
-
# Public: Ruby constant name of definition.
|
43
|
-
#
|
44
|
-
# Returns String or errors if definition was not assigned to a constant.
|
45
|
-
def name
|
46
|
-
@name ||= super || raise(RuntimeError, "definition must be assigned to a constant")
|
47
|
-
end
|
48
|
-
|
49
54
|
# Public: Global name of definition in client document.
|
50
55
|
#
|
51
56
|
# Returns a GraphQL safe name of the Ruby constant String.
|
@@ -54,7 +59,7 @@ module GraphQL
|
|
54
59
|
#
|
55
60
|
# Returns String.
|
56
61
|
def definition_name
|
57
|
-
@definition_name ||= name.gsub("::", "__").freeze
|
62
|
+
@definition_name ||= (name || "#{self.class.name}_#{object_id}").gsub("::", "__").freeze
|
58
63
|
end
|
59
64
|
|
60
65
|
# Public: Get document with only the definitions needed to perform this
|
@@ -140,7 +145,9 @@ module GraphQL
|
|
140
145
|
|
141
146
|
doc.deep_freeze
|
142
147
|
|
143
|
-
|
148
|
+
if document_tracking_enabled
|
149
|
+
self.document.definitions.concat(doc.definitions)
|
150
|
+
end
|
144
151
|
|
145
152
|
if definitions[nil]
|
146
153
|
definitions[nil]
|
@@ -157,6 +164,71 @@ module GraphQL
|
|
157
164
|
@document
|
158
165
|
end
|
159
166
|
|
167
|
+
class Response
|
168
|
+
attr_reader :extensions
|
169
|
+
|
170
|
+
def initialize(extensions: nil)
|
171
|
+
@extensions = extensions || {}
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class SuccessfulResponse < Response
|
176
|
+
attr_reader :data
|
177
|
+
|
178
|
+
def initialize(data:, **kargs)
|
179
|
+
@data = data
|
180
|
+
super(**kargs)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class PartialResponse < SuccessfulResponse
|
185
|
+
attr_reader :errors
|
186
|
+
|
187
|
+
def initialize(errors:, **kargs)
|
188
|
+
@errors = errors
|
189
|
+
super(**kargs)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class FailedResponse < Response
|
194
|
+
attr_reader :errors
|
195
|
+
|
196
|
+
def initialize(errors:, **kargs)
|
197
|
+
@errors = errors
|
198
|
+
super(**kargs)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def query(definition, variables: {})
|
203
|
+
unless fetch
|
204
|
+
raise Error, "client network fetching not configured"
|
205
|
+
end
|
206
|
+
|
207
|
+
document = definition.document
|
208
|
+
result = fetch.call(document, variables)
|
209
|
+
data, errors, extensions = result.values_at("data", "errors", "extensions")
|
210
|
+
|
211
|
+
if data && errors
|
212
|
+
PartialResponse.new(
|
213
|
+
data: definition.new(data),
|
214
|
+
errors: errors.map { |error| ResponseError.new(definition, error) },
|
215
|
+
extensions: extensions
|
216
|
+
)
|
217
|
+
elsif data && !errors
|
218
|
+
SuccessfulResponse.new(
|
219
|
+
data: definition.new(data),
|
220
|
+
extensions: extensions
|
221
|
+
)
|
222
|
+
elsif !data && errors
|
223
|
+
FailedResponse.new(
|
224
|
+
errors: errors.map { |error| ResponseError.new(definition, error) },
|
225
|
+
extensions: extensions
|
226
|
+
)
|
227
|
+
else
|
228
|
+
raise Error, "invalid GraphQL response, expected data or errors"
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
160
232
|
private
|
161
233
|
module LazyName
|
162
234
|
def name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|