rack-graphql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bacf440a72a93e3a15d93d3250673e3e881a7b9c00baeb23a3b492a7bd173726
4
+ data.tar.gz: b025f5b71e3523a9cb420eeb7e84b32244e7b8b2733e2e9233534e7fd15b3235
5
+ SHA512:
6
+ metadata.gz: 37c256d2391cd25a8100885dc46bd7fe09b43441204f8f6265664663759c0bfe5e0329f59e421181074f8153d4eb4f778f6eeb27353053b79608cf5e596c0271
7
+ data.tar.gz: 017c9499ad224dd3ff714f4f28438c69298be871222ed2a639e5ce4bab3f2207b8c645dd3164ddd0628a300d30f6dde84d1a85aebd6bbaec63f64a5ac9c5ca33
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,4 @@
1
+ fail_on_violations: true
2
+
3
+ rubocop:
4
+ config_file: .rubocop.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,62 @@
1
+ Gemspec/RequiredRubyVersion:
2
+ Include:
3
+ - 2.4
4
+ - 2.5
5
+ - 2.6
6
+
7
+ Lint/UnusedBlockArgument:
8
+ Enabled: false
9
+
10
+ Layout/AlignHash:
11
+ Enabled: false
12
+
13
+ Naming/FileName:
14
+ Enabled: false
15
+
16
+ Metrics/BlockLength:
17
+ Max: 30
18
+ Exclude:
19
+ - '**/*_spec.rb'
20
+
21
+ Metrics/LineLength:
22
+ Max: 140
23
+
24
+ Metrics/MethodLength:
25
+ Max: 20
26
+
27
+ Security/YAMLLoad:
28
+ Enabled: false
29
+
30
+ Style/BlockDelimiters:
31
+ EnforcedStyle: braces_for_chaining
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Style/GuardClause:
37
+ Enabled: false
38
+
39
+ Style/EachWithObject:
40
+ Enabled: false
41
+
42
+ Style/MethodMissingSuper:
43
+ Enabled: false
44
+
45
+ Style/MissingRespondToMissing:
46
+ Enabled: false
47
+
48
+ Style/FrozenStringLiteralComment:
49
+ Enabled: false
50
+
51
+ Style/PercentLiteralDelimiters:
52
+ Enabled: false
53
+
54
+ Style/RescueStandardError:
55
+ Exclude:
56
+ - 'spec/*.rb'
57
+
58
+ Style/SymbolArray:
59
+ EnforcedStyle: brackets
60
+
61
+ Style/TrivialAccessors:
62
+ Enabled: false
@@ -0,0 +1,9 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.6
7
+ - 2.5.5
8
+ - 2.6.3
9
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-graphql.gemspec
4
+ gemspec
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-graphql (0.1.0)
5
+ graphql (>= 1.9.0)
6
+ multi_json
7
+ rack (>= 2.0.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.0)
13
+ coderay (1.1.2)
14
+ diff-lcs (1.3)
15
+ graphql (1.9.6)
16
+ jaro_winkler (1.5.2)
17
+ method_source (0.9.2)
18
+ multi_json (1.13.1)
19
+ parallel (1.17.0)
20
+ parser (2.6.3.0)
21
+ ast (~> 2.4.0)
22
+ pry (0.12.2)
23
+ coderay (~> 1.1.0)
24
+ method_source (~> 0.9.0)
25
+ rack (2.0.7)
26
+ rack-test (1.1.0)
27
+ rack (>= 1.0, < 3)
28
+ rainbow (3.0.0)
29
+ rake (10.5.0)
30
+ rspec (3.8.0)
31
+ rspec-core (~> 3.8.0)
32
+ rspec-expectations (~> 3.8.0)
33
+ rspec-mocks (~> 3.8.0)
34
+ rspec-core (3.8.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-expectations (3.8.4)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.8.0)
39
+ rspec-mocks (3.8.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.8.0)
42
+ rspec-support (3.8.2)
43
+ rubocop (0.71.0)
44
+ jaro_winkler (~> 1.5.1)
45
+ parallel (~> 1.10)
46
+ parser (>= 2.6)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 1.7)
50
+ ruby-progressbar (1.10.1)
51
+ unicode-display_width (1.6.0)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ bundler (~> 2.0)
58
+ pry (~> 0.12)
59
+ rack-graphql!
60
+ rack-test (~> 1.1)
61
+ rake (~> 10.0)
62
+ rspec (~> 3.0)
63
+ rubocop (~> 0.71)
64
+
65
+ BUNDLED WITH
66
+ 2.0.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 RenoFi Engineering Team & Krzysztof Knapik
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.
@@ -0,0 +1,24 @@
1
+ [![Gem Version](https://badge.fury.io/rb/rack-pagination.svg)](https://rubygems.org/gems/rack-graphql)
2
+ [![Build Status](https://travis-ci.org/RenoFi/rack-pagination.svg?branch=master)](https://travis-ci.org/RenoFi/rack-graphql)
3
+
4
+ # rack-graphql
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rack-graphql'
13
+ ```
14
+
15
+ ## Usage example
16
+
17
+
18
+ ## Contributing
19
+
20
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RenoFi/rack-graphql. 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.
21
+
22
+ ## License
23
+
24
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rack_graphql'
5
+ require 'pry'
6
+ require 'irb'
7
+
8
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,8 @@
1
+ require 'multi_json'
2
+ require 'rack'
3
+ require 'graphql'
4
+
5
+ require 'rack_graphql/version'
6
+ require 'rack_graphql/health_response_builder'
7
+ require 'rack_graphql/middleware'
8
+ require 'rack_graphql/application'
@@ -0,0 +1 @@
1
+ require 'rack-graphql'
@@ -0,0 +1,19 @@
1
+ module RackGraphql
2
+ class Application
3
+ def self.call(schema:, app_name: 'rack-graphql-service', context_handler: nil)
4
+ ::Rack::Builder.new do
5
+ map '/graphql' do
6
+ run RackGraphql::Middleware.new(schema: schema, context_handler: context_handler)
7
+ end
8
+
9
+ map '/health' do
10
+ run ->(env) { RackGraphql::HealthResponseBuilder.new(app_name: app_name).build }
11
+ end
12
+
13
+ map '/' do
14
+ run ->(env) { RackGraphql::HealthResponseBuilder.new(app_name: app_name).build }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module RackGraphql
2
+ class HealthResponseBuilder
3
+ def initialize(app_name:)
4
+ @app_name = app_name
5
+ end
6
+
7
+ def build
8
+ [200, headers, [body]]
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :app_name
14
+
15
+ def headers
16
+ { 'Content-Type' => 'application/json' }
17
+ end
18
+
19
+ def body
20
+ MultiJson.dump(
21
+ status: :ok,
22
+ app_name: app_name,
23
+ env: ENV['RACK_ENV'],
24
+ host: ENV['HOSTNAME'],
25
+ revision: ENV['REVISION']
26
+ )
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,103 @@
1
+ module RackGraphql
2
+ class Middleware
3
+ def initialize(schema:, context_handler: nil)
4
+ @schema = schema
5
+ @context_handler = context_handler || ->(_) {}
6
+ end
7
+
8
+ def call(env)
9
+ return [406, {}, []] unless post_request?(env)
10
+
11
+ params = post_data(env)
12
+ variables = ensure_hash(params['variables'])
13
+ operation_name = params['operationName']
14
+ context = context_handler.call(env)
15
+
16
+ result = execute(params: params, operation_name: operation_name, variables: variables, context: context)
17
+
18
+ [200, response_headers(result), [MultiJson.dump(result)]]
19
+ rescue ArgumentError
20
+ [400, { 'Content-Type' => 'application/json' }, [MultiJson.dump({})]]
21
+ ensure
22
+ ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :schema, :context_handler
28
+
29
+ def post_request?(env)
30
+ env['REQUEST_METHOD'] == 'POST'
31
+ end
32
+
33
+ def post_data(env)
34
+ ::MultiJson.load(env['rack.input'].gets)
35
+ rescue MultiJson::ParseError
36
+ {}
37
+ end
38
+
39
+ # Handle form data, JSON body, or a blank value
40
+ def ensure_hash(ambiguous_param)
41
+ case ambiguous_param
42
+ when String
43
+ return {} if ambiguous_param.empty?
44
+
45
+ begin
46
+ ensure_hash(::MultiJson.load(ambiguous_param))
47
+ rescue MultiJson::ParseError
48
+ raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
49
+ end
50
+ when Hash
51
+ ambiguous_param
52
+ when nil
53
+ {}
54
+ else
55
+ raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
56
+ end
57
+ end
58
+
59
+ def execute(params:, operation_name:, variables:, context:)
60
+ if valid_multiplex?(params)
61
+ execute_multi(params['_json'], operation_name: operation_name, variables: variables, context: context)
62
+ else
63
+ execute_single(params['query'], operation_name: operation_name, variables: variables, context: context)
64
+ end
65
+ end
66
+
67
+ def execute_single(query, operation_name:, variables:, context:)
68
+ schema.execute(query, operation_name: operation_name, variables: variables, context: context)
69
+ end
70
+
71
+ def valid_multiplex?(params)
72
+ params['_json'].is_a?(Array) && params['_json'].all? { |j| j.is_a?(Hash) }
73
+ end
74
+
75
+ def execute_multi(queries_params, operation_name:, variables:, context:)
76
+ queries = queries_params.map do |param|
77
+ {
78
+ query: param['query'],
79
+ operation_name: operation_name,
80
+ variables: variables,
81
+ context: context
82
+ }
83
+ end
84
+
85
+ schema.multiplex(queries)
86
+ end
87
+
88
+ def response_headers(result = nil)
89
+ {
90
+ 'Access-Control-Expose-Headers' => 'X-Subscription-ID',
91
+ 'Content-Type' => 'application/json'
92
+ }.tap do |headers|
93
+ headers['X-Subscription-ID'] = result.context[:subscription_id] if result_subscription?(result)
94
+ end
95
+ end
96
+
97
+ def result_subscription?(result)
98
+ return false unless result.is_a?(GraphQL::Query::Result)
99
+
100
+ result.subscription?
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module RackGraphql
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rack_graphql/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rack-graphql'
7
+ spec.version = RackGraphql::VERSION
8
+ spec.authors = ['Krzysztof Knapik', 'RenoFi Engineering Team']
9
+ spec.email = ['knapo@knapo.net', 'engineering@renofi.com']
10
+
11
+ spec.summary = 'Rack middleware implementing graphql endpoint.'
12
+ spec.homepage = 'https://github.com/RenoFi/rack-graphql'
13
+ spec.license = 'MIT'
14
+
15
+ spec.metadata['homepage_uri'] = 'https://github.com/RenoFi/rack-graphql'
16
+ spec.metadata['source_code_uri'] = 'https://github.com/RenoFi/rack-graphql'
17
+
18
+ spec.files = Dir.chdir(__dir__) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'graphql', '>= 1.9.0'
26
+ spec.add_dependency 'multi_json'
27
+ spec.add_dependency 'rack', '>= 2.0.0'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 2.0'
30
+ spec.add_development_dependency 'pry', '~> 0.12'
31
+ spec.add_development_dependency 'rack-test', '~> 1.1'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'rubocop', '~> 0.71'
35
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-graphql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Knapik
8
+ - RenoFi Engineering Team
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2019-06-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: graphql
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.9.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.9.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: multi_json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rack
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.0.0
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.0.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.12'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.12'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rack-test
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.1'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.1'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rake
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '10.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '10.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rspec
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '3.0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '3.0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rubocop
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '0.71'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '0.71'
140
+ description:
141
+ email:
142
+ - knapo@knapo.net
143
+ - engineering@renofi.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".gitignore"
149
+ - ".hound.yml"
150
+ - ".rspec"
151
+ - ".rubocop.yml"
152
+ - ".travis.yml"
153
+ - Gemfile
154
+ - Gemfile.lock
155
+ - LICENSE
156
+ - README.md
157
+ - Rakefile
158
+ - bin/console
159
+ - bin/setup
160
+ - lib/rack-graphql.rb
161
+ - lib/rack_graphql.rb
162
+ - lib/rack_graphql/application.rb
163
+ - lib/rack_graphql/health_response_builder.rb
164
+ - lib/rack_graphql/middleware.rb
165
+ - lib/rack_graphql/version.rb
166
+ - rack-graphql.gemspec
167
+ homepage: https://github.com/RenoFi/rack-graphql
168
+ licenses:
169
+ - MIT
170
+ metadata:
171
+ homepage_uri: https://github.com/RenoFi/rack-graphql
172
+ source_code_uri: https://github.com/RenoFi/rack-graphql
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubygems_version: 3.0.3
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: Rack middleware implementing graphql endpoint.
192
+ test_files: []