Skip to content

Commit dcd6293

Browse files
authored
Add graphql-docs:generate Rake task (#176)
This adds an included Rake task for easy use and calling from other Rake tasks to follow pretty normal Ruby standards.
1 parent 94995a1 commit dcd6293

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ A concise overview of the public-facing changes to the gem from version to versi
44

55
## Unreleased
66

7+
- Add Rake task `graphql-docs:generate` for integration with Rails and other Ruby projects that use Rake. Supports both task arguments and environment variables for configuration. Can be used with `Rake::Task["assets:precompile"].enhance(["graphql-docs:generate"])` to generate docs as part of the build process.
8+
79
## v5.2.0 - 2025-02-09
810

911
- Add search filter to sidebar. Thanks @denisahearn!

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,80 @@ See all of the supported CLI options with:
6262
graphql-docs -h
6363
```
6464

65+
### Rake Task
66+
67+
GraphQLDocs includes a Rake task for integration with Rails applications and other Ruby projects that use Rake. This allows you to generate documentation as part of your build process or hook it into other Rake tasks.
68+
69+
#### Basic Usage
70+
71+
Using task arguments (recommended):
72+
73+
```console
74+
rake graphql-docs:generate[schema.graphql]
75+
rake graphql-docs:generate[schema.graphql,./docs]
76+
rake graphql-docs:generate[schema.graphql,./docs,/api-docs,true]
77+
```
78+
79+
Or using environment variables:
80+
81+
```console
82+
GRAPHQL_SCHEMA_FILE=schema.graphql rake graphql-docs:generate
83+
```
84+
85+
#### Available Arguments
86+
87+
Arguments are passed in order: `[schema_file, output_dir, base_url, delete_output]`
88+
89+
1. `schema_file` - Path to GraphQL schema file (required)
90+
2. `output_dir` - Output directory (default: `./output/`)
91+
3. `base_url` - Base URL for assets and links (default: `''`)
92+
4. `delete_output` - Delete output directory before generating (`true`/`false`, default: `false`)
93+
94+
#### Available Environment Variables
95+
96+
- `GRAPHQL_SCHEMA_FILE` - Path to GraphQL schema file (required)
97+
- `GRAPHQL_OUTPUT_DIR` - Output directory (default: `./output/`)
98+
- `GRAPHQL_BASE_URL` - Base URL for assets and links (default: `''`)
99+
- `GRAPHQL_DELETE_OUTPUT` - Delete output directory before generating (`true`/`false`)
100+
101+
Note: Task arguments take precedence over environment variables.
102+
103+
#### Example: Generate Docs Before Asset Compilation
104+
105+
In Rails, you can automatically generate documentation before compiling assets:
106+
107+
```ruby
108+
# Rakefile or lib/tasks/docs.rake
109+
Rake::Task["assets:precompile"].enhance(["graphql-docs:generate"])
110+
```
111+
112+
Then configure using environment variables:
113+
114+
```ruby
115+
# config/application.rb or .env
116+
ENV["GRAPHQL_SCHEMA_FILE"] = Rails.root.join("app/graphql/schema.graphql").to_s
117+
```
118+
119+
Or invoke with arguments in your custom task:
120+
121+
```ruby
122+
# lib/tasks/custom_docs.rake
123+
namespace :docs do
124+
desc "Generate API documentation"
125+
task :generate do
126+
Rake::Task["graphql-docs:generate"].invoke(
127+
"app/graphql/schema.graphql", # schema_file
128+
"public/api-docs", # output_dir
129+
"/api-docs", # base_url
130+
"true" # delete_output
131+
)
132+
end
133+
end
134+
135+
# Hook into asset compilation
136+
Rake::Task["assets:precompile"].enhance(["docs:generate"])
137+
```
138+
65139
### Rack Application (Dynamic)
66140

67141
For more flexibility and control, you can serve documentation dynamically using the Rack application. This is useful for:

Rakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ require "rake/testtask"
55

66
require "standard/rake"
77

8+
# Load graphql-docs rake tasks
9+
Dir.glob("lib/tasks/**/*.rake").each { |r| load r }
10+
811
Rake::TestTask.new(:test) do |t|
912
t.libs << "test"
1013
t.libs << "lib"

lib/tasks/graphql-docs.rake

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
begin
4+
require "graphql-docs"
5+
6+
namespace :"graphql-docs" do
7+
desc "Generate GraphQL documentation from schema"
8+
task :generate, [:schema_file, :output_dir, :base_url, :delete_output] do |_t, args|
9+
options = {}
10+
11+
# Prefer task arguments over environment variables
12+
options[:filename] = args[:schema_file] || ENV["GRAPHQL_SCHEMA_FILE"]
13+
options[:output_dir] = args[:output_dir] || ENV["GRAPHQL_OUTPUT_DIR"]
14+
options[:base_url] = args[:base_url] || ENV["GRAPHQL_BASE_URL"]
15+
16+
# Handle delete_output as a boolean
17+
delete_output_arg = args[:delete_output] || ENV["GRAPHQL_DELETE_OUTPUT"]
18+
options[:delete_output] = delete_output_arg == "true" if delete_output_arg
19+
20+
# Check if a schema file is specified
21+
if options[:filename].nil?
22+
puts "Please specify a GraphQL schema file:"
23+
puts ""
24+
puts "Using task arguments:"
25+
puts " rake graphql-docs:generate[path/to/schema.graphql]"
26+
puts " rake graphql-docs:generate[schema.graphql,./docs]"
27+
puts " rake graphql-docs:generate[schema.graphql,./docs,/api-docs,true]"
28+
puts ""
29+
puts "Or using environment variables:"
30+
puts " GRAPHQL_SCHEMA_FILE=path/to/schema.graphql rake graphql-docs:generate"
31+
puts ""
32+
puts "Available arguments (in order):"
33+
puts " 1. schema_file - Path to GraphQL schema file (required)"
34+
puts " 2. output_dir - Output directory (default: ./output/)"
35+
puts " 3. base_url - Base URL for assets and links (default: '')"
36+
puts " 4. delete_output - Delete output directory before generating (true/false, default: false)"
37+
puts ""
38+
puts "Available environment variables:"
39+
puts " GRAPHQL_SCHEMA_FILE - Path to GraphQL schema file (required)"
40+
puts " GRAPHQL_OUTPUT_DIR - Output directory (default: ./output/)"
41+
puts " GRAPHQL_BASE_URL - Base URL for assets and links (default: '')"
42+
puts " GRAPHQL_DELETE_OUTPUT - Delete output directory before generating (true/false)"
43+
exit 1
44+
end
45+
46+
puts "Generating GraphQL documentation..."
47+
puts " Schema: #{options[:filename]}"
48+
puts " Output: #{options[:output_dir] || "./output/"}"
49+
50+
GraphQLDocs.build(options)
51+
52+
puts "Documentation generated successfully!"
53+
end
54+
end
55+
rescue LoadError
56+
# graphql-docs not available, skip task definition
57+
end

test/rake_task_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "rake"
5+
6+
class RakeTaskTest < Minitest::Test
7+
def setup
8+
# Clear any existing tasks to ensure clean state
9+
Rake::Task.clear if Rake::Task.respond_to?(:clear)
10+
11+
@rake = Rake::Application.new
12+
Rake.application = @rake
13+
14+
# Load the graphql-docs library and the rake task
15+
require "graphql-docs"
16+
load File.expand_path("../lib/tasks/graphql-docs.rake", __dir__)
17+
end
18+
19+
def teardown
20+
# Clean up the task
21+
Rake::Task["graphql-docs:generate"].clear if Rake::Task.task_defined?("graphql-docs:generate")
22+
Rake.application = nil
23+
end
24+
25+
def test_rake_task_is_defined
26+
assert Rake::Task.task_defined?("graphql-docs:generate"), "graphql-docs:generate task should be defined"
27+
end
28+
29+
def test_rake_task_name_is_correct
30+
task = Rake::Task["graphql-docs:generate"]
31+
assert_equal "graphql-docs:generate", task.name
32+
end
33+
34+
def test_rake_task_exits_without_schema_file
35+
task = Rake::Task["graphql-docs:generate"]
36+
37+
# Capture output and exit
38+
out, _err = capture_io do
39+
assert_raises(SystemExit) do
40+
task.invoke
41+
end
42+
end
43+
44+
assert_match(/Please specify a GraphQL schema file/, out)
45+
assert_match(/Using task arguments/, out)
46+
assert_match(/Or using environment variables/, out)
47+
end
48+
49+
def test_rake_task_can_be_enhanced
50+
# This test verifies that the task can be used in task dependencies
51+
# which is the main use case mentioned in the requirements
52+
task = Rake::Task["graphql-docs:generate"]
53+
assert_respond_to task, :enhance
54+
end
55+
56+
def test_rake_task_accepts_arguments
57+
# Verify that the task accepts the expected arguments
58+
task = Rake::Task["graphql-docs:generate"]
59+
assert task.arg_names.include?(:schema_file), "Task should accept schema_file argument"
60+
assert task.arg_names.include?(:output_dir), "Task should accept output_dir argument"
61+
assert task.arg_names.include?(:base_url), "Task should accept base_url argument"
62+
assert task.arg_names.include?(:delete_output), "Task should accept delete_output argument"
63+
end
64+
end

0 commit comments

Comments
 (0)