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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7dbe70f22e89be24185d79bed0ee9e60c7bad3b5e50ea69be106ab582a3308a
4
- data.tar.gz: 52da222074c2f41ea81430b228352abb66c7c7419b102ad62ae7e0b65a7cac2a
3
+ metadata.gz: d20a6abf5bff1dee85af2a7fe01ca15513e4553f87f255f4c4cea9f1166bfeaa
4
+ data.tar.gz: 55c93f0e7fcf6f072dbd8728bddffe082fb006863786cba6d1c8cdb47a0abd75
5
5
  SHA512:
6
- metadata.gz: da22a68c8a8c621dfe65bd9019e22c94fbcd2285794eae28f5ef896663c362630158208f43b81411dac933bae200e8db3fb0a14071b74724d1da3a7678e8354e
7
- data.tar.gz: 40199213530274dd3d8860ef317516ba3908a965e700d50e85be8e1364f817b155410ce34c590ece2ee359ad9970d5b02b63c4e594d0fe6d3d6321d8e99ade4c
6
+ metadata.gz: 8f586d9f2b11cf724fbf4df6f5af9a2b839a4a527029a3c67b4786da7fe30acc45d0f0889657e4df7b38e5c21b792459c56a82fcf09dc6e2d39972ea1a117a65
7
+ data.tar.gz: '0669f17df94d9ecef5d14db5821c927f4747c826e87b737730e83653b2925df2834a3075d7cc635420179fe2daa493af38772744c05bbb06b7ac206132f58788'
@@ -0,0 +1,3 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'spec/**/*'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql-cache (0.1.3)
4
+ graphql-cache (0.2.0)
5
5
  graphql (~> 1.8.0.pre10)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,50 +1,80 @@
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)
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
- $ bundle
15
+ $ bundle
16
16
 
17
- Or install it yourself as:
17
+ Or install it yourself as:
18
18
 
19
- $ gem install graphql-cache
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
- ```ruby
31
- field :calculated_field, Int, cache: true
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).
@@ -32,7 +32,7 @@ module GraphQL
32
32
  end
33
33
 
34
34
  def self.fetch(key, config: {}, &block)
35
- return block.call unless config[:cache]
35
+ return block.call unless config[:metadata][:cache]
36
36
 
37
37
  Marshal[key].read(config, &block)
38
38
  end
@@ -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
- cache: cache,
16
- expiry: expiry
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[:cache_config] = @cache_config
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, &block)
31
- resolved = block.call
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
- :field_definition, :field_args, :query_context,
8
+ :field_definition, :field_args, :query_context
8
9
 
9
- def self.call(*args, &block)
10
- new(*args).call(&block)
10
+ def self.call(*args, &block)
11
+ new(*args).call(&block)
11
12
  end
12
13
 
13
- def initialize(parent_type, parent_object, field_definition, field_args, query_context)
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.try(: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(field_definition.metadata[:cache_config] || {})
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
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Cache
3
- VERSION = '0.1.3'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  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.3
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-15 00:00:00.000000000 Z
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