Skip to content

Commit bd23c98

Browse files
authored
Merge pull request #72 from gjtorikian/support-varied-directives
Support directives
2 parents cc08adc + 5cd5c65 commit bd23c98

File tree

12 files changed

+112
-14
lines changed

12 files changed

+112
-14
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ In your ERB layouts, there are several helper methods you can use. The helper me
109109
* `slugify(str)` - This slugifies the given string.
110110
* `include(filename, opts)` - This embeds a template from your `includes` folder, passing along the local options provided.
111111
* `markdownify(string)` - This converts a string into HTML via CommonMarker.
112-
* `graphql_operation_types`, `graphql_mutation_types`, `graphql_object_types`, `graphql_interface_types`, `graphql_enum_types`, `graphql_union_types`, `graphql_input_object_types`, `graphql_scalar_types` - Collections of the various GraphQL types.
112+
* `graphql_operation_types`, `graphql_mutation_types`, `graphql_object_types`, `graphql_interface_types`, `graphql_enum_types`, `graphql_union_types`, `graphql_input_object_types`, `graphql_scalar_types`, `graphql_directive_types` - Collections of the various GraphQL types.
113113

114114
To call these methods within templates, you must use the dot notation, such as `<%= slugify.(text) %>`.
115115

@@ -127,8 +127,8 @@ The following options are available:
127127
| `delete_output` | Deletes `output_dir` before generating content. | `false` |
128128
| `pipeline_config` | Defines two sub-keys, `pipeline` and `context`, which are used by `html-pipeline` when rendering your output. | `pipeline` has `ExtendedMarkdownFilter`, `EmojiFilter`, and `TableOfContentsFilter`. `context` has `gfm: false` and `asset_root` set to GitHub's CDN. |
129129
| `renderer` | The rendering class to use. | `GraphQLDocs::Renderer`
130-
| `templates` | The templates to use when generating HTML. You may override any of the following keys: `default`, `includes`, `operations`, `objects`, `mutations`, `interfaces`, `enums`, `unions`, `input_objects`, `scalars`. | The defaults are found in _lib/graphql-docs/layouts/_.
131-
| `landing_pages` | The landing page to use when generating HTML for each type. You may override any of the following keys: `index`, `query`, `object`, `mutation`, `interface`, `enum`, `union`, `input_object`, `scalar`. | The defaults are found in _lib/graphql-docs/layouts/_.
130+
| `templates` | The templates to use when generating HTML. You may override any of the following keys: `default`, `includes`, `operations`, `objects`, `mutations`, `interfaces`, `enums`, `unions`, `input_objects`, `scalars`, `directives`. | The defaults are found in _lib/graphql-docs/layouts/_.
131+
| `landing_pages` | The landing page to use when generating HTML for each type. You may override any of the following keys: `index`, `query`, `object`, `mutation`, `interface`, `enum`, `union`, `input_object`, `scalar`, `directive`. | The defaults are found in _lib/graphql-docs/layouts/_.
132132
| `classes` | Additional class names you can provide to certain elements. | The full list is available in _lib/graphql-docs/configuration.rb_.
133133
| `notices` | A proc used to add notices to schema members. See *Customizing Notices* section below. | `nil` |
134134

lib/graphql-docs/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module Configuration
3737
unions: "#{File.dirname(__FILE__)}/layouts/graphql_unions.html",
3838
input_objects: "#{File.dirname(__FILE__)}/layouts/graphql_input_objects.html",
3939
scalars: "#{File.dirname(__FILE__)}/layouts/graphql_scalars.html",
40+
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html",
4041
},
4142

4243
landing_pages: {
@@ -49,6 +50,7 @@ module Configuration
4950
union: "#{File.dirname(__FILE__)}/landing_pages/union.md",
5051
input_object: "#{File.dirname(__FILE__)}/landing_pages/input_object.md",
5152
scalar: "#{File.dirname(__FILE__)}/landing_pages/scalar.md",
53+
directive: "#{File.dirname(__FILE__)}/landing_pages/directive.md",
5254

5355
variables: {} # only used for ERB landing pages
5456
},

lib/graphql-docs/generator.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def initialize(parsed_schema, options)
1414

1515
@renderer = @options[:renderer].new(@parsed_schema, @options)
1616

17-
%i(operations objects mutations interfaces enums unions input_objects scalars).each do |sym|
17+
%i(operations objects mutations interfaces enums unions input_objects scalars directives).each do |sym|
1818
if !File.exist?(@options[:templates][sym])
1919
raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found"
2020
end
2121
instance_variable_set("@graphql_#{sym}_template", ERB.new(File.read(@options[:templates][sym])))
2222
end
2323

24-
%i(index object query mutation interface enum union input_object scalar).each do |sym|
24+
%i(index object query mutation interface enum union input_object scalar directive).each do |sym|
2525
if @options[:landing_pages][sym].nil?
2626
instance_variable_set("@#{sym}_landing_page", nil)
2727
elsif !File.exist?(@options[:landing_pages][sym])
@@ -58,6 +58,7 @@ def generate
5858
create_graphql_union_pages
5959
create_graphql_input_object_pages
6060
create_graphql_scalar_pages
61+
create_graphql_directive_pages
6162

