graphql-persisted_queries 0.1.0 → 0.1.1

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: c766b357d35d1bda4dfba34a5a633260a0dfe3593189f1d60b35d5973b5c18a4
4
- data.tar.gz: 6182e3cdb5ce2ed6194b7ee4eef87ad2c3194299904bd1ecaf29fc5179c1358e
3
+ metadata.gz: d1315da48bdcf5bca972f884cc38d12aae1016355198d31dc8fdadf445b6662a
4
+ data.tar.gz: eb8daf60333fe6683bc68e6eb63e0ef5528dba116b33ab084b2585b6465e3394
5
5
  SHA512:
6
- metadata.gz: 6ee805aae51f2ecf5c055eb5c3d02b20873369a990498f0b2e75348ab62cf8192945b9716d87c616772e2c07b000fd8b73a238e45124cd7e849e3d3041e41cf6
7
- data.tar.gz: 49b7cbe475f09c7f4ec6e4b11a1dbcb3d86a03fcd6d63ef4acf977e59aff6c6e223e858d24f96f6895d8ccee9495f0ffbd623a28e48eec89ccd895fa284cd4d0
6
+ metadata.gz: b80a7cbfe97b3e6c9912732867576b2be8bf682c1a91728462a18aa844df4db12610cb58da16e62d45f75f7256a73370b72d04abb4691503b64eb59909f32b7b
7
+ data.tar.gz: 0dd02d28cc56c2ceab91f3299dcf5252a2528eaf796011dd36a7213fbf77c6a1cbe2fac8486161bb1bd7ca6d1f4d24138dbb6b2e12c029e460db0749fef991b9
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.1.1 (2019-10-24)
6
+
7
+ - [PR#7](https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries/pull/7) Improved Redis configuration – added `Proc` and `ConnectionPool` support ([@DmitryTsepelev][])
8
+
5
9
  ## 0.1.0 (2019-10-21)
6
10
 
7
11
  - Initial version ([@DmitryTsepelev][])
data/README.md CHANGED
@@ -63,11 +63,27 @@ All the queries are stored in memory by default, but you can easily switch to _r
63
63
 
64
64
  ```ruby
65
65
  class GraphqlSchema < GraphQL::Schema
66
- use GraphQL::PersistedQueries, store: :redis, redis_url: ENV["MY_REDIS_URL"]
66
+ use GraphQL::PersistedQueries, store: :redis, redis_client: { redis_url: ENV["MY_REDIS_URL"] }
67
67
  end
68
68
  ```
69
69
 
70
- If you have `ENV["REDIS_URL"]` configured – you don't need to pass it explicitly. Also, you can pass `:redis_host`, `:redis_port` and `:redis_db_name` to build the URL from scratch or configure Redis client as you want and pass it as the `:client` option.
70
+ If you have `ENV["REDIS_URL"]` configured – you don't need to pass it explicitly. Also, you can pass `:redis_host`, `:redis_port` and `:redis_db_name` inside the `:redis_client` hash to build the URL from scratch or pass the configured `Redis` or `ConnectionPool` object:
71
+
72
+ ```ruby
73
+ class GraphqlSchema < GraphQL::Schema
74
+ use GraphQL::PersistedQueries,
75
+ store: :redis,
76
+ redis_client: { redis_host: "127.0.0.2", redis_port: "2214", redis_db_name: "7" }
77
+ # or
78
+ use GraphQL::PersistedQueries,
79
+ store: :redis,
80
+ redis_client: Redis.new(url: "redis://127.0.0.2:2214/7")
81
+ # or
82
+ use GraphQL::PersistedQueries,
83
+ store: :redis,
84
+ redis_client: ConnectionPool.new { Redis.new(url: "redis://127.0.0.2:2214/7") }
85
+ end
86
+ ```
71
87
 
72
88
  ## Alternative hash functions
73
89
 
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "rake", ">= 10.0"
29
29
  spec.add_development_dependency "rubocop", "0.75"
30
30
  spec.add_development_dependency "redis"
31
+ spec.add_development_dependency "connection_pool"
31
32
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module PersistedQueries
5
+ module StoreAdapters
6
+ # Builds Redis object instance based on passed hash
7
+ class RedisClientBuilder
8
+ def initialize(redis_url: nil, redis_host: nil, redis_port: nil, redis_db_name: nil)
9
+ require "redis"
10
+
11
+ @redis_url = redis_url
12
+ @redis_host = redis_host
13
+ @redis_port = redis_port
14
+ @redis_db_name = redis_db_name
15
+ rescue LoadError => e
16
+ msg = "Could not load the 'redis' gem, please add it to your gemfile or " \
17
+ "configure a different adapter, e.g. use GraphQL::PersistedQueries, store: :memory"
18
+ raise e.class, msg, e.backtrace
19
+ end
20
+
21
+ def build
22
+ if @redis_url && (@redis_host || @redis_port || @redis_db_name)
23
+ raise ArgumentError, "redis_url cannot be passed along with redis_host, redis_port " \
24
+ "or redis_db_name options"
25
+ end
26
+
27
+ ::Redis.new(url: @redis_url || build_redis_url)
28
+ end
29
+
30
+ private
31
+
32
+ DEFAULT_REDIS_DB = "0"
33
+
34
+ def build_redis_url
35
+ db_name = @redis_db_name || DEFAULT_REDIS_DB
36
+ base_url = ENV["REDIS_URL"] || "redis://#{@redis_host}:#{@redis_port}"
37
+ URI.join(base_url, db_name).to_s
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,65 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "graphql/persisted_queries/store_adapters/redis_client_builder"
4
+
3
5
  module GraphQL
4
6
  module PersistedQueries
5
7
  module StoreAdapters
6
8
  # Redis adapter for storing persisted queries
7
9
  class RedisStoreAdapter < BaseStoreAdapter
8
- attr_reader :storage
9
-
10
- def initialize(client: nil, **options)
11
- require "redis"
12
-
13
- if client && options.any?
14
- raise ArgumentError, "client cannot be passed along with redis_url, redis_host" \
15
- ", redis_port or redis_db_name options"
16
- end
17
-
18
- @storage = client || configure_redis_client(options)
19
- rescue LoadError => e
20
- msg = "Could not load the 'redis' gem, please add it to your gemfile or " \
21
- "configure a different adapter, e.g. use GraphQL::PersistedQueries, store: :memory"
22
- raise e.class, msg, e.backtrace
10
+ def initialize(redis_client:)
11
+ @redis_proc = build_redis_proc(redis_client)
23
12
  end
24
13
 
25
14
  def fetch_query(hash)
26
- storage.get(key_for(hash))
15
+ @redis_proc.call { |redis| redis.get(key_for(hash)) }
27
16
  end
28
17
 
29
18
  def save_query(hash, query)
30
- storage.set(key_for(hash), query)
19
+ @redis_proc.call { |redis| redis.set(key_for(hash), query, ex: 24 * 60 * 60) }
31
20
  end
32
21
 
33
22
  private
34
23
 
35
24
  def key_for(hash)
36
- "persisted-query-#{hash}"
25
+ "graphql-persisted-query:#{hash}"
37
26
  end
38
27
 
39
- # rubocop:disable Metrics/LineLength
40
- def configure_redis_client(redis_url: nil, redis_host: nil, redis_port: nil, redis_db_name: nil)
41
- if redis_url && (redis_host || redis_port || redis_db_name)
42
- raise ArgumentError, "redis_url cannot be passed along with redis_host, redis_port " \
43
- "or redis_db_name options"
28
+ # rubocop: disable Metrics/MethodLength
29
+ # rubocop: disable Metrics/CyclomaticComplexity
30
+ # rubocop: disable Metrics/PerceivedComplexity
31
+ def build_redis_proc(redis_client)
32
+ if redis_client.is_a?(Hash)
33
+ build_redis_proc(RedisClientBuilder.new(redis_client).build)
34
+ elsif redis_client.is_a?(Proc)
35
+ redis_client
36
+ elsif defined?(::Redis) && redis_client.is_a?(::Redis)
37
+ proc { |&b| b.call(redis_client) }
38
+ elsif defined?(ConnectionPool) && redis_client.is_a?(ConnectionPool)
39
+ proc { |&b| redis_client.with { |r| b.call(r) } }
40
+ else
41
+ raise ArgumentError, ":redis_client accepts Redis, ConnectionPool, Hash or Proc only"
44
42
  end
45
-
46
- redis_url ||= build_redis_url(
47
- redis_host: redis_host,
48
- redis_port: redis_port,
49
- redis_db_name: redis_db_name
50
- )
51
-
52
- Redis.new(url: redis_url)
53
- end
54
- # rubocop:enable Metrics/LineLength
55
-
56
- DEFAULT_REDIS_DB = "0"
57
-
58
- def build_redis_url(redis_host: nil, redis_port: nil, redis_db_name: nil)
59
- redis_db_name ||= DEFAULT_REDIS_DB
60
- redis_base_url = ENV["REDIS_URL"] || "redis://#{redis_host}:#{redis_port}"
61
- URI.join(redis_base_url, redis_db_name).to_s
62
43
  end
44
+ # rubocop: enable Metrics/MethodLength
45
+ # rubocop: enable Metrics/CyclomaticComplexity
46
+ # rubocop: enable Metrics/PerceivedComplexity
63
47
  end
64
48
  end
65
49
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module PersistedQueries
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-persisted_queries
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-21 00:00:00.000000000 Z
11
+ date: 2019-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: connection_pool
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'
83
97
  description: Persisted queries for graphql-ruby
84
98
  email:
85
99
  - dmitry.a.tsepelev@gmail.com
@@ -108,6 +122,7 @@ files:
108
122
  - lib/graphql/persisted_queries/store_adapters.rb
109
123
  - lib/graphql/persisted_queries/store_adapters/base_store_adapter.rb
110
124
  - lib/graphql/persisted_queries/store_adapters/memory_store_adapter.rb
125
+ - lib/graphql/persisted_queries/store_adapters/redis_client_builder.rb
111
126
  - lib/graphql/persisted_queries/store_adapters/redis_store_adapter.rb
112
127
  - lib/graphql/persisted_queries/version.rb
113
128
  homepage: https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries