Skip to content

Commit 946df14

Browse files
authored
Merge pull request #169 from brettchalupa/docs
Document Ruby code
2 parents cb3aa7c + 6568780 commit 946df14

File tree

11 files changed

+396
-2
lines changed

11 files changed

+396
-2
lines changed

.yardopts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--markup markdown
2+
--markup-provider commonmarker
3+
--output-dir doc
4+
--protected
5+
--no-private
6+
lib/**/*.rb
7+
-
8+
README.md
9+
CHANGELOG.md

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ To customize the colors, create a custom CSS file and load it after the default
198198
Then reference it in your custom template by overriding the `default` template and adding a link to your stylesheet:
199199

200200
```html
201-
<link rel="stylesheet" href="<%= base_url %>/assets/style.css">
202-
<link rel="stylesheet" href="<%= base_url %>/assets/custom-theme.css">
201+
<link rel="stylesheet" href="<%= base_url %>/assets/style.css" />
202+
<link rel="stylesheet" href="<%= base_url %>/assets/custom-theme.css" />
203203
```
204204

205205
## Configuration
@@ -291,6 +291,40 @@ painless as possible.
291291
Upcoming work for the project is organized publicly via [GitHub
292292
Projects](https://github.com/users/brettchalupa/projects/7/views/1).
293293

294+
## API Documentation
295+
296+
This gem uses [YARD](https://yardoc.org/) for API documentation. All public classes and methods are documented with examples and detailed descriptions.
297+
298+
### Viewing the API Documentation
299+
300+
To generate and view the API documentation locally:
301+
302+
```console
303+
bundle exec rake yard
304+
```
305+
306+
This will generate HTML documentation in the `doc/` directory. Open `doc/index.html` in your browser to view it.
307+
308+
Alternatively, you can run a live documentation server with auto-reload:
309+
310+
```console
311+
bundle exec rake yard:server
312+
```
313+
314+
This starts a server at http://localhost:8808 that automatically reloads when you make changes to the code.
315+
316+
### Online Documentation
317+
318+
The API documentation is available online at [RubyDoc.info](https://www.rubydoc.info/gems/graphql-docs).
319+
320+
### Documentation Coverage
321+
322+
The codebase maintains 100% documentation coverage for all public APIs. You can check documentation statistics with:
323+
324+
```console
325+
bundle exec yard stats
326+
```
327+
294328
## Development
295329

296330
After checking out the repo, run `bin/setup` to install dependencies. Then, run

Rakefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ task :console do
4040
IRB.start
4141
end
4242

43+
namespace :yard do
44+
desc 'Generate YARD documentation'
45+
task :doc do
46+
sh 'bundle exec yard doc'
47+
end
48+
49+
desc 'Run YARD documentation server'
50+
task :server do
51+
puts "Starting YARD server at http://localhost:8808"
52+
puts "Press Ctrl+C to stop"
53+
sh 'bundle exec yard server --reload --bind 0.0.0.0 --port 8808'
54+
end
55+
end
56+
57+
# Alias for convenience
58+
desc 'Generate YARD documentation'
59+
task yard: 'yard:doc'
60+
4361
namespace :sample do
4462
desc 'Generate the sample documentation'
4563
task :generate do

graphql-docs.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ Gem::Specification.new do |spec|
5555
spec.add_development_dependency 'rubocop-performance', '~> 1.15'
5656
spec.add_development_dependency 'webmock', '~> 2.3'
5757
spec.add_development_dependency 'webrick', '~> 1.7'
58+
spec.add_development_dependency 'yard', '~> 0.9'
5859
end

lib/graphql-docs.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,69 @@
77
require 'graphql-docs/parser'
88
require 'graphql-docs/version'
99

10+
# GraphQLDocs is a library for generating beautiful HTML documentation from GraphQL schemas.
11+
# It parses GraphQL schema files or schema objects and generates a complete documentation website
12+
# with customizable templates and styling.
13+
#
14+
# @example Generate docs from a file
15+
# GraphQLDocs.build(filename: 'schema.graphql')
16+
#
17+
# @example Generate docs from a schema string
18+
# GraphQLDocs.build(schema: schema_string)
19+
#
20+
# @example Generate docs from a schema class
21+
# schema = GraphQL::Schema.define { query query_type }
22+
# GraphQLDocs.build(schema: schema)
23+
#
24+
# @see Configuration For available configuration options
1025
module GraphQLDocs
1126
class << self
27+
# Builds HTML documentation from a GraphQL schema.
28+
#
29+
# This is the main entry point for generating documentation. It accepts either a schema file path
30+
# or a schema string/object, parses it, and generates a complete HTML documentation website.
31+
#
32+
# @param options [Hash] Configuration options for generating the documentation
33+
# @option options [String] :filename Path to GraphQL schema IDL file
34+
# @option options [String, GraphQL::Schema] :schema GraphQL schema as string or schema class
35+
# @option options [String] :output_dir ('./output/') Directory where HTML will be generated
36+
# @option options [Boolean] :use_default_styles (true) Whether to include default CSS styles
37+
# @option options [String] :base_url ('') Base URL to prepend for assets and links
38+
# @option options [Boolean] :delete_output (false) Delete output directory before generating
39+
# @option options [Hash] :pipeline_config Configuration for html-pipeline rendering
40+
# @option options [Class] :renderer (GraphQLDocs::Renderer) Custom renderer class
41+
# @option options [Hash] :templates Custom template file paths
42+
# @option options [Hash] :landing_pages Custom landing page file paths
43+
# @option options [Hash] :classes Additional CSS class names for elements
44+
# @option options [Proc] :notices Proc for adding notices to schema members
45+
#
46+
# @return [Boolean] Returns true on successful generation
47+
#
48+
# @raise [ArgumentError] If both filename and schema are provided, or if neither is provided
49+
# @raise [ArgumentError] If the specified filename does not exist
50+
# @raise [TypeError] If filename is not a String
51+
# @raise [TypeError] If schema is not a String or GraphQL::Schema
52+
#
53+
# @example Basic usage with file
54+
# GraphQLDocs.build(filename: 'schema.graphql')
55+
#
56+
# @example With custom options
57+
# GraphQLDocs.build(
58+
# filename: 'schema.graphql',
59+
# output_dir: './docs',
60+
# base_url: '/api-docs',
61+
# delete_output: true
62+
# )
63+
#
64+
# @example With custom renderer
65+
# class MyRenderer < GraphQLDocs::Renderer
66+
# def render(contents, type: nil, name: nil)
67+
# # Custom rendering logic
68+
# end
69+
# end
70+
# GraphQLDocs.build(filename: 'schema.graphql', renderer: MyRenderer)
71+
#
72+
# @see Configuration::GRAPHQLDOCS_DEFAULTS For all available options
1273
def build(options)
1374
# do not let user provided values overwrite every single value
1475
%i[templates landing_pages].each do |opt|

lib/graphql-docs/configuration.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
# frozen_string_literal: true
22

33
module GraphQLDocs
4+
# Configuration module that defines default options for GraphQLDocs.
5+
#
6+
# All configuration options can be overridden when calling {GraphQLDocs.build}.
7+
#
8+
# @see GraphQLDocs.build
49
module Configuration
10+
# Default configuration options for GraphQLDocs.
11+
#
12+
# @return [Hash] Hash of default configuration values
13+
#
14+
# @option defaults [String] :filename (nil) Path to GraphQL schema IDL file
15+
# @option defaults [String, GraphQL::Schema] :schema (nil) GraphQL schema as string or class
16+
# @option defaults [Boolean] :delete_output (false) Delete output directory before generating
17+
# @option defaults [String] :output_dir ('./output/') Directory for generated HTML files
18+
# @option defaults [Hash] :pipeline_config Configuration for html-pipeline rendering
19+
# @option defaults [Class] :renderer (GraphQLDocs::Renderer) Renderer class to use
20+
# @option defaults [Boolean] :use_default_styles (true) Include default CSS styles
21+
# @option defaults [String] :base_url ('') Base URL to prepend to assets and links
22+
# @option defaults [Hash] :templates Paths to ERB template files for different GraphQL types
23+
# @option defaults [Hash] :landing_pages Paths to landing page files for each type
24+
# @option defaults [Hash] :classes Additional CSS class names for styling elements
525
GRAPHQLDOCS_DEFAULTS = {
626
# initialize
727
filename: nil,

lib/graphql-docs/generator.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,37 @@
66
require 'ostruct'
77

88
module GraphQLDocs
9+
# Generates HTML documentation files from a parsed GraphQL schema.
10+
#
11+
# The Generator takes the parsed schema structure and creates individual HTML pages
12+
# for each GraphQL type, along with landing pages and static assets. It uses ERB
13+
# templates for layout and the Renderer for converting content to HTML.
14+
#
15+
# @example Basic usage
16+
# generator = GraphQLDocs::Generator.new(parsed_schema, options)
17+
# generator.generate
18+
#
19+
# @see Parser
20+
# @see Renderer
921
class Generator
1022
include Helpers
1123

24+
# @!attribute [rw] parsed_schema
25+
# @return [Hash] The parsed schema structure from {Parser}
1226
attr_accessor :parsed_schema
1327

28+
# Initializes a new Generator instance.
29+
#
30+
# Sets up ERB templates for all GraphQL types and landing pages. Validates that
31+
# all required template files exist before generation begins.
32+
#
33+
# @param parsed_schema [Hash] The parsed schema from {Parser#parse}
34+
# @param options [Hash] Configuration options
35+
# @option options [Hash] :templates Paths to ERB template files
36+
# @option options [Hash] :landing_pages Paths to landing page files
37+
# @option options [Class] :renderer Renderer class to use
38+
#
39+
# @raise [IOError] If any required template or landing page file is not found
1440
def initialize(parsed_schema, options)
1541
@parsed_schema = parsed_schema
1642
@options = options
@@ -49,6 +75,17 @@ def initialize(parsed_schema, options)
4975
end
5076
end
5177

78+
# Generates all HTML documentation files.
79+
#
80+
# This is the main generation method that creates:
81+
# - Individual pages for each GraphQL type
82+
# - Landing pages for type categories
83+
# - Static assets (CSS, fonts, images) if using default styles
84+
#
85+
# @return [Boolean] Returns true on successful generation
86+
#
87+
# @example
88+
# generator.generate # Creates all docs in output directory
5289
def generate
5390
FileUtils.rm_rf(@options[:output_dir]) if @options[:delete_output]
5491

@@ -97,6 +134,10 @@ def generate
97134
true
98135
end
99136

137+
# Creates HTML pages for GraphQL operation types (Query, Mutation).
138+
#
139+
# @return [Boolean] Returns true if Query type was found and generated
140+
# @api private
100141
def create_graphql_operation_pages
101142
graphql_operation_types.each do |query_type|
102143
metadata = ''
@@ -121,6 +162,9 @@ def create_graphql_operation_pages
121162
false
122163
end
123164

165+
# Creates HTML pages for GraphQL object types.
166+
# @return [void]
167+
# @api private
124168
def create_graphql_object_pages
125169
graphql_object_types.each do |object_type|
126170
opts = default_generator_options(type: object_type)
@@ -130,6 +174,9 @@ def create_graphql_object_pages
130174
end
131175
end
132176

177+
# Creates HTML pages for GraphQL query fields.
178+
# @return [void]
179+
# @api private
133180
def create_graphql_query_pages
134181
graphql_query_types.each do |query|
135182
opts = default_generator_options(type: query)
@@ -139,6 +186,9 @@ def create_graphql_query_pages
139186
end
140187
end
141188

189+
# Creates HTML pages for GraphQL mutation fields.
190+
# @return [void]
191+
# @api private
142192
def create_graphql_mutation_pages
143193
graphql_mutation_types.each do |mutation|
144194
opts = default_generator_options(type: mutation)
@@ -148,6 +198,9 @@ def create_graphql_mutation_pages
148198
end
149199
end
150200

201+
# Creates HTML pages for GraphQL interface types.
202+
# @return [void]
203+
# @api private
151204
def create_graphql_interface_pages
152205
graphql_interface_types.each do |interface_type|
153206
opts = default_generator_options(type: interface_type)
@@ -157,6 +210,9 @@ def create_graphql_interface_pages
157210
end
158211
end
159212

213+
# Creates HTML pages for GraphQL enum types.
214+
# @return [void]
215+
# @api private
160216
def create_graphql_enum_pages
161217
graphql_enum_types.each do |enum_type|
162218
opts = default_generator_options(type: enum_type)
@@ -166,6 +222,9 @@ def create_graphql_enum_pages
166222
end
167223
end
168224

225+
# Creates HTML pages for GraphQL union types.
226+
# @return [void]
227+
# @api private
169228
def create_graphql_union_pages
170229
graphql_union_types.each do |union_type|
171230
opts = default_generator_options(type: union_type)
@@ -175,6 +234,9 @@ def create_graphql_union_pages
175234
end
176235
end
177236

237+
# Creates HTML pages for GraphQL input object types.
238+
# @return [void]
239+
# @api private
178240
def create_graphql_input_object_pages
179241
graphql_input_object_types.each do |input_object_type|
180242
opts = default_generator_options(type: input_object_type)
@@ -184,6 +246,9 @@ def create_graphql_input_object_pages
184246
end
185247
end
186248

249+
# Creates HTML pages for GraphQL scalar types.
250+
# @return [void]
251+
# @api private
187252
def create_graphql_scalar_pages
188253
graphql_scalar_types.each do |scalar_type|
189254
opts = default_generator_options(type: scalar_type)
@@ -193,6 +258,9 @@ def create_graphql_scalar_pages
193258
end
194259
end
195260

261+
# Creates HTML pages for GraphQL directives.
262+
# @return [void]
263+
# @api private
196264
def create_graphql_directive_pages
197265
graphql_directive_types.each do |directive_type|
198266
opts = default_generator_options(type: directive_type)

0 commit comments

Comments
 (0)