6263
unless @graphql_index_landing_page.nil?
6364
write_file('static', 'index', @graphql_index_landing_page, trim: false)
@@ -95,6 +96,10 @@ def generate
9596
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false)
9697
end
9798

99+
unless @graphql_directive_landing_page.nil?
100+
write_file('static', 'directive', @graphql_directive_landing_page, trim: false)
101+
end
102+
98103
if @options[:use_default_styles]
99104
assets_dir = File.join(File.dirname(__FILE__), 'layouts', 'assets')
100105
FileUtils.mkdir_p(File.join(@options[:output_dir], 'assets'))
@@ -196,6 +201,15 @@ def create_graphql_scalar_pages
196201
end
197202
end
198203

204+
def create_graphql_directive_pages
205+
graphql_directive_types.each do |directive_type|
206+
opts = default_generator_options(type: directive_type)
207+
208+
contents = @graphql_directives_template.result(OpenStruct.new(opts).instance_eval { binding })
209+
write_file('directive', directive_type[:name], contents)
210+
end
211+
end
212+
199213
private
200214

201215
def default_generator_options(opts = {})

lib/graphql-docs/helpers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def graphql_scalar_types
6262
@parsed_schema[:scalar_types] || []
6363
end
6464

65+
def graphql_directive_types
66+
@parsed_schema[:directive_types] || []
67+
end
68+
6569
def split_into_metadata_and_contents(contents, parse: true)
6670
opts = {}
6771
pieces = yaml_split(contents)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Directives
3+
---
4+
5+
# Directives
6+
7+
Directives provide a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
8+
9+
For more information, see [the GraphQL spec](https://graphql.github.io/graphql-spec/draft/#sec-Language.Directives).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h1><%= type[:name] %></h1>
2+
3+
<%= include.('notices.html', notices: type[:notices]) %>
4+
5+
<%= type[:description] %>
6+
7+
<%= include.('locations.html', locations: type[:locations]) %>
8+
9+
<% unless type[:arguments].empty? %>
10+
11+
<h2>Arguments</h2>
12+
13+
<%= include.('arguments.html', arguments: type[:arguments]) %>
14+
15+
<% end %>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h2 id="locations">Locations</h2>
2+
3+
<ul>
4+
5+
<% locations.each do |location| %>
6+
<li><%= location %></li>
7+
<% end %>
8+
9+
</ul>

lib/graphql-docs/layouts/includes/sidebar.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,18 @@
119119
<% end %>
120120
</ul>
121121
</li>
122+
123+
<li>
124+
<p><a href="<%= base_url %>/directive">Directives</a></p>
125+
<ul class="menu-root">
126+
<% graphql_directive_types.().each do |type| %>
127+
<% @name = type[:name] %>
128+
<li>
129+
<a href="<%= base_url %>/directive/<%= @name.downcase %>/" class="sidebar-link<% if title == @name %> current<% end %>">
130+
<%= @name %>
131+
</a>
132+
</li>
133+
<% end %>
134+
</ul>
135+
</li>
122136
</ul>

lib/graphql-docs/layouts/includes/values.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h3 id="fields">Values</h3>
1+
<h3 id="values">Values</h3>
22

33
<% values.each do |value| %>
44

lib/graphql-docs/parser.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def initialize(schema, options)
2727
union_types: [],
2828
input_object_types: [],
2929
scalar_types: [],
30+
directive_types: [],
3031
}
3132
end
3233

@@ -133,13 +134,20 @@ def parse
133134
end
134135
end
135136

136-
@processed_schema[:mutation_types].sort_by! { |o| o[:name] }
137-
@processed_schema[:object_types].sort_by! { |o| o[:name] }
138-
@processed_schema[:interface_types].sort_by! { |o| o[:name] }
139-
@processed_schema[:enum_types].sort_by! { |o| o[:name] }
140-
@processed_schema[:union_types].sort_by! { |o| o[:name] }
141-
@processed_schema[:input_object_types].sort_by! { |o| o[:name] }
142-
@processed_schema[:scalar_types].sort_by! { |o| o[:name] }
137+
@schema.directives.each_value do |directive|
138+
data = {}
139+
data[:notices] = @options[:notices].call(directive.name)
140+
141+
data[:name] = directive.name
142+
data[:description] = directive.description
143+
data[:locations] = directive.locations
144+
145+
data[:arguments], _ = fetch_fields(directive.arguments, directive.name)
146+
147+
@processed_schema[:directive_types] << data
148+
end
149+
150+
sort_by_name!
143151

144152
@processed_schema[:interface_types].each do |interface|
145153
interface[:implemented_by] = []
@@ -229,5 +237,12 @@ def generate_type(type)
229237
def argument?(field)
230238
field.is_a?(::GraphQL::Argument)
231239
end
240+
241+
def sort_by_name!
242+
@processed_schema.each_pair do |key, value|
243+
next if key == :operation_types || key == :root_types
244+
value.sort_by! { |o| o[:name] }
245+
end
246+
end
232247
end
233248
end

0 commit comments

Comments
 (0)