graphql-client 0.0.14 → 0.0.15
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 +26 -4
- data/lib/graphql/client/erubis.rb +17 -0
- data/lib/graphql/client/view_module.rb +145 -0
- metadata +28 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82a2932b4b6e2c7a9bda10a683f88835c6e21a2c
|
4
|
+
data.tar.gz: c01bfc95eee130530808e40a3c83b32f2a3dcb0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 541ce88dda647e3a7f38240d3f53144a5fd2b0739b70b514016419db8d1edb036dd8e8f3fa431f73943dc309e6c818cd7be6bb0f2182515b98e3cf9201439f27
|
7
|
+
data.tar.gz: f612df0408bfdfdef608faf5edc01869c8bad3dc05787c6942bf1db7902bdc3f903d4d5cd9b4d7d45499a936a4e43e6aa83c4a1f6e9109b49bad5ff513e46302
|
data/lib/graphql/client.rb
CHANGED
@@ -17,6 +17,22 @@ module GraphQL
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
class ResponseErrors < Error
|
21
|
+
include Enumerable
|
22
|
+
|
23
|
+
attr_reader :errors
|
24
|
+
|
25
|
+
def initialize(definition, errors)
|
26
|
+
@request_definition = definition
|
27
|
+
@errors = errors.map { |error| ResponseError.new(definition, error) }
|
28
|
+
super @errors.map(&:message).join(", ")
|
29
|
+
end
|
30
|
+
|
31
|
+
def each(&block)
|
32
|
+
errors.each(&block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
20
36
|
attr_reader :schema, :fetch
|
21
37
|
|
22
38
|
attr_accessor :document_tracking_enabled
|
@@ -59,7 +75,13 @@ module GraphQL
|
|
59
75
|
#
|
60
76
|
# Returns String.
|
61
77
|
def definition_name
|
62
|
-
@definition_name
|
78
|
+
return @definition_name if defined?(@definition_name)
|
79
|
+
|
80
|
+
if name
|
81
|
+
@definition_name = name.gsub("::", "__").freeze
|
82
|
+
else
|
83
|
+
"#{self.class.name}_#{object_id}".gsub("::", "__").freeze
|
84
|
+
end
|
63
85
|
end
|
64
86
|
|
65
87
|
# Public: Get document with only the definitions needed to perform this
|
@@ -205,13 +227,13 @@ module GraphQL
|
|
205
227
|
end
|
206
228
|
|
207
229
|
document = definition.document
|
208
|
-
result = fetch.call(document, variables, context)
|
230
|
+
result = fetch.call(document, definition.operation_name, variables, context)
|
209
231
|
data, errors, extensions = result.values_at("data", "errors", "extensions")
|
210
232
|
|
211
233
|
if data && errors
|
212
234
|
PartialResponse.new(
|
213
235
|
data: definition.new(data),
|
214
|
-
errors:
|
236
|
+
errors: ResponseErrors.new(definition, errors),
|
215
237
|
extensions: extensions
|
216
238
|
)
|
217
239
|
elsif data && !errors
|
@@ -221,7 +243,7 @@ module GraphQL
|
|
221
243
|
)
|
222
244
|
elsif !data && errors
|
223
245
|
FailedResponse.new(
|
224
|
-
errors:
|
246
|
+
errors: ResponseErrors.new(definition, errors),
|
225
247
|
extensions: extensions
|
226
248
|
)
|
227
249
|
else
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "action_view"
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
class Client
|
5
|
+
class Erubis < ActionView::Template::Handlers::Erubis
|
6
|
+
def self.extract_graphql_sections(src)
|
7
|
+
src.scan(/<%graphql([^%]+)%>/).flatten.first
|
8
|
+
end
|
9
|
+
|
10
|
+
# Ignore static <%graphql sections
|
11
|
+
def convert_input(src, input)
|
12
|
+
input = input.gsub(/<%graphql/, "<%#")
|
13
|
+
super(src, input)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require "active_support/dependencies"
|
2
|
+
require "active_support/inflector"
|
3
|
+
require "graphql/client/erubis"
|
4
|
+
|
5
|
+
module GraphQL
|
6
|
+
class Client
|
7
|
+
module ViewModule
|
8
|
+
attr_accessor :client
|
9
|
+
|
10
|
+
# Public: Eager load module and all subdependencies.
|
11
|
+
#
|
12
|
+
# Use in production when cache_classes is true.
|
13
|
+
#
|
14
|
+
# Traverses all app/views/**/*.erb and loads all static constants defined in
|
15
|
+
# ERB files.
|
16
|
+
#
|
17
|
+
# Examples
|
18
|
+
#
|
19
|
+
# Views.eager_load!
|
20
|
+
#
|
21
|
+
# Returns nothing.
|
22
|
+
def eager_load!
|
23
|
+
return unless File.directory?(self.path)
|
24
|
+
|
25
|
+
Dir.entries(self.path).each do |entry|
|
26
|
+
next if entry == "." || entry == ".."
|
27
|
+
name = entry.sub(/(\.\w+)+$/, "").camelize.to_sym
|
28
|
+
if ViewModule.valid_constant_name?(name) && loadable_const_defined?(name)
|
29
|
+
mod = const_get(name, false)
|
30
|
+
mod.eager_load!
|
31
|
+
else
|
32
|
+
# Ignore template names that don't camelize to safe constants:
|
33
|
+
# 404, foo-dash
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
# Internal: Check if name is a valid Ruby constant identifier.
|
41
|
+
#
|
42
|
+
# name - String or Symbol constant name
|
43
|
+
#
|
44
|
+
# Examples
|
45
|
+
#
|
46
|
+
# valid_constant_name?("Foo") #=> true
|
47
|
+
# valid_constant_name?("404") #=> false
|
48
|
+
#
|
49
|
+
# Returns true if name is a valid constant, otherwise false if name would
|
50
|
+
# result in a "NameError: wrong constant name".
|
51
|
+
def self.valid_constant_name?(name)
|
52
|
+
name.to_s =~ /^[A-Z][a-zA-Z0-9_]*$/
|
53
|
+
end
|
54
|
+
|
55
|
+
# Public: Override constant defined to check if constant name matches a
|
56
|
+
# view directory or template namespace.
|
57
|
+
#
|
58
|
+
# name - String or Symbol constant name
|
59
|
+
# inherit - If the lookup will also search the ancestors (default: true)
|
60
|
+
#
|
61
|
+
# Returns true if definition is found, otherwise false.
|
62
|
+
# def const_defined?(name, inherit = true)
|
63
|
+
# if super(name.to_sym, inherit)
|
64
|
+
# true
|
65
|
+
# elsif const_path(name)
|
66
|
+
# true
|
67
|
+
# else
|
68
|
+
# false
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
|
72
|
+
def loadable_const_defined?(name)
|
73
|
+
if const_defined?(name.to_sym, false)
|
74
|
+
true
|
75
|
+
elsif const_path(name)
|
76
|
+
true
|
77
|
+
else
|
78
|
+
false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Public: Source location that defined the Module.
|
83
|
+
#
|
84
|
+
# Returns absolute String path under app/views.
|
85
|
+
attr_accessor :path
|
86
|
+
|
87
|
+
# Internal: Detect source location for constant name.
|
88
|
+
#
|
89
|
+
# name - String or Symbol constant name
|
90
|
+
#
|
91
|
+
# Examples
|
92
|
+
#
|
93
|
+
# Views.const_path(:Users) #=> "app/views/users"
|
94
|
+
# Views::Users.const_path(:Show) #=> "app/views/users/show.html.erb"
|
95
|
+
# Views::Users.const_path(:Profile) #=> "app/views/users/_profile.html.erb"
|
96
|
+
#
|
97
|
+
# Returns String absolute path to file, otherwise nil.
|
98
|
+
def const_path(name)
|
99
|
+
pathname = ActiveSupport::Inflector.underscore(name.to_s)
|
100
|
+
Dir[File.join(self.path, "{#{pathname},_#{pathname}}{/,.*}")].map { |fn| File.expand_path(fn) }.first
|
101
|
+
end
|
102
|
+
|
103
|
+
# Internal: Initialize new module for constant name and load ERB statics.
|
104
|
+
#
|
105
|
+
# path - String path of directory or erb file.
|
106
|
+
#
|
107
|
+
# Examples
|
108
|
+
#
|
109
|
+
# load_module("app/views/users")
|
110
|
+
# load_module("app/views/users/show.html.erb")
|
111
|
+
#
|
112
|
+
# Returns new Module implementing Loadable concern.
|
113
|
+
def load_module(path)
|
114
|
+
mod = Module.new
|
115
|
+
|
116
|
+
if File.extname(path) == ".erb"
|
117
|
+
contents = File.read(path)
|
118
|
+
query = GraphQL::Client::Erubis.extract_graphql_sections(contents)
|
119
|
+
mod = client.parse(query) if query
|
120
|
+
end
|
121
|
+
|
122
|
+
mod.extend(ViewModule)
|
123
|
+
mod.client = self.client
|
124
|
+
mod.path = path
|
125
|
+
mod
|
126
|
+
end
|
127
|
+
|
128
|
+
# Public: Implement constant missing hook to autoload View ERB statics.
|
129
|
+
#
|
130
|
+
# name - String or Symbol constant name
|
131
|
+
#
|
132
|
+
# Returns module or raises NameError if missing.
|
133
|
+
def const_missing(name)
|
134
|
+
if path = const_path(name)
|
135
|
+
mod = load_module(path)
|
136
|
+
const_set(name, mod)
|
137
|
+
mod.unloadable
|
138
|
+
mod
|
139
|
+
else
|
140
|
+
super
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,35 @@
|
|
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.15
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
13
33
|
- !ruby/object:Gem::Dependency
|
14
34
|
name: activesupport
|
15
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -17,9 +37,9 @@ dependencies:
|
|
17
37
|
- - ">="
|
18
38
|
- !ruby/object:Gem::Version
|
19
39
|
version: '3.0'
|
20
|
-
- - "
|
40
|
+
- - "<"
|
21
41
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
42
|
+
version: '6.0'
|
23
43
|
type: :runtime
|
24
44
|
prerelease: false
|
25
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +47,9 @@ dependencies:
|
|
27
47
|
- - ">="
|
28
48
|
- !ruby/object:Gem::Version
|
29
49
|
version: '3.0'
|
30
|
-
- - "
|
50
|
+
- - "<"
|
31
51
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
52
|
+
version: '6.0'
|
33
53
|
- !ruby/object:Gem::Dependency
|
34
54
|
name: graphql
|
35
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,7 +100,9 @@ extra_rdoc_files: []
|
|
80
100
|
files:
|
81
101
|
- LICENSE
|
82
102
|
- lib/graphql/client.rb
|
103
|
+
- lib/graphql/client/erubis.rb
|
83
104
|
- lib/graphql/client/query_result.rb
|
105
|
+
- lib/graphql/client/view_module.rb
|
84
106
|
- lib/graphql/language/nodes/deep_freeze_ext.rb
|
85
107
|
- lib/graphql/language/operation_slice.rb
|
86
108
|
homepage: https://github.com/github/graphql-client
|