graphql-result_cache 0.1.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +31 -0
- data/.gitignore +3 -1
- data/Gemfile.lock +20 -20
- data/README.md +19 -24
- data/graphql-result_cache.gemspec +5 -5
- data/lib/graphql/result_cache/callback.rb +9 -3
- data/lib/graphql/result_cache/condition.rb +14 -6
- data/lib/graphql/result_cache/context_config.rb +5 -5
- data/lib/graphql/result_cache/introspection/field_type.rb +2 -1
- data/lib/graphql/result_cache/key.rb +5 -4
- data/lib/graphql/result_cache/result.rb +6 -9
- data/lib/graphql/result_cache/schema/field.rb +19 -0
- data/lib/graphql/result_cache/schema/field_extension.rb +58 -0
- data/lib/graphql/result_cache/schema/query_instrument.rb +15 -0
- data/lib/graphql/result_cache/version.rb +1 -1
- data/lib/graphql/result_cache.rb +14 -7
- metadata +27 -13
- data/lib/graphql/result_cache/field.rb +0 -17
- data/lib/graphql/result_cache/field_instrument.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 535b29453885101a889e6228ded5fd20a83dd6e09f104fc3e9f369e1324b2d28
|
4
|
+
data.tar.gz: e8bad278755e2357bd12aa64ecf3e6078f307ede91625e21ed6794f5e6731144
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 329163b7405a290722f3e594548eaebf459dbfd01d0a5727030b6272043088dc99b2a3acf455b9f40fa4174084b4f37bb17db3da767862ef457ef7a6d8fe2592
|
7
|
+
data.tar.gz: 34dcf33d3e285be6f08c94eba94ea10845f7204eb3fc6247744e16c0377c6ff6f13d75c83f79d17bc247094fc1cf468492a21432f1c72e246e82dc660165d6cd
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on: [push, pull_request]
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
|
15
|
+
runs-on: ubuntu-latest
|
16
|
+
strategy:
|
17
|
+
matrix:
|
18
|
+
ruby-version: ['2.5', '2.6', '2.7']
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v2
|
22
|
+
- name: Set up Ruby
|
23
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
24
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
25
|
+
# uses: ruby/setup-ruby@v1
|
26
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
30
|
+
- name: Run tests
|
31
|
+
run: bundle exec rspec
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,37 +1,37 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
graphql-result_cache (0.
|
5
|
-
graphql (~> 1.
|
4
|
+
graphql-result_cache (1.0.0)
|
5
|
+
graphql (~> 1.10)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
diff-lcs (1.
|
11
|
-
graphql (1.
|
12
|
-
rake (
|
13
|
-
rspec (3.
|
14
|
-
rspec-core (~> 3.
|
15
|
-
rspec-expectations (~> 3.
|
16
|
-
rspec-mocks (~> 3.
|
17
|
-
rspec-core (3.
|
18
|
-
rspec-support (~> 3.
|
19
|
-
rspec-expectations (3.
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
graphql (1.12.20)
|
12
|
+
rake (12.3.3)
|
13
|
+
rspec (3.10.0)
|
14
|
+
rspec-core (~> 3.10.0)
|
15
|
+
rspec-expectations (~> 3.10.0)
|
16
|
+
rspec-mocks (~> 3.10.0)
|
17
|
+
rspec-core (3.10.1)
|
18
|
+
rspec-support (~> 3.10.0)
|
19
|
+
rspec-expectations (3.10.1)
|
20
20
|
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
-
rspec-support (~> 3.
|
22
|
-
rspec-mocks (3.
|
21
|
+
rspec-support (~> 3.10.0)
|
22
|
+
rspec-mocks (3.10.2)
|
23
23
|
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
-
rspec-support (~> 3.
|
25
|
-
rspec-support (3.
|
24
|
+
rspec-support (~> 3.10.0)
|
25
|
+
rspec-support (3.10.3)
|
26
26
|
|
27
27
|
PLATFORMS
|
28
28
|
ruby
|
29
29
|
|
30
30
|
DEPENDENCIES
|
31
|
-
bundler (~>
|
31
|
+
bundler (~> 2.2, >= 2.2.10)
|
32
32
|
graphql-result_cache!
|
33
|
-
rake (~>
|
34
|
-
rspec (~> 3.
|
33
|
+
rake (~> 12.3, >= 12.3.3)
|
34
|
+
rspec (~> 3.10)
|
35
35
|
|
36
36
|
BUNDLED WITH
|
37
|
-
|
37
|
+
2.2.31
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
This gem is to cache the json result, instead of resolved object.
|
4
4
|
|
5
|
+
If you are using `graphql < 1.10.0` or without `GraphQL::Execution::Interpreter`, please [read this](https://github.com/saharaying/graphql-result_cache/wiki/GraphQL::ResultCache-(v0.1.x)).
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
@@ -21,65 +23,57 @@ Or install it yourself as:
|
|
21
23
|
## Usage
|
22
24
|
|
23
25
|
1. Use `GraphQL::ResultCache` as a plugin in your schema.
|
24
|
-
|
26
|
+
|
25
27
|
```ruby
|
26
28
|
class MySchema < GraphQL::Schema
|
29
|
+
# Default value for graphql v1.12.x
|
30
|
+
use GraphQL::Execution::Interpreter
|
31
|
+
use GraphQL::Analysis::AST
|
32
|
+
|
33
|
+
introspection GraphQL::ResultCache::Introspection
|
34
|
+
|
27
35
|
mutation Types::MutationType
|
28
36
|
query Types::QueryType
|
29
|
-
|
37
|
+
|
30
38
|
use GraphQL::ResultCache
|
31
39
|
end
|
32
40
|
```
|
33
41
|
|
34
|
-
2. Add the custom field class to accept `result_cache`
|
42
|
+
2. Add the custom field class to accept `result_cache` definition.
|
35
43
|
|
36
44
|
```ruby
|
37
45
|
module Types
|
38
46
|
class BaseObject < GraphQL::Schema::Object
|
39
|
-
field_class GraphQL::ResultCache::Field
|
47
|
+
field_class GraphQL::ResultCache::Schema::Field
|
40
48
|
end
|
41
49
|
end
|
42
50
|
```
|
43
51
|
|
44
|
-
3. Config the fields which need to be cached
|
52
|
+
3. Config the fields which need to be cached.
|
45
53
|
|
46
54
|
```ruby
|
47
55
|
field :theme, Types::ThemeType, null: false, result_cache: true
|
48
56
|
```
|
49
57
|
|
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
58
|
## Result Cache Customization
|
67
59
|
|
68
60
|
### Cache condition
|
69
61
|
|
62
|
+
The `if` condition can be either a Symbol or a Proc.
|
63
|
+
|
70
64
|
```ruby
|
71
65
|
field :theme, Types::ThemeType, null: false, result_cache: { if: :published? }
|
72
66
|
```
|
73
|
-
The `if` condition can be either a Symbol or a Proc.
|
74
67
|
|
75
68
|
### Customized cache key
|
76
69
|
|
77
|
-
By default, `GraphQL::ResultCache` will generate a cache key combining the field path, arguments and object.
|
70
|
+
By default, `GraphQL::ResultCache` will generate a cache key combining the field path, arguments and object.
|
78
71
|
But you can customize the object clause by specify the `key` option.
|
79
72
|
|
80
73
|
```ruby
|
81
74
|
field :theme, Types::ThemeType, null: false, result_cache: { key: :theme_cache_key }
|
82
75
|
```
|
76
|
+
|
83
77
|
The `key` can be either a Symbol or a Proc.
|
84
78
|
|
85
79
|
### Callback after cached result applied
|
@@ -105,6 +99,7 @@ GraphQL::ResultCache.configure do |config|
|
|
105
99
|
config.namespace = "GraphQL:Result" # Cache key namespace
|
106
100
|
config.expires_in = 1.hour # Expire time for the cache, default to 1.hour
|
107
101
|
config.client_hash = -> { Rails.cache.read(:deploy_version) } # GraphQL client package hash key, used in cache key generation, default to nil
|
102
|
+
config.except = ->(ctx) { !ctx[:result_cacheable] } # Exception rule, skip the cache while evaluated as true, default to nil
|
108
103
|
config.cache = Rails.cache # The cache object, default to Rails.cache in Rails
|
109
104
|
config.logger = Rails.logger # The Logger, default to Rails.logger in Rails
|
110
105
|
end
|
@@ -113,7 +108,7 @@ end
|
|
113
108
|
When using with introspection, you need to assign custom introspection to avoid getting the nullable type for a non-null type.
|
114
109
|
```ruby
|
115
110
|
class MySchema < GraphQL::Schema
|
116
|
-
introspection
|
111
|
+
introspection GraphQL::ResultCache::Introspection
|
117
112
|
end
|
118
113
|
```
|
119
114
|
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ['saharaying@gmail.com']
|
11
11
|
|
12
12
|
spec.summary = 'A result caching plugin for graphql-ruby'
|
13
|
-
spec.description = '
|
13
|
+
spec.description = 'This gem is to cache the json result, instead of resolved object.'
|
14
14
|
spec.homepage = 'https://github.com/saharaying/graphql-result_cache'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
@@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
|
|
19
19
|
end
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.add_development_dependency 'bundler', '~>
|
23
|
-
spec.add_development_dependency 'rake', '~>
|
24
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
22
|
+
spec.add_development_dependency 'bundler', '~> 2.2', '>= 2.2.10'
|
23
|
+
spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
25
25
|
|
26
|
-
spec.add_dependency 'graphql', '~> 1.
|
26
|
+
spec.add_dependency 'graphql', '~> 1.10'
|
27
27
|
|
28
28
|
# graphql v1.9 requires ruby >= 2.2.0
|
29
29
|
spec.required_ruby_version = '>= 2.2.0'
|
@@ -1,21 +1,27 @@
|
|
1
1
|
module GraphQL
|
2
2
|
module ResultCache
|
3
3
|
class Callback
|
4
|
-
def initialize
|
4
|
+
def initialize(obj:, args:, ctx:, value:)
|
5
5
|
@obj = obj
|
6
6
|
@args = args
|
7
7
|
@ctx = ctx
|
8
8
|
@value = value
|
9
9
|
end
|
10
10
|
|
11
|
-
def call
|
11
|
+
def call(result_hash)
|
12
12
|
case @value
|
13
13
|
when Symbol
|
14
14
|
@obj.public_send(@value, result_hash)
|
15
15
|
when Proc
|
16
16
|
@value.call(result_hash, @obj, @args, @ctx)
|
17
17
|
end
|
18
|
-
::GraphQL::ResultCache.logger && ::GraphQL::ResultCache.logger.debug("GraphQL result cache callback called for #{
|
18
|
+
::GraphQL::ResultCache.logger && ::GraphQL::ResultCache.logger.debug("GraphQL result cache callback called for #{ctx_path}")
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def ctx_path
|
24
|
+
@ctx.namespace(:interpreter)[:current_path].join('.')
|
19
25
|
end
|
20
26
|
end
|
21
27
|
end
|
@@ -9,15 +9,23 @@ module GraphQL
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def true?
|
12
|
+
return false if except?
|
12
13
|
case @if
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
when Symbol
|
15
|
+
@obj.send(@if)
|
16
|
+
when Proc
|
17
|
+
@if.call(@obj, @args, @ctx)
|
18
|
+
else
|
19
|
+
true
|
19
20
|
end
|
20
21
|
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def except?
|
26
|
+
except = ::GraphQL::ResultCache.except
|
27
|
+
except.is_a?(Proc) ? except.call(@ctx) : except
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
23
31
|
end
|
@@ -8,16 +8,16 @@ module GraphQL
|
|
8
8
|
@value = {}
|
9
9
|
end
|
10
10
|
|
11
|
-
def add
|
12
|
-
@value[
|
11
|
+
def add query:, path:, key:, after_process: nil
|
12
|
+
@value[query] ||= []
|
13
13
|
cached = cache.exist? key
|
14
14
|
logger && logger.info("GraphQL result cache key #{cached ? 'hit' : 'miss'}: #{key}")
|
15
|
-
config_value = { path:
|
15
|
+
config_value = { path: path, key: key }
|
16
16
|
if cached
|
17
17
|
config_value[:result] = cache.read(key)
|
18
18
|
config_value[:after_process] = after_process if after_process
|
19
19
|
end
|
20
|
-
@value[
|
20
|
+
@value[query] << config_value
|
21
21
|
cached
|
22
22
|
end
|
23
23
|
|
@@ -39,7 +39,7 @@ module GraphQL
|
|
39
39
|
def cache_or_amend_result result, config_of_query
|
40
40
|
config_of_query.each do |config|
|
41
41
|
if config[:result].nil?
|
42
|
-
cache.write config[:key],
|
42
|
+
cache.write config[:key], dig(result, 'data', *config[:path]), cache_write_options.merge(expires_in: expires_in)
|
43
43
|
else
|
44
44
|
# result already got from cache, need to amend to response
|
45
45
|
result_hash = result.to_h
|
@@ -4,7 +4,8 @@ module GraphQL
|
|
4
4
|
class FieldType < ::GraphQL::Introspection::FieldType
|
5
5
|
|
6
6
|
def type
|
7
|
-
@object.
|
7
|
+
return @object.type unless @object.respond_to?(:original_return_type_not_null)
|
8
|
+
@object.original_return_type_not_null && !@object.type.non_null? ? @object.type.to_non_null_type : @object.type
|
8
9
|
end
|
9
10
|
|
10
11
|
end
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module GraphQL
|
2
4
|
module ResultCache
|
3
5
|
class Key
|
4
|
-
def initialize
|
6
|
+
def initialize(obj:, args:, ctx: nil, key: nil)
|
5
7
|
@obj = obj
|
6
8
|
@args = args
|
7
9
|
@ctx = ctx
|
@@ -21,7 +23,7 @@ module GraphQL
|
|
21
23
|
private
|
22
24
|
|
23
25
|
def path_clause
|
24
|
-
@ctx.
|
26
|
+
@ctx.namespace(:interpreter)[:current_path].join('.') unless @ctx.nil?
|
25
27
|
end
|
26
28
|
|
27
29
|
def args_clause
|
@@ -53,7 +55,6 @@ module GraphQL
|
|
53
55
|
return object.id if object.respond_to?(:id)
|
54
56
|
object.object_id
|
55
57
|
end
|
56
|
-
|
57
58
|
end
|
58
59
|
end
|
59
|
-
end
|
60
|
+
end
|
@@ -1,27 +1,24 @@
|
|
1
1
|
module GraphQL
|
2
2
|
module ResultCache
|
3
3
|
class Result
|
4
|
-
extend Forwardable
|
5
|
-
|
6
|
-
attr_reader :value
|
7
|
-
|
8
|
-
def_delegators :@value, :to_json, :as_json
|
9
|
-
|
10
4
|
def initialize query_result
|
11
5
|
@_result = query_result
|
12
|
-
|
6
|
+
end
|
7
|
+
|
8
|
+
def process!
|
9
|
+
process_with_result_cache
|
13
10
|
end
|
14
11
|
|
15
12
|
private
|
16
13
|
|
17
14
|
def process_with_result_cache
|
18
15
|
return process_each(@_result) unless @_result.is_a?(Array)
|
19
|
-
@_result.
|
16
|
+
@_result.each { |result| process_each(result) }
|
20
17
|
end
|
21
18
|
|
22
19
|
def process_each result
|
23
20
|
result_cache_config = result.query.context[:result_cache]
|
24
|
-
|
21
|
+
result_cache_config.process(result) unless blank?(result_cache_config)
|
25
22
|
end
|
26
23
|
|
27
24
|
def blank? obj
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module ResultCache
|
3
|
+
module Schema
|
4
|
+
class Field < ::GraphQL::Schema::Field
|
5
|
+
attr_reader :original_return_type_not_null
|
6
|
+
|
7
|
+
def initialize(*args, result_cache: nil, **kwargs, &block)
|
8
|
+
super(*args, **kwargs, &block)
|
9
|
+
return unless result_cache
|
10
|
+
unless @return_type_null
|
11
|
+
@original_return_type_not_null = true
|
12
|
+
@return_type_null = true
|
13
|
+
end
|
14
|
+
extension FieldExtension, result_cache
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'graphql/result_cache/condition'
|
4
|
+
require 'graphql/result_cache/context_config'
|
5
|
+
require 'graphql/result_cache/key'
|
6
|
+
require 'graphql/result_cache/callback'
|
7
|
+
|
8
|
+
module GraphQL
|
9
|
+
module ResultCache
|
10
|
+
module Schema
|
11
|
+
class FieldExtension < GraphQL::Schema::FieldExtension
|
12
|
+
def resolve(object:, arguments:, context:)
|
13
|
+
cache_config = options.respond_to?(:to_h) ? options.to_h : {}
|
14
|
+
|
15
|
+
if Condition.new(cache_config, obj: object, args: arguments, ctx: context).true?
|
16
|
+
context[:result_cache] ||= ContextConfig.new
|
17
|
+
cached = add_to_context_result_cache(context, object, arguments, cache_config)
|
18
|
+
end
|
19
|
+
|
20
|
+
yield(object, arguments) unless cached
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def add_to_context_result_cache(context, object, arguments, cache_config)
|
26
|
+
cache_key = key(object, arguments, context, cache_config)
|
27
|
+
after_process = after_process(object, arguments, context, cache_config)
|
28
|
+
context[:result_cache].add(
|
29
|
+
query: context.query,
|
30
|
+
path: context.namespace(:interpreter)[:current_path],
|
31
|
+
key: cache_key.to_s,
|
32
|
+
after_process: after_process
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def key(object, arguments, context, config)
|
37
|
+
Key.new(
|
38
|
+
obj: object,
|
39
|
+
args: arguments,
|
40
|
+
ctx: context,
|
41
|
+
key: config[:key]
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
def after_process(object, arguments, context, config)
|
46
|
+
return unless config[:after_process]
|
47
|
+
|
48
|
+
Callback.new(
|
49
|
+
obj: object,
|
50
|
+
args: arguments,
|
51
|
+
ctx: context,
|
52
|
+
value: config[:after_process]
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/graphql/result_cache.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'graphql'
|
2
2
|
require 'graphql/result_cache/version'
|
3
|
-
require 'graphql/result_cache/field'
|
4
3
|
require 'graphql/result_cache/result'
|
5
|
-
require 'graphql/result_cache/
|
4
|
+
require 'graphql/result_cache/schema/field'
|
5
|
+
require 'graphql/result_cache/schema/field_extension'
|
6
|
+
require 'graphql/result_cache/schema/query_instrument'
|
6
7
|
require 'graphql/result_cache/introspection'
|
7
8
|
|
8
9
|
module GraphQL
|
@@ -18,6 +19,9 @@ module GraphQL
|
|
18
19
|
# c.client_hash = -> { Rails.cache.read(:deploy_version) }
|
19
20
|
attr_accessor :client_hash
|
20
21
|
|
22
|
+
# global condition, skip the cache when the value is true, should be a proc.
|
23
|
+
attr_accessor :except
|
24
|
+
|
21
25
|
# ```
|
22
26
|
# GraphQL::ResultCache.configure do |c|
|
23
27
|
# c.namespace = "GraphQL:Result"
|
@@ -28,16 +32,19 @@ module GraphQL
|
|
28
32
|
def configure
|
29
33
|
yield self
|
30
34
|
end
|
35
|
+
|
36
|
+
def use(schema_def)
|
37
|
+
return schema_def.instrument(:query, Schema::QueryInstrument) if schema_def.interpreter?
|
38
|
+
raise 'Please use the graphql gem version >= 1.10 with GraphQL::Execution::Interpreter'
|
39
|
+
end
|
31
40
|
end
|
32
41
|
|
33
42
|
# Default configuration
|
34
|
-
@expires_in = 3600
|
43
|
+
@expires_in = 3600 # 1.hour
|
35
44
|
@namespace = 'GraphQL:Result'
|
36
|
-
|
37
|
-
def self.use(schema_def, options: {})
|
38
|
-
schema_def.instrument(:field, ::GraphQL::ResultCache::FieldInstrument.new)
|
39
|
-
end
|
40
45
|
end
|
41
46
|
end
|
42
47
|
|
48
|
+
GraphQL::Schema::Field.accepts_definition(:result_cache)
|
49
|
+
|
43
50
|
require 'graphql/result_cache/rails' if defined?(::Rails::Engine)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-result_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ying Fu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,63 +16,76 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.2'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.2.10
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
29
|
+
version: '2.2'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.2.10
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
37
|
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
39
|
+
version: '12.3'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 12.3.3
|
34
43
|
type: :development
|
35
44
|
prerelease: false
|
36
45
|
version_requirements: !ruby/object:Gem::Requirement
|
37
46
|
requirements:
|
38
47
|
- - "~>"
|
39
48
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
49
|
+
version: '12.3'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 12.3.3
|
41
53
|
- !ruby/object:Gem::Dependency
|
42
54
|
name: rspec
|
43
55
|
requirement: !ruby/object:Gem::Requirement
|
44
56
|
requirements:
|
45
57
|
- - "~>"
|
46
58
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
59
|
+
version: '3.10'
|
48
60
|
type: :development
|
49
61
|
prerelease: false
|
50
62
|
version_requirements: !ruby/object:Gem::Requirement
|
51
63
|
requirements:
|
52
64
|
- - "~>"
|
53
65
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
66
|
+
version: '3.10'
|
55
67
|
- !ruby/object:Gem::Dependency
|
56
68
|
name: graphql
|
57
69
|
requirement: !ruby/object:Gem::Requirement
|
58
70
|
requirements:
|
59
71
|
- - "~>"
|
60
72
|
- !ruby/object:Gem::Version
|
61
|
-
version: '1.
|
73
|
+
version: '1.10'
|
62
74
|
type: :runtime
|
63
75
|
prerelease: false
|
64
76
|
version_requirements: !ruby/object:Gem::Requirement
|
65
77
|
requirements:
|
66
78
|
- - "~>"
|
67
79
|
- !ruby/object:Gem::Version
|
68
|
-
version: '1.
|
69
|
-
description:
|
80
|
+
version: '1.10'
|
81
|
+
description: This gem is to cache the json result, instead of resolved object.
|
70
82
|
email:
|
71
83
|
- saharaying@gmail.com
|
72
84
|
executables: []
|
73
85
|
extensions: []
|
74
86
|
extra_rdoc_files: []
|
75
87
|
files:
|
88
|
+
- ".github/workflows/ruby.yml"
|
76
89
|
- ".gitignore"
|
77
90
|
- ".rspec"
|
78
91
|
- CODE_OF_CONDUCT.md
|
@@ -88,13 +101,14 @@ files:
|
|
88
101
|
- lib/graphql/result_cache/callback.rb
|
89
102
|
- lib/graphql/result_cache/condition.rb
|
90
103
|
- lib/graphql/result_cache/context_config.rb
|
91
|
-
- lib/graphql/result_cache/field.rb
|
92
|
-
- lib/graphql/result_cache/field_instrument.rb
|
93
104
|
- lib/graphql/result_cache/introspection.rb
|
94
105
|
- lib/graphql/result_cache/introspection/field_type.rb
|
95
106
|
- lib/graphql/result_cache/key.rb
|
96
107
|
- lib/graphql/result_cache/rails.rb
|
97
108
|
- lib/graphql/result_cache/result.rb
|
109
|
+
- lib/graphql/result_cache/schema/field.rb
|
110
|
+
- lib/graphql/result_cache/schema/field_extension.rb
|
111
|
+
- lib/graphql/result_cache/schema/query_instrument.rb
|
98
112
|
- lib/graphql/result_cache/version.rb
|
99
113
|
homepage: https://github.com/saharaying/graphql-result_cache
|
100
114
|
licenses:
|
@@ -1,17 +0,0 @@
|
|
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.metadata[:original_non_null] = true if @result_cache_config && field_defn.type.non_null?
|
13
|
-
field_defn
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'graphql/result_cache/condition'
|
2
|
-
require 'graphql/result_cache/context_config'
|
3
|
-
require 'graphql/result_cache/key'
|
4
|
-
require 'graphql/result_cache/callback'
|
5
|
-
|
6
|
-
module GraphQL
|
7
|
-
module ResultCache
|
8
|
-
class FieldInstrument
|
9
|
-
def instrument _type, field
|
10
|
-
return field unless field.metadata[:result_cache]
|
11
|
-
|
12
|
-
cached_resolve_proc = cached_resolve(field)
|
13
|
-
field.redefine do
|
14
|
-
resolve(cached_resolve_proc)
|
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
|
-
after_process = Callback.new(obj: obj, args: args, ctx: ctx, value: cache_config[:after_process]) if cache_config[:after_process]
|
30
|
-
cached = ctx[:result_cache].add context: ctx, key: cache_key.to_s, after_process: after_process
|
31
|
-
end
|
32
|
-
old_resolve_proc.call(obj, args, ctx) unless cached
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|