graphql_server 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -17
- data/lib/graphql_server.rb +7 -12
- 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: c127eea600c58ed637cc5b5a490f654bf25c52b4f8b25b30fc59bd83d48128c2
|
4
|
+
data.tar.gz: 35590cc4eba3d4c99e5ee11a8cd63317e896fd129bd9fc69030cd35704eefde9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c811324871ea85f0b0bd5c1099f130fdea05945cd4c878379856df8115e7529b6b1b439c2e42eba7cf560286fdbd14e6a2f8995ad3701478c8cd5ac44097e5d1
|
7
|
+
data.tar.gz: ab366054e9bf6fa338161b3bac062f349979551c47220a078393932491bfc112358cd0f70ee64a7103bc6eea5f52980f1cb3a7673e59b720af3e3d2f9da834a5
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ This is a simple spec-compliant GraphQL rack application and middleware based on
|
|
3
3
|
Install the gem:
|
4
4
|
|
5
5
|
```
|
6
|
-
gem install
|
6
|
+
gem install graphql_server
|
7
7
|
```
|
8
8
|
|
9
9
|
# Using the server
|
@@ -26,7 +26,7 @@ resolver = {
|
|
26
26
|
}
|
27
27
|
}
|
28
28
|
|
29
|
-
run
|
29
|
+
run GraphQLServer.new(type_def: type_def, resolver: resolver)
|
30
30
|
```
|
31
31
|
|
32
32
|
Start using `rackup`
|
@@ -35,7 +35,7 @@ Start using `rackup`
|
|
35
35
|
rackup app.ru
|
36
36
|
```
|
37
37
|
|
38
|
-
## As a middleware in your application
|
38
|
+
## As a middleware in your existing application
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
# app.ru
|
@@ -44,13 +44,7 @@ require 'graphql_server'
|
|
44
44
|
type_def = ...
|
45
45
|
resolver = ...
|
46
46
|
|
47
|
-
use
|
48
|
-
```
|
49
|
-
|
50
|
-
Start using `rackup`
|
51
|
-
|
52
|
-
```
|
53
|
-
rackup app.ru
|
47
|
+
use GraphQLServer, type_def: type_def, resolver: resolver
|
54
48
|
```
|
55
49
|
|
56
50
|
# Options
|
@@ -60,21 +54,21 @@ rackup app.ru
|
|
60
54
|
You can get started fast by writing a type defintions and a resolver hash
|
61
55
|
|
62
56
|
```ruby
|
63
|
-
|
57
|
+
GraphQLServer.new(type_def: type_def, resolver: resolver)
|
64
58
|
```
|
65
59
|
|
66
|
-
You can also provide your own schema
|
60
|
+
You can also provide your own [schema](http://graphql-ruby.org/schema/definition.html)
|
67
61
|
|
68
62
|
```ruby
|
69
|
-
|
63
|
+
GraphQLServer.new(schema: schema)
|
70
64
|
```
|
71
65
|
|
72
66
|
See the examples folder for more details
|
73
67
|
|
74
|
-
## Middleware
|
75
|
-
|
76
|
-
When using as a middleware, you can specify the path to mount the graphql endpoint (defaults to `/`)
|
68
|
+
## Rack app Middleware
|
77
69
|
|
78
70
|
```ruby
|
79
|
-
|
71
|
+
map '/graphql'
|
72
|
+
use GraphQLServer, type_def: type_def, resolver: resolver
|
73
|
+
end
|
80
74
|
```
|
data/lib/graphql_server.rb
CHANGED
@@ -2,25 +2,23 @@ require 'graphql'
|
|
2
2
|
require 'json'
|
3
3
|
require 'rack'
|
4
4
|
|
5
|
-
class
|
6
|
-
class
|
5
|
+
class GraphQLServer
|
6
|
+
class InvalidRequestType < Exception; end;
|
7
7
|
class PostBodyMissing < Exception; end;
|
8
8
|
|
9
|
-
# Initilizes
|
9
|
+
# Initilizes GraphQLServer as a Rack app or middleware
|
10
10
|
#
|
11
11
|
# This Rack middleware (or app) implements a spec-compliant GraphQL server which can be queried from any GraphQL client.
|
12
12
|
# It can be used with a provided GraphQL schema or can build one from a type definition and a resolver hash.
|
13
13
|
#
|
14
14
|
# @param [Array<Object>] *args The first argument should be `app` when used as a middleware
|
15
|
-
# @param String path Also for middleware, we will use path to determine when to process GraphQL, defaults to '/'
|
16
15
|
# @param String type_def A schema definition string, or a path to a file containing the definition
|
17
16
|
# @param Hash resolver A hash with callables for handling field resolution
|
18
17
|
# @param GraphQL::Schema schema Use this schema if `type_def` and `resolver` is nil
|
19
18
|
# @param Hash context
|
20
|
-
def initialize(*args
|
19
|
+
def initialize(*args, type_def: nil, resolver: nil, schema: nil, context: nil)
|
21
20
|
@app = args && args[0]
|
22
21
|
@context = context
|
23
|
-
@path = (@app && !path) ? '/' : path
|
24
22
|
@schema = type_def && resolver ? GraphQL::Schema.from_definition(type_def, default_resolve: resolver) : schema
|
25
23
|
end
|
26
24
|
|
@@ -30,19 +28,16 @@ class GraphqlServer
|
|
30
28
|
|
31
29
|
def call(env)
|
32
30
|
request = Rack::Request.new(env)
|
33
|
-
|
34
|
-
# only resolve when url matches `path` if we're in a middleware
|
35
|
-
return @app.call(env) if middleware? && @path != request.path_info
|
36
31
|
|
37
32
|
# graphql accepts GET and POST requests
|
38
|
-
raise
|
33
|
+
raise InvalidRequestType unless request.get? || request.post?
|
39
34
|
|
40
35
|
payload = if request.get?
|
41
36
|
request.params
|
42
37
|
elsif request.post?
|
43
38
|
body = request.body.read
|
44
39
|
raise PostBodyMissing if body.empty?
|
45
|
-
|
40
|
+
JSON.parse(body)
|
46
41
|
end
|
47
42
|
|
48
43
|
response = @schema.execute(
|
@@ -53,7 +48,7 @@ class GraphqlServer
|
|
53
48
|
).to_json
|
54
49
|
|
55
50
|
[200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize.to_s}, [response]]
|
56
|
-
rescue
|
51
|
+
rescue InvalidRequestType
|
57
52
|
# Method Not Allowed
|
58
53
|
[405, {"Content-Type" => "text/html"}, ["GraphQL Server supports only GET/POST requests"]]
|
59
54
|
rescue PostBodyMissing
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql_server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- betaflag
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|