graphql-result_cache 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/graphql-result_cache.gemspec +29 -0
- data/lib/graphql/result_cache.rb +41 -0
- data/lib/graphql/result_cache/condition.rb +23 -0
- data/lib/graphql/result_cache/context_config.rb +71 -0
- data/lib/graphql/result_cache/field.rb +16 -0
- data/lib/graphql/result_cache/field_instrument.rb +36 -0
- data/lib/graphql/result_cache/key.rb +59 -0
- data/lib/graphql/result_cache/rails.rb +15 -0
- data/lib/graphql/result_cache/result.rb +32 -0
- data/lib/graphql/result_cache/version.rb +5 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ba79fe918d359fba4d366fd3becfd4779860b3af1c4083c634578bba7258fbf9
|
4
|
+
data.tar.gz: 814addd76ffa4ccb92a4641d27c58d71d0359005eff4e1bc9145cd6d47cf2b0b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a8cb8d375f740c870acd94feaafded3d38e911acafa73573748ff63c49c9134e844488308a054ab601daafe56fa6af23a71bb9b7e4a68c7b3e27975d9a1a5f0
|
7
|
+
data.tar.gz: 59ca4da7a641b8c66f8de5452c647cf56301a37ce5a62e0b0de4a02165d90749f594c5ad6b139fef361f18e7a2dd63f0bb2b2e1eea13590d5a77a99bcdd0e3e5
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at saharaying@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
graphql-result_cache (0.1.0)
|
5
|
+
graphql (~> 1.9)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
graphql (1.9.7)
|
12
|
+
rake (10.5.0)
|
13
|
+
rspec (3.8.0)
|
14
|
+
rspec-core (~> 3.8.0)
|
15
|
+
rspec-expectations (~> 3.8.0)
|
16
|
+
rspec-mocks (~> 3.8.0)
|
17
|
+
rspec-core (3.8.2)
|
18
|
+
rspec-support (~> 3.8.0)
|
19
|
+
rspec-expectations (3.8.4)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.8.0)
|
22
|
+
rspec-mocks (3.8.1)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.8.0)
|
25
|
+
rspec-support (3.8.2)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.16)
|
32
|
+
graphql-result_cache!
|
33
|
+
rake (~> 10.0)
|
34
|
+
rspec (~> 3.0)
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Ying Fu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# GraphQL::ResultCache
|
2
|
+
|
3
|
+
This gem is to cache the json result, instead of resolved object.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'graphql-result_cache'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install graphql-result_cache
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
1. Use `GraphQL::ResultCache` as a plugin in your schema.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class MySchema < GraphQL::Schema
|
27
|
+
mutation Types::MutationType
|
28
|
+
query Types::QueryType
|
29
|
+
|
30
|
+
use GraphQL::ResultCache
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
2. Add the custom field class to accept `result_cache` metadata.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
module Types
|
38
|
+
class BaseObject < GraphQL::Schema::Object
|
39
|
+
field_class GraphQL::ResultCache::Field
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
3. Config the fields which need to be cached with `result_cache` definition.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
field :theme, Types::ThemeType, null: false, result_cache: true
|
48
|
+
```
|
49
|
+
|
50
|
+
4. Wrap query result with `GraphQL::ResultCache::Result`.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class GraphqlController < ApplicationController
|
54
|
+
def execute
|
55
|
+
# ...
|
56
|
+
result = if params[:_json]
|
57
|
+
multiple_execute(params[:_json], context: context)
|
58
|
+
else
|
59
|
+
execute_query(context: context)
|
60
|
+
end
|
61
|
+
render json: GraphQL::ResultCache::Result.new(result)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
## Result Cache Customization
|
67
|
+
|
68
|
+
### Cache condition
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
field :theme, Types::ThemeType, null: false, result_cache: { if: :published? }
|
72
|
+
```
|
73
|
+
The `if` condition can be either a Symbol or a Proc.
|
74
|
+
|
75
|
+
### Customized cache key
|
76
|
+
|
77
|
+
By default, `GraphQL::ResultCache` will generate a cache key combining the field path, arguments and object.
|
78
|
+
But you can customize the object clause by specify the `key` option.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
field :theme, Types::ThemeType, null: false, result_cache: { key: :theme_cache_key }
|
82
|
+
```
|
83
|
+
The `key` can be either a Symbol or a Proc.
|
84
|
+
|
85
|
+
## Global Configuration
|
86
|
+
|
87
|
+
`GraphQL::ResultCache` can be configured in initializer.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# config/initializers/graphql/result_cache.rb
|
91
|
+
|
92
|
+
GraphQL::ResultCache.configure do |config|
|
93
|
+
config.namespace = "GraphQL:Result" # Cache key namespace
|
94
|
+
config.expires_in = 1.hour # Expire time for the cache, default to 1.hour
|
95
|
+
config.client_hash = -> { Rails.cache.read(:deploy_version) } # GraphQL client package hash key, used in cache key generation, default to nil
|
96
|
+
config.cache = Rails.cache # The cache object, default to Rails.cache in Rails
|
97
|
+
config.logger = Rails.logger # The Logger, default to Rails.logger in Rails
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
## Development
|
102
|
+
|
103
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
104
|
+
|
105
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/saharaying/graphql-result_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.
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
114
|
+
|
115
|
+
## Code of Conduct
|
116
|
+
|
117
|
+
Everyone interacting in the GraphQL::ResultCache project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/saharaying/graphql-result_cache/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "graphql/result_cache"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'graphql/result_cache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'graphql-result_cache'
|
8
|
+
spec.version = GraphQL::ResultCache::VERSION
|
9
|
+
spec.authors = ['Ying Fu']
|
10
|
+
spec.email = ['saharaying@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'A result caching plugin for graphql-ruby'
|
13
|
+
spec.description = 'A result caching plugin for graphql-ruby'
|
14
|
+
spec.homepage = 'https://github.com/saharaying/graphql-result_cache'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
|
26
|
+
spec.add_dependency 'graphql', '~> 1.9'
|
27
|
+
|
28
|
+
spec.required_ruby_version = '>= 2.1.0'
|
29
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'graphql'
|
2
|
+
require 'graphql/result_cache/version'
|
3
|
+
require 'graphql/result_cache/field'
|
4
|
+
require 'graphql/result_cache/result'
|
5
|
+
require 'graphql/result_cache/field_instrument'
|
6
|
+
|
7
|
+
module GraphQL
|
8
|
+
module ResultCache
|
9
|
+
class << self
|
10
|
+
attr_accessor :expires_in
|
11
|
+
attr_accessor :namespace
|
12
|
+
attr_accessor :cache
|
13
|
+
attr_accessor :logger
|
14
|
+
|
15
|
+
# to expire the cache when client hash changes, should be a proc. eg:
|
16
|
+
# c.client_hash = -> { Rails.cache.read(:deploy_version) }
|
17
|
+
attr_accessor :client_hash
|
18
|
+
|
19
|
+
# ```
|
20
|
+
# GraphQL::ResultCache.configure do |c|
|
21
|
+
# c.namespace = "GraphQL:Result"
|
22
|
+
# c.expires_in = 1.hour
|
23
|
+
# c.client_hash = -> { Rails.cache.read(:deploy_version) }
|
24
|
+
# end
|
25
|
+
# ```
|
26
|
+
def configure
|
27
|
+
yield self
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Default configuration
|
32
|
+
@expires_in = 3600 # 1.hour
|
33
|
+
@namespace = 'GraphQL:Result'
|
34
|
+
|
35
|
+
def self.use(schema_def, options: {})
|
36
|
+
schema_def.instrument(:field, ::GraphQL::ResultCache::FieldInstrument.new)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
require 'graphql/result_cache/rails' if defined?(::Rails::Engine)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
class Condition
|
4
|
+
def initialize config, obj:, args:, ctx:
|
5
|
+
@if = config[:if]
|
6
|
+
@obj = obj
|
7
|
+
@args = args
|
8
|
+
@ctx = ctx
|
9
|
+
end
|
10
|
+
|
11
|
+
def true?
|
12
|
+
case @if
|
13
|
+
when Symbol
|
14
|
+
@obj.send(@if)
|
15
|
+
when Proc
|
16
|
+
@if.call(@obj, @args, @ctx)
|
17
|
+
else
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
class ContextConfig
|
4
|
+
|
5
|
+
attr_accessor :value
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@value = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def add context:, key:
|
12
|
+
@value[context.query] ||= []
|
13
|
+
cached = cache.exist? key
|
14
|
+
logger&.info "GraphQL result cache key #{cached ? 'hit' : 'miss'}: #{key}"
|
15
|
+
@value[context.query] << { path: context.path, key: key, result: cached ? cache.read(key) : nil }
|
16
|
+
cached
|
17
|
+
end
|
18
|
+
|
19
|
+
def process result
|
20
|
+
config_of_query = of_query(result.query)
|
21
|
+
blank?(config_of_query) ? result : cache_or_amend_result(result, config_of_query)
|
22
|
+
end
|
23
|
+
|
24
|
+
def of_query query
|
25
|
+
@value[query]
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def blank? obj
|
31
|
+
obj.respond_to?(:empty?) ? !!obj.empty? : !obj
|
32
|
+
end
|
33
|
+
|
34
|
+
def cache_or_amend_result result, config_of_query
|
35
|
+
config_of_query.each do |config|
|
36
|
+
if config[:result].nil?
|
37
|
+
cache.write config[:key], result.dig('data', *config[:path]), expires_in: expires_in
|
38
|
+
else
|
39
|
+
# result already got from cache, need to amend to response
|
40
|
+
deep_merge! result.to_h, 'data' => config[:path].reverse.inject(config[:result]) { |a, n| {n => a} }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
result
|
44
|
+
end
|
45
|
+
|
46
|
+
def deep_merge! hash, other_hash, &block
|
47
|
+
hash.merge!(other_hash) do |key, this_val, other_val|
|
48
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
49
|
+
deep_merge!(this_val.dup, other_val, &block)
|
50
|
+
elsif block_given?
|
51
|
+
block.call(key, this_val, other_val)
|
52
|
+
else
|
53
|
+
other_val
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def cache
|
59
|
+
::GraphQL::ResultCache.cache
|
60
|
+
end
|
61
|
+
|
62
|
+
def logger
|
63
|
+
::GraphQL::ResultCache.logger
|
64
|
+
end
|
65
|
+
|
66
|
+
def expires_in
|
67
|
+
::GraphQL::ResultCache.expires_in
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
class Field < ::GraphQL::Schema::Field
|
4
|
+
def initialize(*args, result_cache: nil, **kwargs, &block)
|
5
|
+
@result_cache_config = result_cache
|
6
|
+
super(*args, **kwargs, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_graphql
|
10
|
+
field_defn = super # Returns a GraphQL::Field
|
11
|
+
field_defn.metadata[:result_cache] = @result_cache_config
|
12
|
+
field_defn
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'graphql/result_cache/condition'
|
2
|
+
require 'graphql/result_cache/context_config'
|
3
|
+
require 'graphql/result_cache/key'
|
4
|
+
|
5
|
+
module GraphQL
|
6
|
+
module ResultCache
|
7
|
+
class FieldInstrument
|
8
|
+
def instrument _type, field
|
9
|
+
return field unless field.metadata[:result_cache]
|
10
|
+
|
11
|
+
cached_resolve_proc = cached_resolve(field)
|
12
|
+
field.redefine do
|
13
|
+
resolve(cached_resolve_proc)
|
14
|
+
# for cacheable field, change type to be nullable
|
15
|
+
type(field.type.of_type) if field.type.non_null?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def cached_resolve field
|
22
|
+
old_resolve_proc = field.resolve_proc
|
23
|
+
cache_config = field.metadata[:result_cache]
|
24
|
+
cache_config = {} unless cache_config.is_a?(Hash)
|
25
|
+
lambda do |obj, args, ctx|
|
26
|
+
if Condition.new(cache_config, obj: obj, args: args, ctx: ctx).true?
|
27
|
+
ctx[:result_cache] ||= ContextConfig.new
|
28
|
+
cache_key = Key.new(obj: obj, args: args, ctx: ctx, key: cache_config[:key])
|
29
|
+
cached = ctx[:result_cache].add context: ctx, key: cache_key.to_s
|
30
|
+
end
|
31
|
+
old_resolve_proc.call(obj, args, ctx) unless cached
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
class Key
|
4
|
+
def initialize obj:, args:, ctx:, key: nil
|
5
|
+
@obj = obj
|
6
|
+
@args = args
|
7
|
+
@ctx = ctx
|
8
|
+
@key = key
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
@to_s ||= [
|
13
|
+
::GraphQL::ResultCache.namespace,
|
14
|
+
path_clause,
|
15
|
+
args_clause,
|
16
|
+
object_clause,
|
17
|
+
client_hash_clause
|
18
|
+
].flatten.compact.join(':')
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def path_clause
|
24
|
+
@ctx.path.join('/')
|
25
|
+
end
|
26
|
+
|
27
|
+
def args_clause
|
28
|
+
@args.to_h.to_a.flatten
|
29
|
+
end
|
30
|
+
|
31
|
+
def object_clause
|
32
|
+
case @key
|
33
|
+
when Symbol
|
34
|
+
@obj.public_send(@key)
|
35
|
+
when Proc
|
36
|
+
@key.call(@obj, @args, @ctx)
|
37
|
+
when NilClass
|
38
|
+
guess_id
|
39
|
+
else
|
40
|
+
@key
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def client_hash_clause
|
45
|
+
clause = ::GraphQL::ResultCache.client_hash
|
46
|
+
clause.is_a?(Proc) ? clause.call : clause
|
47
|
+
end
|
48
|
+
|
49
|
+
def guess_id
|
50
|
+
object = @obj.object
|
51
|
+
return unless object
|
52
|
+
return object.cache_key if object.respond_to?(:cache_key)
|
53
|
+
return object.id if object.respond_to?(:id)
|
54
|
+
object.object_id
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
if defined?(::Rails)
|
4
|
+
# Railtie integration used to default {GraphQL::ResultCache.cache}
|
5
|
+
# and {GraphQL::ResultCache.logger} when in a Rails environment.
|
6
|
+
class Rails < ::Rails::Engine
|
7
|
+
config.after_initialize do
|
8
|
+
# default values for cache and logger in Rails if not set already
|
9
|
+
GraphQL::ResultCache.cache = ::Rails.cache unless GraphQL::ResultCache.cache
|
10
|
+
GraphQL::ResultCache.logger = ::Rails.logger unless GraphQL::ResultCache.logger
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
class Result
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
attr_reader :value
|
7
|
+
|
8
|
+
def_delegators :@value, :to_json, :as_json
|
9
|
+
|
10
|
+
def initialize query_result
|
11
|
+
@_result = query_result
|
12
|
+
@value = process_with_result_cache
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def process_with_result_cache
|
18
|
+
return process_each(@_result) unless @_result.is_a?(Array)
|
19
|
+
@_result.map { |result| process_each(result) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def process_each result
|
23
|
+
result_cache_config = result.query.context[:result_cache]
|
24
|
+
blank?(result_cache_config) ? result : result_cache_config.process(result)
|
25
|
+
end
|
26
|
+
|
27
|
+
def blank? obj
|
28
|
+
obj.respond_to?(:empty?) ? !!obj.empty? : !obj
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graphql-result_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ying Fu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: graphql
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.9'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.9'
|
69
|
+
description: A result caching plugin for graphql-ruby
|
70
|
+
email:
|
71
|
+
- saharaying@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- graphql-result_cache.gemspec
|
87
|
+
- lib/graphql/result_cache.rb
|
88
|
+
- lib/graphql/result_cache/condition.rb
|
89
|
+
- lib/graphql/result_cache/context_config.rb
|
90
|
+
- lib/graphql/result_cache/field.rb
|
91
|
+
- lib/graphql/result_cache/field_instrument.rb
|
92
|
+
- lib/graphql/result_cache/key.rb
|
93
|
+
- lib/graphql/result_cache/rails.rb
|
94
|
+
- lib/graphql/result_cache/result.rb
|
95
|
+
- lib/graphql/result_cache/version.rb
|
96
|
+
homepage: https://github.com/saharaying/graphql-result_cache
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 2.1.0
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.7.8
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: A result caching plugin for graphql-ruby
|
120
|
+
test_files: []
|