Skip to content

Commit 2aec020

Browse files
authored
List Queries in sidebar (#156)
* Queries in sidebar * Fix tests that broke when generator behavior changed
1 parent 222703c commit 2aec020

File tree

10 files changed

+88
-55
lines changed

10 files changed

+88
-55
lines changed

lib/graphql-docs/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Configuration
3232

3333
operations: "#{File.dirname(__FILE__)}/layouts/graphql_operations.html",
3434
objects: "#{File.dirname(__FILE__)}/layouts/graphql_objects.html",
35+
queries: "#{File.dirname(__FILE__)}/layouts/graphql_queries.html",
3536
mutations: "#{File.dirname(__FILE__)}/layouts/graphql_mutations.html",
3637
interfaces: "#{File.dirname(__FILE__)}/layouts/graphql_interfaces.html",
3738
enums: "#{File.dirname(__FILE__)}/layouts/graphql_enums.html",

lib/graphql-docs/generator.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(parsed_schema, options)
1717

1818
@renderer = @options[:renderer].new(@parsed_schema, @options)
1919

20-
%i[operations objects mutations interfaces enums unions input_objects scalars directives].each do |sym|
20+
%i[operations objects queries mutations interfaces enums unions input_objects scalars directives].each do |sym|
2121
raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found" unless File.exist?(@options[:templates][sym])
2222

2323
instance_variable_set("@graphql_#{sym}_template", ERB.new(File.read(@options[:templates][sym])))
@@ -52,8 +52,9 @@ def initialize(parsed_schema, options)
5252
def generate
5353
FileUtils.rm_rf(@options[:output_dir]) if @options[:delete_output]
5454

55-
has_query = create_graphql_query_pages
55+
has_query = create_graphql_operation_pages
5656
create_graphql_object_pages
57+
create_graphql_query_pages
5758
create_graphql_mutation_pages
5859
create_graphql_interface_pages
5960
create_graphql_enum_pages
@@ -96,7 +97,7 @@ def generate
9697
true
9798
end
9899

99-
def create_graphql_query_pages
100+
def create_graphql_operation_pages
100101
graphql_operation_types.each do |query_type|
101102
metadata = ''
102103
next unless query_type[:name] == graphql_root_types['query']
@@ -129,6 +130,15 @@ def create_graphql_object_pages
129130
end
130131
end
131132

133+
def create_graphql_query_pages
134+
graphql_query_types.each do |query|
135+
opts = default_generator_options(type: query)
136+
137+
contents = @graphql_queries_template.result(OpenStruct.new(opts).instance_eval { binding })
138+
write_file('query', query[:name], contents)
139+
end
140+
end
141+
132142
def create_graphql_mutation_pages
133143
graphql_mutation_types.each do |mutation|
134144
opts = default_generator_options(type: mutation)

lib/graphql-docs/helpers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def graphql_operation_types
3636
@parsed_schema[:operation_types] || []
3737
end
3838

39+
def graphql_query_types
40+
@parsed_schema[:query_types] || []
41+
end
42+
3943
def graphql_mutation_types
4044
@parsed_schema[:mutation_types] || []
4145
end
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
---
22
title: Queries
33
---
4-
Every GraphQL schema has a root type for both queries and mutations. The [query type](http://spec.graphql.org/draft/#sec-Type-System) defines GraphQL operations that retrieve data from the server.
4+
Every GraphQL schema has a root type for both queries and mutations.
5+
6+
The query type defines GraphQL operations that retrieve data from the server.
7+
8+
For more information, see [the GraphQL spec](http://spec.graphql.org/draft/#sec-Type-System).
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
11
<h1><%= type[:name] %></h1>
22

33
<%= type[:description] %>
4-
5-
<% unless type[:connections].empty? %>
6-
7-
<h2>Connections</h2>
8-
9-
<%= include.('connections.html', connections: type[:connections]) %>
10-
11-
<% end %>
12-
13-
<% unless type[:fields].empty? %>
14-
15-
<h2>Fields</h2>
16-
17-
<%= include.('fields.html', fields: type[:fields]) %>
18-
19-
<% end %>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<h1><%= type[:name] %></h1>
2+
3+
<%= include.('notices.html', notices: type[:notices]) %>
4+
5+
<%= type[:description] %>
6+
7+
<% if !type[:arguments].empty? %>
8+
9+
<h2>Arguments</h2>
10+
11+
<%= include.('fields.html', fields: type[:arguments]) %>
12+
13+
<% end %>
14+
15+
16+
<% if !type[:return_fields].empty? %>
17+
18+
<h2>Return fields</h2>
19+
20+
<%= include.('fields.html', fields: type[:return_fields]) %>
21+
22+
<% end %>

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</li>
88

99
<li>
10-
<p>Queries</p>
10+
<p>Operations</p>
1111
<ul class="menu-root">
1212
<li>
1313
<a href="<%= base_url %>/operation/query/" class="sidebar-link<% if title == "Query" %> current<% end %>">
@@ -23,12 +23,12 @@
2323
</li>
2424

2525
<li>
26-
<p><a href="<%= base_url %>/object">Objects</a></p>
26+
<p><a href="<%= base_url %>/operation/query">Queries</a></p>
2727
<ul class="menu-root">
28-
<% graphql_object_types.().each do |type| %>
29-
<% @name = type[:name] %>
28+
<% graphql_query_types.().each do |type| %>
29+
<% @name = type[:name] %>
3030
<li>
31-
<a href="<%= base_url %>/object/<%= @name.downcase %>/" class="sidebar-link<% if title == @name %> current<% end %>">
31+
<a href="<%= base_url %>/query/<%= @name.downcase %>/" class="sidebar-link<% if title == @name %> current<% end %>">
3232
<%= @name %>
3333
</a>
3434
</li>
@@ -50,6 +50,20 @@
5050
</ul>
5151
</li>
5252

53+
<li>
54+
<p><a href="<%= base_url %>/object">Objects</a></p>
55+
<ul class="menu-root">
56+
<% graphql_object_types.().each do |type| %>
57+
<% @name = type[:name] %>
58+
<li>
59+
<a href="<%= base_url %>/object/<%= @name.downcase %>/" class="sidebar-link<% if title == @name %> current<% end %>">
60+
<%= @name %>
61+
</a>
62+
</li>
63+
<% end %>
64+
</ul>
65+
</li>
66+
5367
<li>
5468
<p><a href="<%= base_url %>/interface">Interfaces</a></p>
5569
<ul class="menu-root">

lib/graphql-docs/parser.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def initialize(schema, options)
2121

2222
@processed_schema = {
2323
operation_types: [],
24+
query_types: [],
2425
mutation_types: [],
2526
object_types: [],
2627
interface_types: [],
@@ -51,8 +52,25 @@ def parse
5152
if data[:name] == root_types['query']
5253
data[:interfaces] = object.interfaces.map(&:graphql_name).sort
5354
data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)
54-
5555
@processed_schema[:operation_types] << data
56+
57+
object.fields.each_value do |query|
58+
h = {}
59+
60+
h[:notices] = @options[:notices].call([object.graphql_name, query.graphql_name].join('.'))
61+
h[:name] = query.graphql_name
62+
h[:description] = query.description
63+
h[:arguments], = fetch_fields(query.arguments, [object.graphql_name, query.graphql_name].join('.'))
64+
65+
return_type = query.type
66+
if return_type.unwrap.respond_to?(:fields)
67+
h[:return_fields], = fetch_fields(return_type.unwrap.fields, return_type.graphql_name)
68+
else # it is a scalar return type
69+
h[:return_fields], = fetch_fields({ return_type.graphql_name => query }, return_type.graphql_name)
70+
end
71+
72+
@processed_schema[:query_types] << h
73+
end
5674
elsif data[:name] == root_types['mutation']
5775
@processed_schema[:operation_types] << data
5876

test/graphql-docs/fixtures/named-root-schema.graphql

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/graphql-docs/generator_test.rb

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def setup
2626
@tiny_parser = GraphQLDocs::Parser.new(tiny_schema, {})
2727
@tiny_results = @tiny_parser.parse
2828

29-
named_root_schema = File.read(File.join(fixtures_dir, 'named-root-schema.graphql'))
30-
@named_root_parser = GraphQLDocs::Parser.new(named_root_schema, {})
31-
@named_root_results = @named_root_parser.parse
32-
3329
@output_dir = File.join(fixtures_dir, 'output')
3430
end
3531

@@ -66,6 +62,7 @@ def test_that_it_works
6662
assert File.exist? File.join(@output_dir, 'enum', 'issuestate', 'index.html')
6763
assert File.exist? File.join(@output_dir, 'input_object', 'projectorder', 'index.html')
6864
assert File.exist? File.join(@output_dir, 'interface', 'reactable', 'index.html')
65+
assert File.exist? File.join(@output_dir, 'query', 'codeofconduct', 'index.html')
6966
assert File.exist? File.join(@output_dir, 'mutation', 'addcomment', 'index.html')
7067
assert File.exist? File.join(@output_dir, 'object', 'repository', 'index.html')
7168
assert File.exist? File.join(@output_dir, 'scalar', 'boolean', 'index.html')
@@ -136,18 +133,6 @@ def test_that_it_sets_classes
136133
assert_match(/<div class="field-entry my-4">/, object)
137134
end
138135

139-
def test_that_named_query_root_generates_fields
140-
options = deep_copy(GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS)
141-
options[:output_dir] = @output_dir
142-
143-
generator = GraphQLDocs::Generator.new(@named_root_results, options)
144-
generator.generate
145-
146-
object = File.read File.join(@output_dir, 'operation', 'query', 'index.html')
147-
148-
assert_match(/Do a thing/, object)
149-
end
150-
151136
def test_that_missing_landing_pages_are_reported
152137
options = deep_copy(GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS)
153138
options[:landing_pages][:index] = 'BOGUS'
@@ -202,9 +187,9 @@ def test_that_empty_html_lines_not_interpreted_by_markdown
202187
generator = GraphQLDocs::Generator.new(@tiny_results, options)
203188
generator.generate
204189

205-
contents = File.read File.join(@output_dir, 'operation', 'query', 'index.html')
190+
contents = File.read File.join(@output_dir, 'query', 'codeofconduct', 'index.html')
206191

207-
assert_match(%r{<td>\s+<p>The code of conduct's key</p>\s+</td>}, contents)
192+
assert_match(%r{<p>The code of conduct's key</p>}, contents)
208193
end
209194

210195
def test_that_non_empty_html_lines_not_interpreted_by_markdown
@@ -216,6 +201,7 @@ def test_that_non_empty_html_lines_not_interpreted_by_markdown
216201

217202
contents = File.read File.join(@output_dir, 'input_object', 'projectorder', 'index.html')
218203

219-
assert_match %r{<div class="description-wrapper">\n <p>The direction in which to order projects by the specified field.</p>\s+</div>}, contents
204+
assert_match %r{<div class="description-wrapper">\n <p>The direction in which to order projects by the specified field.</p>\s+</div>},
205+
contents
220206
end
221207
end

0 commit comments

Comments
 (0)