You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This makes it so that users of the gem can optionally run the app as a
Rack app, making it easy to mount within a Rails or Sinatra app. Reasons
why would be to add authentication or have a nicer developer experience
instead of regenerating the docs over and over again.
Fixes#102
GraphQLDocs can be used as a Ruby library to build the documentation website. Using it as a Ruby library allows for more control and using every supported option. Here's an example:
28
35
29
36
```ruby
@@ -54,6 +61,110 @@ See all of the supported CLI options with:
54
61
graphql-docs -h
55
62
```
56
63
64
+
### Rack Application (Dynamic)
65
+
66
+
For more flexibility and control, you can serve documentation dynamically using the Rack application. This is useful for:
67
+
68
+
- Internal tools with frequently changing schemas
69
+
- Integration with existing Rails/Sinatra applications
70
+
- Adding authentication/authorization middleware
71
+
- Dynamic schema loading from databases or APIs
72
+
73
+
**Requirements**: The Rack application feature requires the `rack` gem (version 2.x or 3.x). Add it to your Gemfile:
74
+
75
+
```ruby
76
+
gem 'rack', '~> 3.0'# or '~> 2.0' for Rack 2.x
77
+
```
78
+
79
+
The gem is compatible with both Rack 2.x and 3.x, so you can use whichever version your application requires.
80
+
81
+
#### Standalone Rack App
82
+
83
+
Create a `config.ru` file:
84
+
85
+
```ruby
86
+
require'graphql-docs'
87
+
88
+
schema =File.read('schema.graphql')
89
+
90
+
app =GraphQLDocs::App.new(
91
+
schema: schema,
92
+
options: {
93
+
base_url:'',
94
+
use_default_styles:true,
95
+
cache:true# Enable page caching
96
+
}
97
+
)
98
+
99
+
run app
100
+
```
101
+
102
+
Then run with:
103
+
104
+
```console
105
+
rackup config.ru
106
+
```
107
+
108
+
Visit `http://localhost:9292` to view your docs.
109
+
110
+
#### Mounting in Rails
111
+
112
+
```ruby
113
+
# config/routes.rb
114
+
require'graphql-docs'
115
+
116
+
Rails.application.routes.draw do
117
+
mount GraphQLDocs::App.new(schema:MyGraphQLSchema) => '/docs'
118
+
end
119
+
```
120
+
121
+
#### Mounting in Sinatra
122
+
123
+
```ruby
124
+
require'sinatra'
125
+
require'graphql-docs'
126
+
127
+
schema =File.read('schema.graphql')
128
+
docs_app =GraphQLDocs::App.new(schema: schema)
129
+
130
+
map '/docs'do
131
+
run docs_app
132
+
end
133
+
134
+
map '/'do
135
+
run Sinatra::Application
136
+
end
137
+
```
138
+
139
+
#### Rack App Features
140
+
141
+
-**On-demand generation** - Pages are generated when requested
142
+
-**Built-in caching** - Generated pages are cached in memory (disable with `cache: false`)
143
+
-**Schema reloading** - Update schema without restarting server:
144
+
145
+
```ruby
146
+
app =GraphQLDocs::App.new(schema: schema)
147
+
148
+
# Later, reload with new schema
149
+
new_schema =File.read('updated_schema.graphql')
150
+
app.reload_schema!(new_schema)
151
+
```
152
+
153
+
-**Asset serving** - CSS, fonts, and images served automatically
154
+
-**Error handling** - Friendly error pages for missing types
155
+
156
+
#### SSG vs Rack Comparison
157
+
158
+
| Feature | SSG | Rack App |
159
+
|---------|-----|----------|
160
+
| Setup complexity | Low | Medium |
161
+
| First page load | Instant | Fast (with caching) |
| Authentication | Separate layer | Built-in middleware |
166
+
| Best for | Public docs, open source | Internal tools, dynamic schemas |
167
+
57
168
## Breakdown
58
169
59
170
There are several phases going on the single `GraphQLDocs.build` call:
@@ -395,20 +506,42 @@ an interactive prompt that will allow you to experiment.
395
506
396
507
## Sample Site
397
508
398
-
Clone this repository and run:
509
+
Clone this repository and try out both modes:
399
510
400
-
```
511
+
### Static Site Generation
512
+
513
+
Generate the sample documentation to the `output` directory:
514
+
515
+
```console
401
516
bin/rake sample:generate
402
517
```
403
518
404
-
to see some sample output in the `output` dir.
519
+
Then boot up a server to view the pre-generated files:
520
+
521
+
```console
522
+
bin/rake sample:serve
523
+
```
524
+
525
+
Visit `http://localhost:5050` to view the static documentation.
405
526
406
-
Boot up a server to view it:
527
+
### Rack Application (Dynamic)
407
528
529
+
Run the sample docs as a dynamic Rack application:
530
+
531
+
```console
532
+
bin/rake sample:rack
408
533
```
409
-
bin/rake sample:serve
534
+
535
+
Or use the config.ru directly:
536
+
537
+
```console
538
+
rackup config.ru
410
539
```
411
540
541
+
Visit `http://localhost:9292` to view the documentation served dynamically.
542
+
543
+
**Key Difference**: The SSG version pre-generates all pages (faster initial load, no runtime cost), while the Rack version generates pages on-demand (better for dynamic schemas, easier integration).
544
+
412
545
## Credits
413
546
414
547
Originally built by [gjtorikian](https://github.com/gjtorikian). Actively maintained by [brettchalupa](https://github.com/brettchalupa).
0 commit comments