graphql-batch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3cf4d18124c65f414eb8fcdc54c0dab15e569041
4
+ data.tar.gz: fddb094273f8333c2cf25300c9a813104d382d6b
5
+ SHA512:
6
+ metadata.gz: 2867edc13e5537b9999d2f1e0752c16524fa8a818ee5d834e2e7ef5273fcbf19db6e3a1f90a22a7874ca38ec7b312c35fd2412d6afae0dda07970bef23cff564
7
+ data.tar.gz: bc0e578c9315a211acd23748e765b8b1e993c34edf30e0f4dbd05acf97bc69b5a5671aa5015931491b6017f6940afbf57d2b5f29b0d1faa79f84530161a42488
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Shopify Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # Graphql::Batch
2
+
3
+ Provides an executor for the [`graphql` gem](https://github.com/rmosolgo/graphql-ruby) which allows queries to be batched.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'graphql-batch'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install graphql-batch
20
+
21
+ ## Usage
22
+
23
+ ### Basic Usage
24
+
25
+ Require the library
26
+
27
+ ```ruby
28
+ require 'graphql/batch'
29
+ ```
30
+
31
+ Define a GraphQL::Batch::Query derived class. Use group_key to specify which queries can be reduced into a batch query, and an execute class method which makes that batch query and passes the result to each individual query.
32
+
33
+ ```ruby
34
+ class FindQuery < GraphQL::Batch::Query
35
+ attr_reader :model, :id
36
+
37
+ def initialize(model, id, &block)
38
+ @model = model
39
+ @id = id
40
+ super(&block)
41
+ end
42
+
43
+ # super returns the class name
44
+ def group_key
45
+ "#{super}:#{model.name}"
46
+ end
47
+
48
+ def self.execute(queries)
49
+ model = queries.first.model
50
+ ids = queries.map(&:id)
51
+ records_by_id = model.where(id: ids).index_by(&:id)
52
+ queries.each do |query|
53
+ query.complete(records_by_id[query.id])
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ When defining your schema, using the graphql gem, return a your batch query object from the resolve proc.
60
+
61
+ ```ruby
62
+ resolve -> (obj, args, context) { FindQuery.new(Product, args["id"]) }
63
+ ```
64
+
65
+ Use the batch execution strategy with your schema
66
+
67
+ ```ruby
68
+ MySchema = GraphQL::Schema.new(query: MyQueryType)
69
+ MySchema.query_execution_strategy = GraphQL::Batch::ExecutionStrategy
70
+ ```
71
+
72
+ ### Query Dependant Computed Fields
73
+
74
+ If you don't want to use a query result directly, then you can pass a block which gets called after the query completes.
75
+
76
+ ```ruby
77
+ resolve -> (obj, args, context) do
78
+ FindQuery.new(Product, args["id"]) do |product|
79
+ product.title
80
+ end
81
+ end
82
+ ```
83
+
84
+ You may also need to do another query that depends on the first one to get the result, in which case the query block can return another query.
85
+
86
+ ```ruby
87
+ resolve -> (obj, args, context) do
88
+ FindQuery.new(Product, args["id"]) do |product|
89
+ FindQuery.new(Image, product.image_id)
90
+ end
91
+ end
92
+ ```
93
+
94
+ If the second query doesn't depend on the other one, then you can use GraphQL::Batch::QueryGroup, which allows each query in the group to be batched with other queries.
95
+
96
+ ```ruby
97
+ resolve -> (obj, args, context) do
98
+ smart_collection_query = CountQuery.new(SmartCollection, context.shop_id)
99
+ custom_collection_query = CountQuery.new(CustomCollection, context.shop_id)
100
+
101
+ QueryGroup.new([smart_collection_query, custom_collection_query]) do
102
+ smart_collection_query.result + custom_collection_query.result
103
+ end
104
+ end
105
+ ```
106
+
107
+ ## Development
108
+
109
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql-batch.
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "graphql/batch"
5
+
6
+ require "irb"
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphql/batch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "graphql-batch"
8
+ spec.version = Graphql::Batch::VERSION
9
+ spec.authors = ["Dylan Thacker-Smith"]
10
+ spec.email = ["Dylan.Smith@shopify.com"]
11
+
12
+ spec.summary = "A query batching executor for the graphql gem"
13
+ spec.homepage = "https://github.com/Shopify/graphql-batch"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "graphql", "~>0.8"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,6 @@
1
+ require "graphql"
2
+ require_relative "batch/version"
3
+ require_relative "batch/query_container"
4
+ require_relative "batch/query"
5
+ require_relative "batch/query_group"
6
+ require_relative "batch/execution_strategy"
@@ -0,0 +1,57 @@
1
+ module GraphQL::Batch
2
+ class ExecutionStrategy < GraphQL::Query::SerialExecution
3
+ attr_reader :batched_queries
4
+
5
+ def initialize
6
+ @batched_queries = Hash.new{ |hash, key| hash[key] = [] }
7
+ end
8
+
9
+ class OperationResolution < GraphQL::Query::SerialExecution::OperationResolution
10
+ def result
11
+ result = super
12
+ until execution_strategy.batched_queries.empty?
13
+ queries = execution_strategy.batched_queries.shift.last
14
+ queries.first.class.execute(queries)
15
+ end
16
+ result
17
+ end
18
+ end
19
+
20
+ class SelectionResolution < GraphQL::Query::SerialExecution::SelectionResolution
21
+ def result
22
+ result_hash = super
23
+ result_hash.each do |key, value|
24
+ if value.is_a?(FieldResolution)
25
+ value.result_hash = result_hash
26
+ end
27
+ end
28
+ result_hash
29
+ end
30
+ end
31
+
32
+ class FieldResolution < GraphQL::Query::SerialExecution::FieldResolution
33
+ attr_accessor :result_hash
34
+
35
+ def get_finished_value(raw_value)
36
+ if raw_value.is_a?(QueryContainer)
37
+ raw_value.query_listener = self
38
+ register_queries(raw_value)
39
+ self
40
+ else
41
+ super
42
+ end
43
+ end
44
+
45
+ def register_queries(query_container)
46
+ query_container.each_query do |query|
47
+ execution_strategy.batched_queries[query.group_key] << query
48
+ end
49
+ end
50
+
51
+ def query_completed(query)
52
+ result_key = ast_node.alias || ast_node.name
53
+ @result_hash[result_key] = get_finished_value(query.result)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,29 @@
1
+ module GraphQL::Batch
2
+ class Query < QueryContainer
3
+ def initialize(&block)
4
+ @block = block
5
+ end
6
+
7
+ # batched queries with the same key are merged together
8
+ def group_key
9
+ self.class.name
10
+ end
11
+
12
+ def each_query
13
+ yield self
14
+ end
15
+
16
+ def complete(result)
17
+ if @block
18
+ result = @block.call(result)
19
+ @block = nil
20
+ end
21
+ super(result)
22
+ end
23
+
24
+ # execute queries, with the same group_key, as a batch
25
+ def self.execute(queries)
26
+ raise NotImplementedError
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ module GraphQL::Batch
2
+ class QueryContainer
3
+ attr_accessor :query_listener, :result
4
+
5
+ def each_query
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def complete(result)
10
+ if result.is_a?(QueryContainer)
11
+ result.query_listener = self
12
+ register_queries(result)
13
+ else
14
+ if instance_variable_defined?(:@result)
15
+ raise "Query was already completed"
16
+ end
17
+ @result = result
18
+ query_listener.query_completed(self)
19
+ end
20
+ end
21
+
22
+ def query_completed(query)
23
+ complete(query.result)
24
+ end
25
+
26
+ def register_queries(query_container)
27
+ query_listener.register_queries(query_container)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ module GraphQL::Batch
2
+ class QueryGroup < QueryContainer
3
+ def initialize(queries, &block)
4
+ @pending_queries = queries.dup
5
+ @pending_queries.each do |query|
6
+ query.query_listener = self
7
+ end
8
+ @block = block
9
+ raise ArgumentError, "QueryGroup requires a block" unless block
10
+ end
11
+
12
+ def each_query
13
+ @pending_queries.each do |query_container|
14
+ query_container.each_query do |query|
15
+ yield query
16
+ end
17
+ end
18
+ end
19
+
20
+ def query_completed(query)
21
+ @pending_queries.delete(query)
22
+ if @pending_queries.empty?
23
+ result = @block.call
24
+ @block = nil
25
+ if result.is_a?(QueryContainer)
26
+ result.query_listener = self
27
+ @pending_queries << result
28
+ register_queries(result)
29
+ else
30
+ complete(result)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Graphql
2
+ module Batch
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-batch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Thacker-Smith
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - Dylan.Smith@shopify.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - graphql-batch.gemspec
85
+ - lib/graphql/batch.rb
86
+ - lib/graphql/batch/execution_strategy.rb
87
+ - lib/graphql/batch/query.rb
88
+ - lib/graphql/batch/query_container.rb
89
+ - lib/graphql/batch/query_group.rb
90
+ - lib/graphql/batch/version.rb
91
+ homepage: https://github.com/Shopify/graphql-batch
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A query batching executor for the graphql gem
115
+ test_files: []