graphql-analyzer 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql/analyzer.rb +5 -1
- data/lib/graphql/analyzer/instrumentation/active_record.rb +43 -0
- data/lib/graphql/analyzer/instrumentation/base.rb +11 -29
- data/lib/graphql/analyzer/instrumentation/elastic_search.rb +10 -4
- data/lib/graphql/analyzer/instrumentation/mysql.rb +1 -1
- data/lib/graphql/analyzer/instrumentation/postgresql.rb +1 -1
- data/lib/graphql/analyzer/instrumentation/sqlite3.rb +1 -1
- data/lib/graphql/analyzer/version.rb +1 -1
- 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: b0214e0468ba2d0b41740b1ba6eb83e545c9778c
|
4
|
+
data.tar.gz: 637e201e8b6c9461dcdf07df0c278dcb0ea117ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 776bfaa067aa81dcb4ce8eb2ffe627d20c54cae1fa1932244290d9235ff7613654f1aa72c26b0436f759da8c44d713a2a7b6abdaef0c4f2eee3fbe47613f13d7
|
7
|
+
data.tar.gz: 144c77105b00b0e0ebfda11964a4cc92432950e92a603736079a57cf5a8b3a85c28c2f65ce9081d327c5cc2ccc24659fb03e35984e8a3406cce57e4a1e5e1097
|
data/lib/graphql/analyzer.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "graphql"
|
2
2
|
require "graphql/analyzer/version"
|
3
3
|
require "graphql/analyzer/instrumentation/base"
|
4
|
+
require "graphql/analyzer/instrumentation/active_record"
|
4
5
|
require "graphql/analyzer/instrumentation/elastic_search"
|
5
6
|
require "graphql/analyzer/instrumentation/mysql"
|
6
7
|
require "graphql/analyzer/instrumentation/postgresql"
|
@@ -43,7 +44,10 @@ module GraphQL
|
|
43
44
|
|
44
45
|
def instrument(type, field)
|
45
46
|
instruments.reduce(field) do |field, instrumentation|
|
46
|
-
field.redefine
|
47
|
+
field.redefine do
|
48
|
+
resolve(instrumentation.instrument(type, field))
|
49
|
+
lazy_resolve(instrumentation.instrument_lazy(type, field))
|
50
|
+
end
|
47
51
|
end
|
48
52
|
end
|
49
53
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module GraphQL
|
2
|
+
class Analyzer
|
3
|
+
module Instrumentation
|
4
|
+
class ActiveRecord < Base
|
5
|
+
private
|
6
|
+
|
7
|
+
def resolve_proc(type, field, method)
|
8
|
+
->(obj, args, ctx) do
|
9
|
+
result = nil
|
10
|
+
queries = ::ActiveRecord::Base.collecting_queries_for_explain do
|
11
|
+
result = field.public_send(method).call(obj, args, ctx)
|
12
|
+
if result.respond_to?(:to_a)
|
13
|
+
result.to_a
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
if queries.any?
|
18
|
+
explain_output = ::ActiveRecord::Base.exec_explain(queries)
|
19
|
+
parsed_output = parser.parse(explain_output)
|
20
|
+
|
21
|
+
# TODO: Merge results when a field makes two types of queries
|
22
|
+
# e.g. path: ['user', 'name'] makes a SQL and ES Query.
|
23
|
+
ctx['graphql-analyzer']['resolvers'] << {
|
24
|
+
'path' => ctx.path,
|
25
|
+
'adapter' => adapter,
|
26
|
+
'parentType' => type.name,
|
27
|
+
'fieldName' => field.name,
|
28
|
+
'returnType' => field.type.to_s,
|
29
|
+
'details' => parsed_output
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def parser
|
38
|
+
raise NotImplementedError, "Please override in #{self.class.name}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -3,40 +3,22 @@ module GraphQL
|
|
3
3
|
module Instrumentation
|
4
4
|
class Base
|
5
5
|
def instrument(type, field)
|
6
|
-
|
7
|
-
|
8
|
-
queries = ::ActiveRecord::Base.collecting_queries_for_explain do
|
9
|
-
result = field.resolve_proc.call(obj, args, ctx)
|
10
|
-
result.to_a if result.respond_to?(:to_a)
|
11
|
-
end
|
6
|
+
resolve_proc(type, field, :resolve_proc)
|
7
|
+
end
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
def instrument_lazy(type, field)
|
10
|
+
resolve_proc(type, field, :lazy_resolve_proc)
|
11
|
+
end
|
16
12
|
|
17
|
-
|
18
|
-
# e.g. path: ['user', 'name'] makes a SQL and ES Query.
|
19
|
-
ctx['graphql-analyzer']['resolvers'] << {
|
20
|
-
'path' => ctx.path,
|
21
|
-
'adapter' => adapter,
|
22
|
-
'parentType' => type.name,
|
23
|
-
'fieldName' => field.name,
|
24
|
-
'returnType' => field.type.to_s,
|
25
|
-
'details' => parsed_output
|
26
|
-
}
|
27
|
-
end
|
13
|
+
private
|
28
14
|
|
29
|
-
|
30
|
-
|
15
|
+
def resolve_proc(type, field, method)
|
16
|
+
raise NotImplementedError, "Please override in #{self.class.name}"
|
31
17
|
end
|
32
|
-
end
|
33
18
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
def adapter
|
39
|
-
raise NotImplementedError, "Please override in #{self.class.name}"
|
19
|
+
def adapter
|
20
|
+
raise NotImplementedError, "Please override in #{self.class.name}"
|
21
|
+
end
|
40
22
|
end
|
41
23
|
end
|
42
24
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module GraphQL
|
2
2
|
class Analyzer
|
3
3
|
module Instrumentation
|
4
|
-
class ElasticSearch
|
4
|
+
class ElasticSearch < Base
|
5
5
|
def initialize
|
6
6
|
@notifications = []
|
7
7
|
ActiveSupport::Notifications.subscribe('search.elasticsearch') do |name, start, finish, id, payload|
|
@@ -15,16 +15,18 @@ module GraphQL
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
|
18
|
+
private
|
19
|
+
|
20
|
+
def resolve_proc(type, field, method)
|
19
21
|
->(obj, args, ctx) do
|
20
|
-
result = field.
|
22
|
+
result = field.public_send(method).call(obj, args, ctx)
|
21
23
|
|
22
24
|
if @notifications.any?
|
23
25
|
# TODO: Merge results when a field makes two types of queries
|
24
26
|
# e.g. path: ['user', 'name'] makes a SQL and ES Query.
|
25
27
|
ctx['graphql-analyzer']['resolvers'] << {
|
26
28
|
'path' => ctx.path,
|
27
|
-
'adapter' =>
|
29
|
+
'adapter' => adapter,
|
28
30
|
'parentType' => type.name,
|
29
31
|
'fieldName' => field.name,
|
30
32
|
'returnType' => field.type.to_s,
|
@@ -37,6 +39,10 @@ module GraphQL
|
|
37
39
|
result
|
38
40
|
end
|
39
41
|
end
|
42
|
+
|
43
|
+
def adapter
|
44
|
+
@adapter ||= 'elasticsearch'
|
45
|
+
end
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-analyzer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DerekStride
|
@@ -199,6 +199,7 @@ files:
|
|
199
199
|
- Rakefile
|
200
200
|
- graphql-analyzer.gemspec
|
201
201
|
- lib/graphql/analyzer.rb
|
202
|
+
- lib/graphql/analyzer/instrumentation/active_record.rb
|
202
203
|
- lib/graphql/analyzer/instrumentation/base.rb
|
203
204
|
- lib/graphql/analyzer/instrumentation/elastic_search.rb
|
204
205
|
- lib/graphql/analyzer/instrumentation/mysql.rb
|