graphql-cache 0.1.1 → 0.1.2
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/.github/PULL_REQUEST_TEMPLATE.md +11 -0
- data/.travis.yml +8 -1
- data/CONTRIBUTING.md +23 -0
- data/Gemfile.lock +19 -2
- data/README.md +3 -3
- data/graphql-cache.gemspec +19 -15
- data/lib/graphql/cache.rb +2 -59
- data/lib/graphql/cache/builder.rb +95 -0
- data/lib/graphql/cache/field.rb +3 -1
- data/lib/graphql/cache/marshal.rb +54 -0
- data/lib/graphql/cache/version.rb +1 -1
- metadata +49 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d858418f7a97b6b74af740be3e490d56da27e8a0aec2aea48b69b1cf1abc728e
|
4
|
+
data.tar.gz: '0281225dfc87aee0914b580b0fed83c57f3b42838ffe01c19778f91784f2b49d'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0942c0baac595bdfbb336d253653e4343d7f0c6d14b6ffe87a06e9632d475fa3b67d7e613dafe8aac8ef5e14cb81cf952dccdd322a3383f60eac5ee81a5b39e5'
|
7
|
+
data.tar.gz: 9fc08f962f99cef760c95fd8bb1e8389cc1b20222f4c6a7bf53afddefd455597504fcb839f40b5b282edc5c873b0334af329b44c389c10f122cc1782fa28a98e
|
data/.travis.yml
CHANGED
@@ -2,4 +2,11 @@ sudo: false
|
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
4
|
- 2.5.0
|
5
|
-
|
5
|
+
before_script:
|
6
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
7
|
+
- chmod +x ./cc-test-reporter
|
8
|
+
- ./cc-test-reporter before-build
|
9
|
+
- gem install bundler -v 1.16.1
|
10
|
+
script: bundle exec rspec
|
11
|
+
after_script:
|
12
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
## How to contribute to GraphQL Cache
|
2
|
+
|
3
|
+
#### **Did you find a bug?**
|
4
|
+
|
5
|
+
* **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/Leanstack/graphql-cache/issues).
|
6
|
+
|
7
|
+
* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/Leanstack/graphql-cache/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible, and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring.
|
8
|
+
|
9
|
+
#### **Did you write a patch that fixes a bug?**
|
10
|
+
|
11
|
+
* Open a new GitHub pull request with the patch.
|
12
|
+
|
13
|
+
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
|
14
|
+
|
15
|
+
#### **Do you intend to add a new feature or change an existing one?**
|
16
|
+
|
17
|
+
* Open an issue on GitHub and label it "Enhancement" so that the community can comment on the feature
|
18
|
+
|
19
|
+
* If you have code, by all means, open a pull request!
|
20
|
+
|
21
|
+
Thanks again for contirbuting to GraphQL Cache! :tada:
|
22
|
+
|
23
|
+
The StackShare Team
|
data/Gemfile.lock
CHANGED
@@ -1,14 +1,23 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
graphql-cache (0.1.
|
5
|
-
graphql (
|
4
|
+
graphql-cache (0.1.1)
|
5
|
+
graphql (~> 1.8.0.pre10)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
+
codeclimate-test-reporter (0.6.0)
|
11
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
12
|
+
coderay (1.1.2)
|
10
13
|
diff-lcs (1.3)
|
14
|
+
docile (1.3.0)
|
11
15
|
graphql (1.8.0.pre11)
|
16
|
+
json (2.1.0)
|
17
|
+
method_source (0.9.0)
|
18
|
+
pry (0.11.3)
|
19
|
+
coderay (~> 1.1.0)
|
20
|
+
method_source (~> 0.9.0)
|
12
21
|
rake (10.5.0)
|
13
22
|
rspec (3.7.0)
|
14
23
|
rspec-core (~> 3.7.0)
|
@@ -23,15 +32,23 @@ GEM
|
|
23
32
|
diff-lcs (>= 1.2.0, < 2.0)
|
24
33
|
rspec-support (~> 3.7.0)
|
25
34
|
rspec-support (3.7.1)
|
35
|
+
simplecov (0.16.1)
|
36
|
+
docile (~> 1.1)
|
37
|
+
json (>= 1.8, < 3)
|
38
|
+
simplecov-html (~> 0.10.0)
|
39
|
+
simplecov-html (0.10.2)
|
26
40
|
|
27
41
|
PLATFORMS
|
28
42
|
ruby
|
29
43
|
|
30
44
|
DEPENDENCIES
|
31
45
|
bundler (~> 1.16)
|
46
|
+
codeclimate-test-reporter
|
32
47
|
graphql-cache!
|
48
|
+
pry
|
33
49
|
rake (~> 10.0)
|
34
50
|
rspec (~> 3.0)
|
51
|
+
simplecov
|
35
52
|
|
36
53
|
BUNDLED WITH
|
37
54
|
1.16.1
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Graphql Cache
|
1
|
+
# Graphql Cache [](https://travis-ci.org/Leanstack/graphql-cache) [](https://codeclimate.com/github/Leanstack/graphql-cache/test_coverage) [](https://codeclimate.com/github/Leanstack/graphql-cache/maintainability)
|
2
2
|
|
3
3
|
GraphQL Cache is a custom middleware for graphql-ruby providing field-level caching
|
4
4
|
|
@@ -39,7 +39,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
39
39
|
|
40
40
|
## Contributing
|
41
41
|
|
42
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Leanstack/graphql-cache. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
43
43
|
|
44
44
|
## License
|
45
45
|
|
@@ -47,4 +47,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
47
47
|
|
48
48
|
## Code of Conduct
|
49
49
|
|
50
|
-
Everyone interacting in the Graphql::Cache project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
50
|
+
Everyone interacting in the Graphql::Cache project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Leanstack/graphql-cache/blob/master/CODE_OF_CONDUCT.md).
|
data/graphql-cache.gemspec
CHANGED
@@ -3,25 +3,29 @@ lib = File.expand_path("../lib", __FILE__)
|
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require "graphql/cache/version"
|
5
5
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "graphql-cache"
|
8
|
+
s.version = GraphQL::Cache::VERSION
|
9
|
+
s.authors = ["Michael Kelly"]
|
10
|
+
s.email = ["michaelkelly322@gmail.com"]
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
s.summary = %q{Caching middleware for graphql-ruby}
|
13
|
+
s.description = %q{Provides middleware field-level caching for graphql-ruby}
|
14
|
+
s.homepage = "https://github.com/Leanstack/graphql-cache"
|
15
|
+
s.license = "MIT"
|
16
16
|
|
17
|
-
|
17
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
18
|
f.match(%r{^(test|spec|features)/})
|
19
19
|
end
|
20
|
-
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.required_ruby_version = ">= 2.2.0" # bc graphql-ruby requires >= 2.2.0
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
s.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
s.add_development_dependency "rake", "~> 10.0"
|
25
|
+
s.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
s.add_development_dependency "simplecov"
|
27
|
+
s.add_development_dependency "codeclimate-test-reporter"
|
28
|
+
s.add_development_dependency "pry"
|
25
29
|
|
26
|
-
|
30
|
+
s.add_dependency 'graphql', '~> 1.8.0.pre10'
|
27
31
|
end
|
data/lib/graphql/cache.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'graphql/cache/version'
|
2
2
|
require 'graphql/cache/middleware'
|
3
3
|
require 'graphql/cache/field'
|
4
|
+
require 'graphql/cache/marshal'
|
4
5
|
|
5
6
|
module GraphQL
|
6
7
|
module Cache
|
@@ -26,10 +27,6 @@ module GraphQL
|
|
26
27
|
def self.logger; @@logger; end
|
27
28
|
def self.logger=(obj); @@logger=obj; end
|
28
29
|
|
29
|
-
@@log_level = nil
|
30
|
-
def self.log_level; @@log_level; end
|
31
|
-
def self.log_level=(obj); @@log_level=obj; @@logger.try(:level=, obj); end
|
32
|
-
|
33
30
|
def self.configure
|
34
31
|
yield self
|
35
32
|
end
|
@@ -37,61 +34,7 @@ module GraphQL
|
|
37
34
|
def self.fetch(key, config: {}, &block)
|
38
35
|
return block.call unless config[:cache]
|
39
36
|
|
40
|
-
|
41
|
-
|
42
|
-
if cached.nil?
|
43
|
-
logger.debug "Cache miss: (#{key})"
|
44
|
-
marshal_to_cache(key, config, &block)
|
45
|
-
else
|
46
|
-
logger.debug "Cache hit: (#{key})"
|
47
|
-
marshal_from_cache(cached, config)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.marshal_to_cache(key, config, &block)
|
52
|
-
raw = block.call
|
53
|
-
|
54
|
-
marshaled = case raw.class.name
|
55
|
-
when 'Array'
|
56
|
-
raw.map(&:object)
|
57
|
-
when 'GraphQL::Relay::RelationConnection'
|
58
|
-
raw.nodes
|
59
|
-
else
|
60
|
-
raw.try(:object) || raw
|
61
|
-
end
|
62
|
-
|
63
|
-
cache.write(key, marshaled, expires_in: config[:expiry] || GraphQL::Cache.expiry)
|
64
|
-
raw
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.marshal_from_cache(cached, config = {})
|
68
|
-
case cached.class.name
|
69
|
-
when 'Array'
|
70
|
-
cached.map do |raw|
|
71
|
-
config[:field_definition].type.unwrap.graphql_definition.metadata[:type_class].new(
|
72
|
-
raw,
|
73
|
-
config[:query_context]
|
74
|
-
)
|
75
|
-
end
|
76
|
-
when 'ActiveRecord::Associations::CollectionProxy'
|
77
|
-
GraphQL::Relay::RelationConnection.new(
|
78
|
-
cached,
|
79
|
-
config[:field_args],
|
80
|
-
field: config[:query_context].field,
|
81
|
-
parent: config[:parent_object].object,
|
82
|
-
context: config[:query_context]
|
83
|
-
)
|
84
|
-
else
|
85
|
-
klass = config[:field_definition].type.unwrap.graphql_definition.metadata[:type_class]
|
86
|
-
if klass
|
87
|
-
new(
|
88
|
-
cached,
|
89
|
-
config[:query_context]
|
90
|
-
)
|
91
|
-
else
|
92
|
-
cached
|
93
|
-
end
|
94
|
-
end
|
37
|
+
Marshal[key].read(config, &block)
|
95
38
|
end
|
96
39
|
end
|
97
40
|
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module Cache
|
3
|
+
class Builder
|
4
|
+
attr_accessor :raw, :method, :config
|
5
|
+
|
6
|
+
def self.[](raw)
|
7
|
+
build_method = namify(raw.class.name)
|
8
|
+
new(raw, build_method)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.namify(str)
|
12
|
+
str.split('::').last.downcase
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(raw, method)
|
16
|
+
self.raw = raw
|
17
|
+
self.method = method
|
18
|
+
end
|
19
|
+
|
20
|
+
def build(config)
|
21
|
+
self.config = config
|
22
|
+
|
23
|
+
return build_array if method == 'array'
|
24
|
+
return build_relation if method == 'collectionproxy' || method == 'relation'
|
25
|
+
build_object
|
26
|
+
end
|
27
|
+
|
28
|
+
def deconstruct
|
29
|
+
case self.class.namify(raw.class.name)
|
30
|
+
when 'array'
|
31
|
+
deconstruct_array(raw)
|
32
|
+
when 'relationconnection'
|
33
|
+
raw.nodes
|
34
|
+
else
|
35
|
+
deconstruct_object(raw)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def deconstruct_object(raw)
|
40
|
+
if raw.respond_to?(:object)
|
41
|
+
raw.object
|
42
|
+
else
|
43
|
+
raw
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def deconstruct_array(raw)
|
48
|
+
return [] if raw.empty?
|
49
|
+
|
50
|
+
if raw.first.class.ancestors.include? GraphQL::Schema::Object
|
51
|
+
raw.map(&:object)
|
52
|
+
else
|
53
|
+
raw
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def build_relation
|
58
|
+
GraphQL::Relay::RelationConnection.new(
|
59
|
+
raw,
|
60
|
+
config[:field_args],
|
61
|
+
field: config[:query_context].field,
|
62
|
+
parent: config[:parent_object].object,
|
63
|
+
context: config[:query_context]
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
def build_array
|
68
|
+
gql_def = config[:field_definition].type.unwrap.graphql_definition
|
69
|
+
|
70
|
+
raw.map do |item|
|
71
|
+
if gql_def.kind.name == 'OBJECT'
|
72
|
+
config[:field_definition].type.unwrap.graphql_definition.metadata[:type_class].new(
|
73
|
+
item,
|
74
|
+
config[:query_context]
|
75
|
+
)
|
76
|
+
else
|
77
|
+
item
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def build_object
|
83
|
+
klass = config[:field_definition].type.unwrap.graphql_definition.metadata[:type_class]
|
84
|
+
if klass
|
85
|
+
klass.new(
|
86
|
+
raw,
|
87
|
+
config[:query_context]
|
88
|
+
)
|
89
|
+
else
|
90
|
+
raw
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/graphql/cache/field.rb
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'graphql/cache/builder'
|
4
|
+
|
5
|
+
module GraphQL
|
6
|
+
module Cache
|
7
|
+
class Marshal
|
8
|
+
attr_accessor :key
|
9
|
+
|
10
|
+
def self.[](key)
|
11
|
+
new(key)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(key)
|
15
|
+
self.key = key.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
def read(config, &block)
|
19
|
+
cached = cache.read(key)
|
20
|
+
|
21
|
+
if cached.nil?
|
22
|
+
logger.debug "Cache miss: (#{key})"
|
23
|
+
write config, &block
|
24
|
+
else
|
25
|
+
logger.debug "Cache hit: (#{key})"
|
26
|
+
build cached, config
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def write(config, &block)
|
31
|
+
resolved = block.call
|
32
|
+
expiry = config[:expiry] || GraphQL::Cache.expiry
|
33
|
+
|
34
|
+
document = Builder[resolved].deconstruct
|
35
|
+
|
36
|
+
cache.write(key, document, expires_in: expiry)
|
37
|
+
resolved
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def build(cached, config)
|
42
|
+
Builder[cached].build(config)
|
43
|
+
end
|
44
|
+
|
45
|
+
def cache
|
46
|
+
GraphQL::Cache.cache
|
47
|
+
end
|
48
|
+
|
49
|
+
def logger
|
50
|
+
GraphQL::Cache.logger
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Kelly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,48 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codeclimate-test-reporter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
55
97
|
- !ruby/object:Gem::Dependency
|
56
98
|
name: graphql
|
57
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,10 +115,12 @@ executables: []
|
|
73
115
|
extensions: []
|
74
116
|
extra_rdoc_files: []
|
75
117
|
files:
|
118
|
+
- ".github/PULL_REQUEST_TEMPLATE.md"
|
76
119
|
- ".gitignore"
|
77
120
|
- ".rspec"
|
78
121
|
- ".travis.yml"
|
79
122
|
- CODE_OF_CONDUCT.md
|
123
|
+
- CONTRIBUTING.md
|
80
124
|
- Gemfile
|
81
125
|
- Gemfile.lock
|
82
126
|
- LICENSE.txt
|
@@ -86,7 +130,9 @@ files:
|
|
86
130
|
- bin/setup
|
87
131
|
- graphql-cache.gemspec
|
88
132
|
- lib/graphql/cache.rb
|
133
|
+
- lib/graphql/cache/builder.rb
|
89
134
|
- lib/graphql/cache/field.rb
|
135
|
+
- lib/graphql/cache/marshal.rb
|
90
136
|
- lib/graphql/cache/middleware.rb
|
91
137
|
- lib/graphql/cache/rails.rb
|
92
138
|
- lib/graphql/cache/version.rb
|
@@ -102,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
148
|
requirements:
|
103
149
|
- - ">="
|
104
150
|
- !ruby/object:Gem::Version
|
105
|
-
version:
|
151
|
+
version: 2.2.0
|
106
152
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
153
|
requirements:
|
108
154
|
- - ">="
|