rack-graphql 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +54 -2
- data/lib/rack_graphql/application.rb +8 -6
- data/lib/rack_graphql/version.rb +1 -1
- data/rack-graphql.gemspec +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afa1d729bd08ddcda6bb8f5b0d6a64b7c4d339bb96abc34ae75840cb30c00175
|
4
|
+
data.tar.gz: a0fe1c3fb2b0aa9104f44db024305f98f9901567fd5f3765c8c5b5b76bd55c4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2793fd223e882a6e91f4389d65dd9a95e4ec40df68c77d11392599c1775ec04228287a797781e01c41dba5d4972fd5ac13b03346c6e78a87b7cd99467d3e5c32
|
7
|
+
data.tar.gz: f4feff581c4ea565a2bb5672a45fd6aac01d7f8cd4962b53eecab128ba41e81b66e931deb28615dcaef4e53ca58fd766be7230d904dd03ac8be3524add09d24c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
[![Gem Version](https://badge.fury.io/rb/rack-
|
2
|
-
[![Build Status](https://travis-ci.org/RenoFi/rack-
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/rack-graphql.svg)](https://rubygems.org/gems/rack-graphql)
|
2
|
+
[![Build Status](https://travis-ci.org/RenoFi/rack-graphql.svg?branch=master)](https://travis-ci.org/RenoFi/rack-graphql)
|
3
3
|
|
4
4
|
# rack-graphql
|
5
5
|
|
6
|
+
Rack middleware implementing graphql endpoint for ruby (non-`ActionController`) services. It uses pure rack and none of `ActionController` or `Sinatra` is required. By default it implements health route on `/health` and `/`, since it's only expected to be used on graphql-only services.
|
7
|
+
|
8
|
+
It also handles [subscriptions](https://graphql-ruby.org/guides#subscriptions-guides) and [multiplex](https://graphql-ruby.org/queries/multiplex.html).
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -14,6 +17,55 @@ gem 'rack-graphql'
|
|
14
17
|
|
15
18
|
## Usage example
|
16
19
|
|
20
|
+
Add to your `config.ru` file
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
run RackGraphql::Application.call(
|
24
|
+
schema: YourGraqphqlSchema, # required
|
25
|
+
app_name: 'your-service-name', # optional, used for health route
|
26
|
+
context_handler: YourGraphqlContextHandler, # optional, empty proc by default
|
27
|
+
health_route: true, # optional, true by default
|
28
|
+
)
|
29
|
+
```
|
30
|
+
|
31
|
+
`context_handler` can be a class, object or proc. It only must respond to `call` merhod taking `env` as an argument. It is supposed to decode request properties to graphql context (eg. jwt token to user object, as shown below).
|
32
|
+
|
33
|
+
### Example: using context handler for JWT authentication
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class GraphqlContextHandler
|
37
|
+
class << self
|
38
|
+
def call(env)
|
39
|
+
payload = decode_payload(env)
|
40
|
+
|
41
|
+
graphql_context_hash(payload)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def graphql_context_hash(payload)
|
47
|
+
{
|
48
|
+
current_user: current_user(payload)
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def decode_payload(env)
|
53
|
+
jwt = env["HTTP_AUTHORIZATION"].to_s.split(' ').last
|
54
|
+
|
55
|
+
return if jwt.blank?
|
56
|
+
|
57
|
+
DecodeJwt.call(jwt) || {}
|
58
|
+
end
|
59
|
+
|
60
|
+
def current_user(payload)
|
61
|
+
return unless payload
|
62
|
+
return unless payload['user_id']
|
63
|
+
|
64
|
+
UserRepo.find_by_id(payload['user_id])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
17
69
|
|
18
70
|
## Contributing
|
19
71
|
|
@@ -1,17 +1,19 @@
|
|
1
1
|
module RackGraphql
|
2
2
|
class Application
|
3
|
-
def self.call(schema:, app_name: 'rack-graphql-service', context_handler: nil)
|
3
|
+
def self.call(schema:, app_name: 'rack-graphql-service', context_handler: nil, health_route: true)
|
4
4
|
::Rack::Builder.new do
|
5
5
|
map '/graphql' do
|
6
6
|
run RackGraphql::Middleware.new(schema: schema, context_handler: context_handler)
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
if health_route
|
10
|
+
map '/health' do
|
11
|
+
run ->(env) { RackGraphql::HealthResponseBuilder.new(app_name: app_name).build }
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
map '/' do
|
15
|
+
run ->(env) { RackGraphql::HealthResponseBuilder.new(app_name: app_name).build }
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
data/lib/rack_graphql/version.rb
CHANGED
data/rack-graphql.gemspec
CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
+
spec.required_ruby_version = '>= 2.4'
|
26
|
+
|
25
27
|
spec.add_dependency 'graphql', '>= 1.9.0'
|
26
28
|
spec.add_dependency 'multi_json'
|
27
29
|
spec.add_dependency 'rack', '>= 2.0.0'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-graphql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Krzysztof Knapik
|
@@ -178,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
178
178
|
requirements:
|
179
179
|
- - ">="
|
180
180
|
- !ruby/object:Gem::Version
|
181
|
-
version: '
|
181
|
+
version: '2.4'
|
182
182
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
183
|
requirements:
|
184
184
|
- - ">="
|