graphql-cache 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +49 -19
- data/lib/graphql/cache.rb +1 -1
- data/lib/graphql/cache/field.rb +6 -6
- data/lib/graphql/cache/marshal.rb +4 -4
- data/lib/graphql/cache/middleware.rb +25 -8
- data/lib/graphql/cache/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d20a6abf5bff1dee85af2a7fe01ca15513e4553f87f255f4c4cea9f1166bfeaa
|
4
|
+
data.tar.gz: 55c93f0e7fcf6f072dbd8728bddffe082fb006863786cba6d1c8cdb47a0abd75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f586d9f2b11cf724fbf4df6f5af9a2b839a4a527029a3c67b4786da7fe30acc45d0f0889657e4df7b38e5c21b792459c56a82fcf09dc6e2d39972ea1a117a65
|
7
|
+
data.tar.gz: '0669f17df94d9ecef5d14db5821c927f4747c826e87b737730e83653b2925df2834a3075d7cc635420179fe2daa493af38772744c05bbb06b7ac206132f58788'
|
data/.rubocop.yml
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,50 +1,80 @@
|
|
1
|
-
#
|
1
|
+
# GraphQL Cache [![Build Status](https://travis-ci.org/Leanstack/graphql-cache.svg?branch=master)](https://travis-ci.org/Leanstack/graphql-cache) [![Test Coverage](https://api.codeclimate.com/v1/badges/c8560834b10db0618175/test_coverage)](https://codeclimate.com/github/Leanstack/graphql-cache/test_coverage) [![Maintainability](https://api.codeclimate.com/v1/badges/c8560834b10db0618175/maintainability)](https://codeclimate.com/github/Leanstack/graphql-cache/maintainability)
|
2
2
|
|
3
|
-
GraphQL Cache is a custom middleware for graphql-ruby providing field-level caching
|
3
|
+
GraphQL Cache is a custom middleware for graphql-ruby providing field-level caching. It is currently a work in progress and the API is subject to change prior to the release v1.0.0.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
Add this line to your application's Gemfile:
|
7
|
+
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
```ruby
|
10
|
-
gem 'graphql-cache'
|
11
|
-
```
|
9
|
+
```ruby
|
10
|
+
gem 'graphql-cache'
|
11
|
+
```
|
12
12
|
|
13
|
-
And then execute:
|
13
|
+
And then execute:
|
14
14
|
|
15
|
-
|
15
|
+
$ bundle
|
16
16
|
|
17
|
-
Or install it yourself as:
|
17
|
+
Or install it yourself as:
|
18
18
|
|
19
|
-
|
19
|
+
$ gem install graphql-cache
|
20
20
|
|
21
21
|
## Setup
|
22
22
|
|
23
23
|
1. Add `middleware GraphQL::Cache::Middleware` to your schema
|
24
24
|
2. Add `field_class GraphQL::Cache::Field` to your base object type
|
25
25
|
|
26
|
+
## Configuration
|
27
|
+
|
28
|
+
GraphQL Cache can be configured in an initializer:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# config/initializers/graphql_cache.rb
|
32
|
+
|
33
|
+
GraphQL::Cache.configure do |config|
|
34
|
+
config.namespace = 'GraphQL::Cache' # Cache key prefix for keys generated by graphql-cache
|
35
|
+
config.cache = Rails.cache # The cache object to use for caching
|
36
|
+
config.logger = Rails.logger # Logger to receive cache-related log messages
|
37
|
+
config.expiry = 5400 # 90 minutes (in seconds)
|
38
|
+
config.force = false # Cache override, when true no caching takes place
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
26
42
|
## Usage
|
27
43
|
|
28
|
-
Any object, list, or connection field can be cached by simply adding `cache: true` to the field definition:
|
44
|
+
Any object, list, or connection field can be cached by simply adding `cache: true` to the field definition:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
field :calculated_field, Int, cache: true
|
48
|
+
```
|
49
|
+
|
50
|
+
By default all keys will have an expiration of `GraphQL::Cache.expiry` which defaults to 90 minutes. If you want to set a field-specific expiration time pass a hash to the `cache` parameter like this:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
field :calculated_field, Int, cache: { expiry: 10800 } # expires key after 180 minutes
|
54
|
+
```
|
55
|
+
|
56
|
+
When passing a hash in the `cache` parameter the possible options are:
|
29
57
|
|
30
|
-
|
31
|
-
|
32
|
-
|
58
|
+
| Key | Default | Description |
|
59
|
+
|----------|---------|------------------------------------------------------------|
|
60
|
+
| `expiry` | 5400 | expiration time for this field's key in seconds |
|
61
|
+
| `force` | false | force cache misses on this field |
|
62
|
+
| `prefix` | | cache key prefix (appended after GraphQL::Cache.namespace) |
|
33
63
|
|
34
64
|
## Development
|
35
65
|
|
36
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
37
67
|
|
38
|
-
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).
|
68
|
+
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).
|
39
69
|
|
40
70
|
## Contributing
|
41
71
|
|
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.
|
72
|
+
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
73
|
|
44
74
|
## License
|
45
75
|
|
46
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
76
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
47
77
|
|
48
78
|
## Code of Conduct
|
49
79
|
|
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).
|
80
|
+
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/lib/graphql/cache.rb
CHANGED
data/lib/graphql/cache/field.rb
CHANGED
@@ -7,20 +7,20 @@ module GraphQL
|
|
7
7
|
def initialize(
|
8
8
|
*args,
|
9
9
|
cache: false,
|
10
|
-
expiry: nil,
|
11
10
|
**kwargs,
|
12
11
|
&block
|
13
12
|
)
|
14
|
-
@cache_config =
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
@cache_config = if cache.is_a? Hash
|
14
|
+
cache
|
15
|
+
else
|
16
|
+
{ cache: cache }
|
17
|
+
end
|
18
18
|
super(*args, **kwargs, &block)
|
19
19
|
end
|
20
20
|
|
21
21
|
def to_graphql
|
22
22
|
field_defn = super # Returns a GraphQL::Field
|
23
|
-
field_defn.metadata[:
|
23
|
+
field_defn.metadata[:cache] = @cache_config
|
24
24
|
field_defn
|
25
25
|
end
|
26
26
|
end
|
@@ -4,6 +4,7 @@ require 'graphql/cache/builder'
|
|
4
4
|
|
5
5
|
module GraphQL
|
6
6
|
module Cache
|
7
|
+
# Used to marshal data to/from cache on cache hits/misses
|
7
8
|
class Marshal
|
8
9
|
attr_accessor :key
|
9
10
|
|
@@ -27,9 +28,9 @@ module GraphQL
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
def write(config
|
31
|
-
resolved =
|
32
|
-
expiry = config[:expiry] || GraphQL::Cache.expiry
|
31
|
+
def write(config)
|
32
|
+
resolved = yield
|
33
|
+
expiry = config[:metadata][:expiry] || GraphQL::Cache.expiry
|
33
34
|
|
34
35
|
document = Builder[resolved].deconstruct
|
35
36
|
|
@@ -37,7 +38,6 @@ module GraphQL
|
|
37
38
|
resolved
|
38
39
|
end
|
39
40
|
|
40
|
-
|
41
41
|
def build(cached, config)
|
42
42
|
Builder[cached].build(config)
|
43
43
|
end
|
@@ -2,21 +2,26 @@
|
|
2
2
|
|
3
3
|
module GraphQL
|
4
4
|
module Cache
|
5
|
+
# graphql-ruby middleware to wrap resolvers for caching
|
5
6
|
class Middleware
|
6
7
|
attr_accessor :parent_type, :parent_object, :object, :cache,
|
7
|
-
|
8
|
+
:field_definition, :field_args, :query_context
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def self.call(*args, &block)
|
11
|
+
new(*args).call(&block)
|
11
12
|
end
|
12
13
|
|
13
|
-
def initialize(parent_type,
|
14
|
+
def initialize(parent_type,
|
15
|
+
parent_object,
|
16
|
+
field_definition,
|
17
|
+
field_args,
|
18
|
+
query_context)
|
14
19
|
self.parent_type = parent_type
|
15
20
|
self.parent_object = parent_object
|
16
21
|
self.field_definition = field_definition
|
17
22
|
self.field_args = field_args
|
18
23
|
self.query_context = query_context
|
19
|
-
self.object = parent_object.
|
24
|
+
self.object = parent_object.object if parent_object
|
20
25
|
self.cache = ::GraphQL::Cache.cache
|
21
26
|
end
|
22
27
|
|
@@ -28,7 +33,15 @@ module GraphQL
|
|
28
33
|
field_args: field_args,
|
29
34
|
query_context: query_context,
|
30
35
|
object: object
|
31
|
-
}.merge
|
36
|
+
}.merge metadata_hash
|
37
|
+
end
|
38
|
+
|
39
|
+
def metadata_hash
|
40
|
+
{
|
41
|
+
metadata: {
|
42
|
+
cache: field_definition.metadata[:cache] || {}
|
43
|
+
}
|
44
|
+
}
|
32
45
|
end
|
33
46
|
|
34
47
|
def call(&block)
|
@@ -51,10 +64,14 @@ module GraphQL
|
|
51
64
|
def object_key
|
52
65
|
return nil unless object
|
53
66
|
|
54
|
-
id_from_object = object.try(:id) || object.try(:fetch, :id, nil) || object.try(:fetch, 'id', nil)
|
55
|
-
|
56
67
|
"#{object.class.name}:#{id_from_object}"
|
57
68
|
end
|
69
|
+
|
70
|
+
def id_from_object
|
71
|
+
return object.id if object.respond_to? :id
|
72
|
+
return object.fetch(:id, nil) if object.respond_to? :fetch
|
73
|
+
return object.fetch('id', nil) if object.respond_to? :fetch
|
74
|
+
end
|
58
75
|
end
|
59
76
|
end
|
60
77
|
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.
|
4
|
+
version: 0.2.0
|
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-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -118,6 +118,7 @@ files:
|
|
118
118
|
- ".github/PULL_REQUEST_TEMPLATE.md"
|
119
119
|
- ".gitignore"
|
120
120
|
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
121
122
|
- ".travis.yml"
|
122
123
|
- CODE_OF_CONDUCT.md
|
123
124
|
- CONTRIBUTING.md
|