graphql-batch 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +2 -2
- data/examples/http_loader.rb +65 -0
- data/lib/graphql/batch.rb +7 -5
- data/lib/graphql/batch/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 257c2068b92162f1e38b17192c08979fdfcd04fe4f4fa93d2a290dc3746e12ec
|
4
|
+
data.tar.gz: 4b050c894e800925946423723a6431c5a28f3486c4f5de6f2e80c73ed578db18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f2d63cc78370652458bc351d5d05f0502f7429c66e4bd0676b1fd62eea0fc9a6eb359d7df9213a3be413bbcd81b29a425e300c8c9f21769b13a0e91864e59b7
|
7
|
+
data.tar.gz: 0d35c4b83c8f8d732dc24889b16a4ad672e308582b4aab9c2a7f84da6d8388a5df8e0ae5adcca4e5ccb538b5c2f2568e772c635bff31663bb1620358ee98e3f6
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -143,11 +143,11 @@ end
|
|
143
143
|
```ruby
|
144
144
|
def product(id:)
|
145
145
|
# Try the cache first ...
|
146
|
-
CacheLoader.for(Product).load(
|
146
|
+
CacheLoader.for(Product).load(id).then(nil, lambda do |exc|
|
147
147
|
# But if there's a connection error, go to the underlying database
|
148
148
|
raise exc unless exc.is_a?(Redis::BaseConnectionError)
|
149
149
|
logger.warn err.message
|
150
|
-
RecordLoader.for(Product).load(
|
150
|
+
RecordLoader.for(Product).load(id)
|
151
151
|
end)
|
152
152
|
end
|
153
153
|
```
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# A sample HTTP loader using:
|
2
|
+
#
|
3
|
+
# 1. https://github.com/httprb/http
|
4
|
+
# 2. https://github.com/mperham/connection_pool
|
5
|
+
# 3. https://github.com/ruby-concurrency/concurrent-ruby
|
6
|
+
#
|
7
|
+
# Setup:
|
8
|
+
#
|
9
|
+
# field :weather, String, null: true do
|
10
|
+
# argument :lat, Float, required: true
|
11
|
+
# argument :lng, Float, required: true
|
12
|
+
# argument :lang, String, required: true
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# def weather(lat:, lng:, lang:)
|
16
|
+
# key = Rails.application.credentials.darksky_key
|
17
|
+
# path = "/forecast/#{key}/#{lat},#{lng}?lang=#{lang}"
|
18
|
+
# Loaders::HTTPLoader
|
19
|
+
# .for(host: 'https://api.darksky.net')
|
20
|
+
# .load(->(connection) { connection.get(path).flush })
|
21
|
+
# .then do |response|
|
22
|
+
# if response.status.ok?
|
23
|
+
# json = JSON.parse(response.body)
|
24
|
+
# json['currently']['summary']
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Querying:
|
30
|
+
#
|
31
|
+
# <<~GQL
|
32
|
+
# query Weather {
|
33
|
+
# montreal: weather(lat: 45.5017, lng: -73.5673, lang: "fr")
|
34
|
+
# waterloo: weather(lat: 43.4643, lng: -80.5204, lang: "en")
|
35
|
+
# }
|
36
|
+
# GQL
|
37
|
+
|
38
|
+
module Loaders
|
39
|
+
class HTTPLoader < GraphQL::Batch::Loader
|
40
|
+
def initialize(host:, size: 4, timeout: 4)
|
41
|
+
@host = host
|
42
|
+
@size = size
|
43
|
+
@timeout = timeout
|
44
|
+
end
|
45
|
+
|
46
|
+
def perform(operations)
|
47
|
+
futures = operations.map do |operation|
|
48
|
+
Concurrent::Promises.future do
|
49
|
+
pool.with { |connection| operation.call(connection) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
operations.each_with_index.each do |operation, index|
|
53
|
+
fulfill(operation, futures[index].value)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def pool
|
60
|
+
@pool ||= ConnectionPool.new(size: @size, timeout: @timeout) do
|
61
|
+
HTTP.persistent(@host)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/graphql/batch.rb
CHANGED
@@ -16,16 +16,18 @@ module GraphQL
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.use(schema_defn, executor_class: GraphQL::Batch::Executor)
|
19
|
-
|
20
|
-
|
19
|
+
# Support 1.10+ which passes the class instead of the definition proxy
|
20
|
+
schema = schema_defn.is_a?(Class) ? schema_defn : schema_defn.target
|
21
|
+
current_gem_version = Gem::Version.new(GraphQL::VERSION)
|
22
|
+
if current_gem_version >= Gem::Version.new("1.6.0")
|
21
23
|
instrumentation = GraphQL::Batch::SetupMultiplex.new(schema, executor_class: executor_class)
|
22
24
|
schema_defn.instrument(:multiplex, instrumentation)
|
23
25
|
if schema.mutation
|
24
|
-
if
|
25
|
-
schema.mutation.metadata[:type_class]
|
26
|
+
if current_gem_version >= Gem::Version.new('1.9.0.pre3') &&
|
27
|
+
(schema.mutation.is_a?(Class) || schema.mutation.metadata[:type_class])
|
26
28
|
require_relative "batch/mutation_field_extension"
|
27
29
|
schema.mutation.fields.each do |name, f|
|
28
|
-
field = f.metadata[:type_class]
|
30
|
+
field = f.respond_to?(:type_class) ? f.type_class : f.metadata[:type_class]
|
29
31
|
field.extension(GraphQL::Batch::MutationFieldExtension)
|
30
32
|
end
|
31
33
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-batch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dylan Thacker-Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|
@@ -103,6 +103,7 @@ files:
|
|
103
103
|
- bin/console
|
104
104
|
- bin/setup
|
105
105
|
- examples/association_loader.rb
|
106
|
+
- examples/http_loader.rb
|
106
107
|
- examples/record_loader.rb
|
107
108
|
- graphql-batch.gemspec
|
108
109
|
- lib/graphql/batch.rb
|
@@ -131,8 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
132
|
- !ruby/object:Gem::Version
|
132
133
|
version: '0'
|
133
134
|
requirements: []
|
134
|
-
|
135
|
-
rubygems_version: 2.7.6
|
135
|
+
rubygems_version: 3.0.3
|
136
136
|
signing_key:
|
137
137
|
specification_version: 4
|
138
138
|
summary: A query batching executor for the graphql gem
|