graphql_server 0.0.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +80 -0
  3. data/lib/graphql_server.rb +63 -0
  4. metadata +87 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ea7e41d5079cadfd0e82b88bc6b3fc3d4f9650e4e35d6791a974d4a74f98e856
4
+ data.tar.gz: 0d7510f53f3c12e69edb84a2f7c3b7f030963d61a124abde624ff7afbd4c0305
5
+ SHA512:
6
+ metadata.gz: 6339904a26acca60b5e16fd6bff3b6da87cf61a12c224f00e9702e3b02a344183e9a6154e6fc6fbeb5b49162d06019480832b45db843221146e6fbe9b86361c6
7
+ data.tar.gz: 873be8b072f5fd36174fcdbbf9fee36a51803bec80774bf811060bf70094e536469ed6a53f68ecefc4eac4927df6c157790dd402f38d1dfe9f45c0d603bdbfce
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ This is a simple spec-compliant GraphQL rack application and middleware based on the [graphql](https://github.com/rmosolgo/graphql-ruby) gem. Since it's built with Rack, it can be mounted with most ruby web servers.
2
+
3
+ Install the gem:
4
+
5
+ ```
6
+ gem install graphql-server
7
+ ```
8
+
9
+ # Using the server
10
+
11
+ ## As a standalone application
12
+
13
+ ```ruby
14
+ # app.ru
15
+ require 'graphql_server'
16
+
17
+ type_def = <<-GRAPHQL
18
+ type Query {
19
+ hello: String
20
+ }
21
+ GRAPHQL
22
+
23
+ resolver = {
24
+ "Query" => {
25
+ "hello" => Proc.new { "world" }
26
+ }
27
+ }
28
+
29
+ run GraphqlServer.new(type_def: type_def, resolver: resolver)
30
+ ```
31
+
32
+ Start using `rackup`
33
+
34
+ ```
35
+ rackup app.ru
36
+ ```
37
+
38
+ ## As a middleware in your application
39
+
40
+ ```ruby
41
+ # app.ru
42
+ require 'graphql_server'
43
+
44
+ type_def = ...
45
+ resolver = ...
46
+
47
+ use GraphqlServer, type_def: type_def, resolver: resolver, path: '/graphql'
48
+ ```
49
+
50
+ Start using `rackup`
51
+
52
+ ```
53
+ rackup app.ru
54
+ ```
55
+
56
+ # Options
57
+
58
+ ## Schema
59
+
60
+ You can get started fast by writing a type defintions and a resolver hash
61
+
62
+ ```ruby
63
+ GraphqlServer.new(type_def: type_def, resolver: resolver)
64
+ ```
65
+
66
+ You can also provide your own schema
67
+
68
+ ```ruby
69
+ GraphqlServer.new(schema: schema)
70
+ ```
71
+
72
+ See the examples folder for more details
73
+
74
+ ## Middleware
75
+
76
+ When using as a middleware, you can specify the path to mount the graphql endpoint (defaults to `/`)
77
+
78
+ ```ruby
79
+ use GraphqlServer, type_def: type_def, resolver: resolver, path: '/graphql'
80
+ ```
@@ -0,0 +1,63 @@
1
+ require 'graphql'
2
+ require 'json'
3
+ require 'rack'
4
+
5
+ class GraphqlServer
6
+ class InvalidRequestTypeError < Exception; end;
7
+ class PostBodyMissing < Exception; end;
8
+
9
+ # Initilizes GraphqlServer as a Rack app or middleware
10
+ #
11
+ # This Rack middleware (or app) implements a spec-compliant GraphQL server which can be queried from any GraphQL client.
12
+ # It can be used with a provided GraphQL schema or can build one from a type definition and a resolver hash.
13
+ #
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
+ # @param String type_def A schema definition string, or a path to a file containing the definition
17
+ # @param Hash resolver A hash with callables for handling field resolution
18
+ # @param GraphQL::Schema schema Use this schema if `type_def` and `resolver` is nil
19
+ # @param Hash context
20
+ def initialize(*args , path: nil, type_def: nil, resolver: nil, schema: nil, context: nil)
21
+ @app = args && args[0]
22
+ @context = context
23
+ @path = (@app && !path) ? '/' : path
24
+ @schema = type_def && resolver ? GraphQL::Schema.from_definition(type_def, default_resolve: resolver) : schema
25
+ end
26
+
27
+ def middleware?
28
+ !@app.nil?
29
+ end
30
+
31
+ def call(env)
32
+ 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
+
37
+ # graphql accepts GET and POST requests
38
+ raise InvalidRequestTypeError unless request.get? || request.post?
39
+
40
+ payload = if request.get?
41
+ request.params
42
+ elsif request.post?
43
+ body = request.body.read
44
+ raise PostBodyMissing if body.empty?
45
+ payload = JSON.parse(body)
46
+ end
47
+
48
+ response = @schema.execute(
49
+ payload['query'],
50
+ variables: payload['variables'],
51
+ operation_name: payload['operationName'],
52
+ context: @context,
53
+ ).to_json
54
+
55
+ [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize.to_s}, [response]]
56
+ rescue InvalidRequestTypeError
57
+ # Method Not Allowed
58
+ [405, {"Content-Type" => "text/html"}, ["GraphQL Server supports only GET/POST requests"]]
59
+ rescue PostBodyMissing
60
+ # Bad Request
61
+ [400, {"Content-Type" => "text/html"}, ["POST body missing"]]
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - betaflag
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: graphql
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ description: A simple spec-compliant GraphQL rack application and middleware
56
+ email: hello@betaflag.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - lib/graphql_server.rb
63
+ homepage: https://github.com/betaflag/graphql-server-ruby
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.7.6
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A simple spec-compliant GraphQL rack application and middleware
87
+ test_files: []