graphql-client 0.2.1 → 0.2.2
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 +10 -2
- data/lib/graphql/client/erubis.rb +2 -3
- data/lib/graphql/client/query_result.rb +3 -5
- data/lib/rubocop/cop/graphql/heredoc.rb +17 -0
- data/lib/rubocop/cop/graphql/overfetch.rb +63 -0
- metadata +4 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 261b7d370edcba549d839ac1faa87f1e33ba4966
|
4
|
+
data.tar.gz: 4c64adc1352929f43f3d2f44f6a698d61c9a0e0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b3c3460544febc35e64b5c4f1da101d8651cee640bdc8ff3a224f4ff6d9edebae2c306d00ad5841990fbed84d92a9a50282aabcf1fce7a532490bc7e9d55e23
|
7
|
+
data.tar.gz: 2a7df513bf269e8cde27157ff3c0a917dd665c73efce31230750d9ef897e36681ada81b7a0d55b1fb47b80e57a4e1e2898b3ed085be89cbc14013ba5d15840e4
|
data/lib/graphql/client.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "active_support/core_ext/hash/keys"
|
1
2
|
require "active_support/inflector"
|
2
3
|
require "active_support/notifications"
|
3
4
|
require "graphql"
|
@@ -193,8 +194,13 @@ module GraphQL
|
|
193
194
|
|
194
195
|
errors = validator.validate(query)
|
195
196
|
errors.fetch(:errors).each do |error|
|
196
|
-
|
197
|
-
|
197
|
+
if error.respond_to?(:message)
|
198
|
+
validation_line = error.line
|
199
|
+
error = ValidationError.new(error.message)
|
200
|
+
else # TODO: Remove when only supporting graphql-ruby 1.x
|
201
|
+
validation_line = error["locations"][0]["line"]
|
202
|
+
error = ValidationError.new(error["message"])
|
203
|
+
end
|
198
204
|
error.set_backtrace(["#{filename}:#{lineno + validation_line}"] + caller) if filename && lineno
|
199
205
|
raise error
|
200
206
|
end
|
@@ -245,6 +251,8 @@ module GraphQL
|
|
245
251
|
raise TypeError, "expected definition to be a #{OperationDefinition.name} but was #{document.class.name}"
|
246
252
|
end
|
247
253
|
|
254
|
+
variables = variables.deep_stringify_keys
|
255
|
+
|
248
256
|
document = definition.document
|
249
257
|
operation = definition.definition_node
|
250
258
|
|
@@ -25,9 +25,8 @@ module GraphQL
|
|
25
25
|
# defined.
|
26
26
|
def self.extract_graphql_section(src)
|
27
27
|
query_string = src.scan(/<%graphql([^%]+)%>/).flatten.first
|
28
|
-
|
29
|
-
|
30
|
-
end
|
28
|
+
return nil unless query_string
|
29
|
+
[query_string, Regexp.last_match.pre_match.count("\n") + 1]
|
31
30
|
end
|
32
31
|
|
33
32
|
# Internal: Extend Rails' Erubis handler to simply ignore <%graphql
|
@@ -22,6 +22,7 @@ module GraphQL
|
|
22
22
|
node.selections.each do |selection|
|
23
23
|
case selection
|
24
24
|
when Language::Nodes::FragmentSpread
|
25
|
+
nil
|
25
26
|
when Language::Nodes::Field
|
26
27
|
field_name = selection.alias || selection.name
|
27
28
|
field_klass = selection.selections.any? ? wrap(selection, name: "#{name}[:#{field_name}]") : nil
|
@@ -115,15 +116,12 @@ module GraphQL
|
|
115
116
|
when Hash
|
116
117
|
new(obj, errors)
|
117
118
|
when self
|
118
|
-
|
119
|
+
obj
|
119
120
|
when QueryResult
|
120
121
|
spreads = Set.new(self.spreads(obj.class.source_node).map(&:name))
|
121
122
|
|
122
123
|
unless spreads.include?(source_node.name)
|
123
|
-
|
124
|
-
suggestion = "\n ...#{name || 'YourFragment'} # SUGGESTION"
|
125
|
-
message << GraphQL::Language::Generation.generate(obj.class.source_node).sub(/\n}$/, "#{suggestion}\n}")
|
126
|
-
raise TypeError, message
|
124
|
+
raise TypeError, "couldn't cast #{obj.inspect} to #{inspect}"
|
127
125
|
end
|
128
126
|
cast(obj.to_h, obj.errors)
|
129
127
|
when Array
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rubocop"
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# Public: Cop for enforcing non-interpolated GRAPHQL heredocs.
|
7
|
+
class Heredoc < Cop
|
8
|
+
def on_str(node)
|
9
|
+
return unless node.location.is_a?(Parser::Source::Map::Heredoc)
|
10
|
+
return unless node.location.expression.source == "<<-GRAPHQL"
|
11
|
+
|
12
|
+
add_offense(node, :expression, "GraphQL heredocs should be quoted. <<-'GRAPHQL'")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "active_support/inflector"
|
2
|
+
require "graphql"
|
3
|
+
require "graphql/client/erubis"
|
4
|
+
require "rubocop"
|
5
|
+
|
6
|
+
module RuboCop
|
7
|
+
module Cop
|
8
|
+
module GraphQL
|
9
|
+
# Public: Rubocop for catching overfetched fields in ERB templates.
|
10
|
+
class Overfetch < Cop
|
11
|
+
def_node_search :send_methods, "(send ...)"
|
12
|
+
|
13
|
+
def investigate(processed_source)
|
14
|
+
erb = File.read(processed_source.buffer.name)
|
15
|
+
query, = ::GraphQL::Client::Erubis.extract_graphql_section(erb)
|
16
|
+
return unless query
|
17
|
+
|
18
|
+
aliases = {}
|
19
|
+
fields = {}
|
20
|
+
ranges = {}
|
21
|
+
|
22
|
+
# TODO: Use GraphQL client parser
|
23
|
+
document = ::GraphQL.parse(query.gsub(/::/, "__"))
|
24
|
+
|
25
|
+
visitor = ::GraphQL::Language::Visitor.new(document)
|
26
|
+
visitor[::GraphQL::Language::Nodes::Field] << ->(node, _parent) do
|
27
|
+
name = node.alias || node.name
|
28
|
+
fields[name] ||= 0
|
29
|
+
field_aliases(name).each { |n| (aliases[n] ||= []) << name }
|
30
|
+
ranges[name] ||= source_range(processed_source.buffer, node.line, 0)
|
31
|
+
end
|
32
|
+
visitor.visit
|
33
|
+
|
34
|
+
send_methods(processed_source.ast).each do |node|
|
35
|
+
_receiver, method_name, *_args = *node
|
36
|
+
aliases.fetch(method_name.to_s, []).each do |field_name|
|
37
|
+
fields[field_name] += 1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
fields.each do |field, count|
|
42
|
+
next if count > 0
|
43
|
+
add_offense(nil, ranges[field], "GraphQL field '#{field}' query but was not used in template.")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def field_aliases(name)
|
48
|
+
names = Set.new
|
49
|
+
|
50
|
+
names << name
|
51
|
+
names << "#{name}?"
|
52
|
+
|
53
|
+
names << underscore_name = ActiveSupport::Inflector.underscore(name)
|
54
|
+
names << "#{underscore_name}?"
|
55
|
+
|
56
|
+
names << "each_node" if name == "edges" || name == "node"
|
57
|
+
|
58
|
+
names
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
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.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -34,9 +34,6 @@ dependencies:
|
|
34
34
|
name: graphql
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "~>"
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: '0.19'
|
40
37
|
- - ">="
|
41
38
|
- !ruby/object:Gem::Version
|
42
39
|
version: 0.19.2
|
@@ -44,9 +41,6 @@ dependencies:
|
|
44
41
|
prerelease: false
|
45
42
|
version_requirements: !ruby/object:Gem::Requirement
|
46
43
|
requirements:
|
47
|
-
- - "~>"
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: '0.19'
|
50
44
|
- - ">="
|
51
45
|
- !ruby/object:Gem::Version
|
52
46
|
version: 0.19.2
|
@@ -133,6 +127,8 @@ files:
|
|
133
127
|
- lib/graphql/client/response.rb
|
134
128
|
- lib/graphql/client/view_module.rb
|
135
129
|
- lib/graphql/language/nodes/deep_freeze_ext.rb
|
130
|
+
- lib/rubocop/cop/graphql/heredoc.rb
|
131
|
+
- lib/rubocop/cop/graphql/overfetch.rb
|
136
132
|
homepage: https://github.com/github/graphql-client
|
137
133
|
licenses:
|
138
134
|
- MIT
|