graphdoc-ruby 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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.rubocop.yml +76 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +202 -0
- data/Rakefile +8 -0
- data/config.ru +17 -0
- data/graphdoc-ruby.gemspec +33 -0
- data/lib/graphdoc-ruby.rb +3 -0
- data/lib/graphdoc-ruby/application.rb +91 -0
- data/lib/graphdoc-ruby/config.rb +104 -0
- data/lib/graphdoc-ruby/graphdoc.rb +58 -0
- data/lib/graphdoc-ruby/graphql_json.rb +38 -0
- data/lib/graphdoc-ruby/railtie.rb +11 -0
- data/lib/graphdoc-ruby/rake_task.rb +41 -0
- data/lib/graphdoc-ruby/static.rb +37 -0
- data/lib/graphdoc-ruby/utils.rb +16 -0
- data/lib/graphdoc-ruby/version.rb +5 -0
- data/lib/graphdoc_ruby.rb +22 -0
- data/package.json +7 -0
- data/yarn.lock +455 -0
- metadata +165 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c92f93cca1793852b9ac3572e5e4ef2f1eedadc0
|
4
|
+
data.tar.gz: 5e986a910e76dbdf1674c20876a502ded8a59d80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1711959ac52564a030985dd6c03ed1d8780edeea6e3ca2b40254879b8d2a7f38ac0121305c3cb0a8034c31236f660b155bfb474bbb8bd84aae2aead16bf21b9a
|
7
|
+
data.tar.gz: fb2e8b583548377f59efe1ef5ccf78e3b7a7fcd2e55fbcb06992ecf913ba66740d49c7973fd14c81202b8274dc86276bbc27f9d5c59a2f93a39a8f50c442c41a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'bin/*'
|
4
|
+
- 'vendor/**/*'
|
5
|
+
- '**/Rakefile'
|
6
|
+
- '**/config.ru'
|
7
|
+
- 'node_modules/**/*'
|
8
|
+
TargetRubyVersion: 2.5
|
9
|
+
DisplayCopNames: true
|
10
|
+
|
11
|
+
Performance:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Bundler:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Naming:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/BlockNesting:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/ClassLength:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Metrics/LineLength:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Metrics/MethodLength:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Metrics/BlockLength:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Metrics/ModuleLength:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/AsciiComments:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/BlockDelimiters:
|
42
|
+
Exclude:
|
43
|
+
- 'spec/**/*'
|
44
|
+
|
45
|
+
Style/Documentation:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/BlockDelimiters:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/DoubleNegation:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Style/GuardClause:
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
Style/ClassAndModuleChildren:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Style/SpecialGlobalVars:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Style/NumericPredicate:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Style/Lambda:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
Layout/AlignParameters:
|
70
|
+
EnforcedStyle: with_fixed_indentation
|
71
|
+
|
72
|
+
Layout/MultilineMethodCallIndentation:
|
73
|
+
EnforcedStyle: indented
|
74
|
+
|
75
|
+
Lint/AmbiguousRegexpLiteral:
|
76
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 alpaca-tc
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
# Graphdoc::Ruby
|
2
|
+
|
3
|
+
Mountable [graphdoc](https://github.com/2fd/graphdoc) based on rack.
|
4
|
+
graphdoc is static page generator for documenting GraphQL Schema.
|
5
|
+
|
6
|
+
<img width="1169" alt="screen" src="https://user-images.githubusercontent.com/1688137/34389782-11c80450-eb7f-11e7-8b83-fdfbcbfa711e.png">
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'graphdoc-ruby'
|
14
|
+
```
|
15
|
+
|
16
|
+
Install graphdoc to your machine:
|
17
|
+
|
18
|
+
```sh
|
19
|
+
$ npm install -g @2fd/graphdoc
|
20
|
+
|
21
|
+
OR
|
22
|
+
|
23
|
+
$ yarn add @2fd/graphdoc
|
24
|
+
```
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### In pure rack application
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# config.ru
|
32
|
+
|
33
|
+
require 'graphdoc_ruby'
|
34
|
+
|
35
|
+
GraphdocRuby.configure do |config|
|
36
|
+
# endpoint of GraphQL
|
37
|
+
config.endpoint = 'https://graphql-pokemon.now.sh/'
|
38
|
+
end
|
39
|
+
|
40
|
+
run(GraphdocRuby::Application)
|
41
|
+
```
|
42
|
+
|
43
|
+
```sh
|
44
|
+
$ gem install rack
|
45
|
+
$ bundle exec rackup
|
46
|
+
```
|
47
|
+
|
48
|
+
### In rails application
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# config/routes.rb
|
52
|
+
Rails.application.routes.draw do
|
53
|
+
mount GraphdocRuby::Application, at: 'graphdoc'
|
54
|
+
end
|
55
|
+
|
56
|
+
# config/initializers/graphdoc.rb
|
57
|
+
GraphdocRuby.configure do |config|
|
58
|
+
config.endpoint = 'https://graphql-pokemon.now.sh/'
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
```sh
|
63
|
+
$ bundle exec rails server --port 3000
|
64
|
+
$ open http://0.0.0.0:3000/graphdoc
|
65
|
+
```
|
66
|
+
|
67
|
+
## Configuration
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
GraphdocRuby.configure do |config|
|
71
|
+
# :endpoint
|
72
|
+
#
|
73
|
+
# Required: <String>
|
74
|
+
# GraphQL endpoint url or dumped schema.json path.
|
75
|
+
#
|
76
|
+
# Example
|
77
|
+
# config.endpoint = 'https://your-application.com/graphql'
|
78
|
+
# config.endpoint = Rails.root.join('tmp', 'graphql', 'schema.json')
|
79
|
+
|
80
|
+
# :executable_path
|
81
|
+
#
|
82
|
+
# Optional: <String>
|
83
|
+
# Executable path of `graphdoc`.
|
84
|
+
# (default: `Bundler.which('graphdoc')`)
|
85
|
+
#
|
86
|
+
# Example
|
87
|
+
# config.executable_path = Rails.root.join('node_modules', '.bin', 'graphdoc')
|
88
|
+
|
89
|
+
# :output_directory
|
90
|
+
#
|
91
|
+
# Optional: <String>
|
92
|
+
# Output path for `graphdoc`. If you disabled run_time_generation, this value must be customized.
|
93
|
+
# NOTE: Do not assign private directory because output_directory folders are served via rack application.
|
94
|
+
# (default: `File.join(Dir.mktmpdir, 'graphdoc')`)
|
95
|
+
#
|
96
|
+
# Example
|
97
|
+
# config.output_directory = Rails.root.join('tmp', 'graphdoc')
|
98
|
+
|
99
|
+
# :overwrite
|
100
|
+
# Optional: <Boolean>
|
101
|
+
# Overwrite files if generated html already exist.
|
102
|
+
# (default: true)
|
103
|
+
#
|
104
|
+
# Example
|
105
|
+
# config.overwrite = false
|
106
|
+
|
107
|
+
# :run_time_generation
|
108
|
+
# Optional: <Boolean>
|
109
|
+
# Generate html with graphdoc on the first access.
|
110
|
+
# (default: true)
|
111
|
+
#
|
112
|
+
# Example
|
113
|
+
# config.run_time_generation = Rails.env.development?
|
114
|
+
|
115
|
+
# :graphql_context
|
116
|
+
#
|
117
|
+
# Optional: <Proc>
|
118
|
+
# Context of your graphql.
|
119
|
+
# (default: -> {})
|
120
|
+
# config.graphql_context = -> { { 'Authorization' => "Token #{ENV['GITHUB_ACCESS_TOKEN']}", 'User-Agent' => 'graphdoc-client' } }
|
121
|
+
|
122
|
+
# :graphql_query
|
123
|
+
#
|
124
|
+
# Optional: <Proc>
|
125
|
+
# Query of your graphql.
|
126
|
+
# (default: -> {})
|
127
|
+
# config.graphql_query = -> { { 'token' => ENV['SECRET_API_TOKEN'] } }
|
128
|
+
|
129
|
+
# ===
|
130
|
+
# Integrated with [graphql-ruby](https://github.com/rmosolgo/graphql-ruby)
|
131
|
+
# ===
|
132
|
+
#
|
133
|
+
# :schema_name
|
134
|
+
#
|
135
|
+
# Optional: <String>
|
136
|
+
# Schema name of your graphql-ruby. It is necessary when generating schema.json.
|
137
|
+
# (default: nil)
|
138
|
+
#
|
139
|
+
# Example
|
140
|
+
# config.schema_name = 'MyApplicationSchema'
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
### Example Configuration
|
145
|
+
|
146
|
+
#### Github
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
GraphdocRuby.configure do |config|
|
150
|
+
# Github
|
151
|
+
config.endpoint = 'https://api.github.com/graphql'
|
152
|
+
|
153
|
+
config.graphql_context = -> {
|
154
|
+
{
|
155
|
+
'Authorization' => "bearer #{ENV['GITHUB_ACCESS_TOKEN']}",
|
156
|
+
'User-Agent' => 'my-client',
|
157
|
+
}
|
158
|
+
}
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
#### Your Rails product
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
# config/routes.rb
|
166
|
+
Rails.application.routes.draw do
|
167
|
+
namespace :admin do
|
168
|
+
mount GraphdocRuby::Application, at: 'graphdoc'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# config/initializers/graphdoc.rb
|
173
|
+
GraphdocRuby.configure do |config|
|
174
|
+
config.endpoint = Rails.root.join('tmp', 'graphql', 'schema.json')
|
175
|
+
config.output_directory = Rails.root.join('tmp', 'graphdoc').to_s
|
176
|
+
config.schema_name = 'MyApplicationSchema'
|
177
|
+
config.run_time_generation = Rails.env.development?
|
178
|
+
end
|
179
|
+
|
180
|
+
# Capfile
|
181
|
+
namespace :deploy do
|
182
|
+
after :generate_graphdoc do
|
183
|
+
within release_path do
|
184
|
+
# Generate schema.json from MyApplicationSchema
|
185
|
+
execute :rake, 'graphdoc:dump_schema'
|
186
|
+
|
187
|
+
# Generate html with graphdoc from schema.json
|
188
|
+
execute :rake, 'graphdoc:generate'
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
after :publishing, :generate_graphdoc
|
193
|
+
end
|
194
|
+
```
|
195
|
+
|
196
|
+
## Contributing
|
197
|
+
|
198
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/graphdoc-ruby.
|
199
|
+
|
200
|
+
## License
|
201
|
+
|
202
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'graphdoc_ruby'
|
2
|
+
|
3
|
+
GraphdocRuby.configure do |config|
|
4
|
+
# Pokemon
|
5
|
+
config.endpoint = 'https://graphql-pokemon.now.sh/'
|
6
|
+
|
7
|
+
# Github
|
8
|
+
# config.endpoint = 'https://api.github.com/graphql'
|
9
|
+
# config.graphql_context = -> {
|
10
|
+
# {
|
11
|
+
# 'Authorization' => "bearer XXXXXXX",
|
12
|
+
# 'User-Agent' => 'my-client',
|
13
|
+
# }
|
14
|
+
# }
|
15
|
+
end
|
16
|
+
|
17
|
+
run(GraphdocRuby::Application)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'graphdoc-ruby/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'graphdoc-ruby'
|
10
|
+
spec.version = GraphdocRuby::VERSION
|
11
|
+
spec.authors = ['alpaca-tc']
|
12
|
+
spec.email = ['alpaca-tc@alpaca.tc']
|
13
|
+
|
14
|
+
spec.summary = 'Static page rack application for documenting GraphQL Schema'
|
15
|
+
spec.description = 'Static page rack application for documenting GraphQL Schema'
|
16
|
+
spec.homepage = 'https://github.com/alpaca-tc/graphdoc-ruby'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_dependency 'activesupport', '>= 4.2.0'
|
26
|
+
spec.add_dependency 'bundler'
|
27
|
+
spec.add_dependency 'rack'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
spec.add_development_dependency 'rake'
|
31
|
+
spec.add_development_dependency 'rspec'
|
32
|
+
spec.add_development_dependency 'rubocop'
|
33
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack/response'
|
4
|
+
|
5
|
+
module GraphdocRuby
|
6
|
+
class Application
|
7
|
+
Semaphore = Mutex.new
|
8
|
+
|
9
|
+
def self.call(env)
|
10
|
+
@application ||= new
|
11
|
+
@application.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.graphdoc
|
15
|
+
config = GraphdocRuby.config
|
16
|
+
config.assert_configuration!
|
17
|
+
|
18
|
+
GraphdocRuby::Graphdoc.new(
|
19
|
+
output: config.output_directory,
|
20
|
+
executable: config.executable_path,
|
21
|
+
endpoint: config.endpoint,
|
22
|
+
overwrite: config.overwrite,
|
23
|
+
mtime: config.mtime,
|
24
|
+
query: config.evaluate_graphql_query,
|
25
|
+
context: config.evaluate_graphql_context
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
generate_html if GraphdocRuby.config.run_time_generation
|
31
|
+
|
32
|
+
@static = GraphdocRuby::Static.new(GraphdocRuby.config.output_directory)
|
33
|
+
end
|
34
|
+
|
35
|
+
def call(env)
|
36
|
+
serve_static_file(env) || not_found
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def generate_html
|
42
|
+
Semaphore.synchronize do
|
43
|
+
if should_generate_schema_json?
|
44
|
+
GraphdocRuby::GraphqlJson.write_schema_json
|
45
|
+
end
|
46
|
+
|
47
|
+
self.class.graphdoc.generate_document!
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def should_generate_schema_json?
|
52
|
+
!GraphdocRuby::Utils.valid_url?(GraphdocRuby.config.endpoint) && !GraphdocRuby::Utils.file_exist?(GraphdocRuby.config.endpoint)
|
53
|
+
end
|
54
|
+
|
55
|
+
def not_found
|
56
|
+
if GraphdocRuby.config.run_time_generation
|
57
|
+
[404, { 'Content-Type' => 'text/html' }, ['Not found generated html']]
|
58
|
+
else
|
59
|
+
[404, { 'Content-Type' => 'text/html' }, ['Not found generated html. Please run `rake graphdoc:generate`']]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def serve_static_file(env)
|
64
|
+
request = Rack::Request.new(env)
|
65
|
+
return unless request.get? || request.head?
|
66
|
+
return redirect_to_slash_path(env) if html_access_without_slash?(env)
|
67
|
+
|
68
|
+
path = request.path_info.chomp('/')
|
69
|
+
match = @static.match?(path)
|
70
|
+
|
71
|
+
if match
|
72
|
+
request.path_info = match
|
73
|
+
@static.serve(request)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def html_access_without_slash?(env)
|
78
|
+
original_path = env[Rack::REQUEST_PATH]
|
79
|
+
File.extname(original_path).empty? && !original_path.end_with?('/')
|
80
|
+
end
|
81
|
+
|
82
|
+
# Unfortunately, html generated by graphdoc contains relative path.
|
83
|
+
def redirect_to_slash_path(env)
|
84
|
+
path = env[Rack::REQUEST_PATH] + '/'
|
85
|
+
|
86
|
+
response = Rack::Response.new
|
87
|
+
response.redirect(path)
|
88
|
+
response.finish
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|