Skip to content

Commit c1c8668

Browse files
author
betaflag
committed
Change GraphqlServer to GraphQLServer, removed path, code style
1 parent 40e1993 commit c1c8668

File tree

9 files changed

+33
-29
lines changed

9 files changed

+33
-29
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
## v0.1.0
4+
5+
- Changed `GraphqlServer` class name to `GraphQLServer` to follow the graphql gem convention
6+
- Removed `path` as an option for middleware since it can be mapped with rack
7+
- Fix a few small issues with the code

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ resolver = {
2626
}
2727
}
2828

29-
run GraphqlServer.new(type_def: type_def, resolver: resolver)
29+
run GraphQLServer.new(type_def: type_def, resolver: resolver)
3030
```
3131

3232
Start using `rackup`
@@ -44,7 +44,7 @@ require 'graphql_server'
4444
type_def = ...
4545
resolver = ...
4646

47-
use GraphqlServer, type_def: type_def, resolver: resolver, path: '/graphql'
47+
use GraphQLServer, type_def: type_def, resolver: resolver
4848
```
4949

5050
# Options
@@ -54,21 +54,21 @@ use GraphqlServer, type_def: type_def, resolver: resolver, path: '/graphql'
5454
You can get started fast by writing a type defintions and a resolver hash
5555

5656
```ruby
57-
GraphqlServer.new(type_def: type_def, resolver: resolver)
57+
GraphQLServer.new(type_def: type_def, resolver: resolver)
5858
```
5959

6060
You can also provide your own [schema](http://graphql-ruby.org/schema/definition.html)
6161

6262
```ruby
63-
GraphqlServer.new(schema: schema)
63+
GraphQLServer.new(schema: schema)
6464
```
6565

6666
See the examples folder for more details
6767

68-
## Middleware
69-
70-
When using as a middleware, you can specify the path to mount the graphql endpoint (defaults to `/`)
68+
## Rack app Middleware
7169

7270
```ruby
73-
use GraphqlServer, type_def: type_def, resolver: resolver, path: '/graphql'
71+
map '/graphql'
72+
use GraphQLServer, type_def: type_def, resolver: resolver
73+
end
7474
```

examples/blog/app.ru

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require_relative 'types/post_type'
88
require_relative 'types/query_type'
99
require_relative 'schema'
1010

11-
run GraphqlServer.new(schema: Schema)
11+
run GraphQLServer.new(schema: Schema)

examples/hello_world_app/hello_world_app.ru

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ resolver = {
1212
}
1313
}
1414

15-
run GraphqlServer.new(type_def: type_def, resolver: resolver)
15+
run GraphQLServer.new(type_def: type_def, resolver: resolver)

examples/hello_world_middleware/hello_world_middleware.ru

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ resolver = {
1212
}
1313
}
1414

15-
use GraphqlServer, type_def: type_def, resolver: resolver, path: '/graphql'
15+
map '/graphql' do
16+
use GraphQLServer, type_def: type_def, resolver: resolver
17+
end
18+
1619
run ->(env) { [200, {"Content-Type" => "text/html"}, ["Hello World!"]] }

graphql_server.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22
s.name = 'graphql_server'
3-
s.version = '0.0.1'
4-
s.date = '2019-01-18'
3+
s.version = '0.0.2'
4+
s.date = '2019-01-21'
55
s.summary = "A simple spec-compliant GraphQL rack application and middleware"
66
s.description = "A simple spec-compliant GraphQL rack application and middleware"
77
s.authors = ["betaflag"]

lib/graphql_server.rb

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
require 'json'
33
require 'rack'
44

5-
class GraphqlServer
6-
class InvalidRequestTypeError < Exception; end;
5+
class GraphQLServer
6+
class InvalidRequestType < Exception; end;
77
class PostBodyMissing < Exception; end;
88

9-
# Initilizes GraphqlServer as a Rack app or middleware
9+
# Initilizes GraphQLServer as a Rack app or middleware
1010
#
1111
# This Rack middleware (or app) implements a spec-compliant GraphQL server which can be queried from any GraphQL client.
1212
# It can be used with a provided GraphQL schema or can build one from a type definition and a resolver hash.
1313
#
1414
# @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 '/'
1615
# @param String type_def A schema definition string, or a path to a file containing the definition
1716
# @param Hash resolver A hash with callables for handling field resolution
1817
# @param GraphQL::Schema schema Use this schema if `type_def` and `resolver` is nil
1918
# @param Hash context
20-
def initialize(*args , path: nil, type_def: nil, resolver: nil, schema: nil, context: nil)
19+
def initialize(*args, type_def: nil, resolver: nil, schema: nil, context: nil)
2120
@app = args && args[0]
2221
@context = context
23-
@path = (@app && !path) ? '/' : path
2422
@schema = type_def && resolver ? GraphQL::Schema.from_definition(type_def, default_resolve: resolver) : schema
2523
end
2624

@@ -30,19 +28,16 @@ def middleware?
3028

3129
def call(env)
3230
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
3631

3732
# graphql accepts GET and POST requests
38-
raise InvalidRequestTypeError unless request.get? || request.post?
33+
raise InvalidRequestType unless request.get? || request.post?
3934

4035
payload = if request.get?
4136
request.params
4237
elsif request.post?
4338
body = request.body.read
4439
raise PostBodyMissing if body.empty?
45-
payload = JSON.parse(body)
40+
JSON.parse(body)
4641
end
4742

4843
response = @schema.execute(
@@ -53,7 +48,7 @@ def call(env)
5348
).to_json
5449

5550
[200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize.to_s}, [response]]
56-
rescue InvalidRequestTypeError
51+
rescue InvalidRequestType
5752
# Method Not Allowed
5853
[405, {"Content-Type" => "text/html"}, ["GraphQL Server supports only GET/POST requests"]]
5954
rescue PostBodyMissing

test/graphql_server_test.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
require 'graphql_server'
55
require 'rack/test'
6-
require 'graphql'
76
require 'hello_world_server'
87

9-
describe GraphqlServer do
8+
describe GraphQLServer do
109
include Rack::Test::Methods
1110

1211
describe "app" do

test/hello_world_server.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def app
1414
}
1515

1616
Rack::Builder.new do
17-
run GraphqlServer.new(type_def: type_def, resolver: resolver)
17+
run GraphQLServer.new(type_def: type_def, resolver: resolver)
1818
end
1919
end
2020

@@ -32,7 +32,7 @@ def middleware(path = nil)
3232
}
3333

3434
Rack::Builder.new do
35-
use GraphqlServer, type_def: type_def, resolver: resolver, path: path
35+
use GraphQLServer, type_def: type_def, resolver: resolver
3636
run [200]
3737
end
3838
end

0 commit comments

Comments
 (0)