Skip to content

Commit 6f29f4b

Browse files
committed
Upgrade to new GQL gem
1 parent 23e188b commit 6f29f4b

File tree

6 files changed

+110
-120
lines changed

6 files changed

+110
-120
lines changed

lib/graphql-docs.rb

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'graphql-docs/helpers'
34
require 'graphql-docs/renderer'
45
require 'graphql-docs/configuration'
@@ -10,18 +11,15 @@
1011
require 'awesome_print'
1112
require 'pry'
1213
rescue LoadError; end
13-
1414
module GraphQLDocs
1515
class << self
1616
def build(options)
1717
# do not let user provided values overwrite every single value
18-
%i(templates landing_pages).each do |opt|
19-
if options.key?(opt)
20-
GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS[opt].each_pair do |key, value|
21-
unless options[opt].key?(key)
22-
options[opt][key] = value
23-
end
24-
end
18+
%i[templates landing_pages].each do |opt|
19+
next unless options.key?(opt)
20+
21+
GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS[opt].each_pair do |key, value|
22+
options[opt][key] = value unless options[opt].key?(key)
2523
end
2624
end
2725

@@ -30,28 +28,18 @@ def build(options)
3028
filename = options[:filename]
3129
schema = options[:schema]
3230

33-
if !filename.nil? && !schema.nil?
34-
raise ArgumentError, 'Pass in `filename` or `schema`, but not both!'
35-
end
31+
raise ArgumentError, 'Pass in `filename` or `schema`, but not both!' if !filename.nil? && !schema.nil?
3632

37-
if filename.nil? && schema.nil?
38-
raise ArgumentError, 'Pass in either `filename` or `schema`'
39-
end
33+
raise ArgumentError, 'Pass in either `filename` or `schema`' if filename.nil? && schema.nil?
4034

4135
if filename
42-
unless filename.is_a?(String)
43-
raise TypeError, "Expected `String`, got `#{filename.class}`"
44-
end
36+
raise TypeError, "Expected `String`, got `#{filename.class}`" unless filename.is_a?(String)
4537

46-
unless File.exist?(filename)
47-
raise ArgumentError, "#{filename} does not exist!"
48-
end
38+
raise ArgumentError, "#{filename} does not exist!" unless File.exist?(filename)
4939

5040
schema = File.read(filename)
5141
else
52-
if !schema.is_a?(String) && !schema.is_a?(GraphQL::Schema)
53-
raise TypeError, "Expected `String` or `GraphQL::Schema`, got `#{schema.class}`"
54-
end
42+
raise TypeError, "Expected `String` or `GraphQL::Schema`, got `#{schema.class}`" if !schema.is_a?(String) && !schema_type?(schema)
5543

5644
schema = schema
5745
end
@@ -63,5 +51,9 @@ def build(options)
6351

6452
generator.generate
6553
end
54+
55+
private def schema_type?(object)
56+
object.respond_to?(:ancestors) && object.ancestors.include?(GraphQL::Schema)
57+
end
6658
end
6759
end

lib/graphql-docs/parser.rb

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'graphql'
34

45
module GraphQLDocs
@@ -10,13 +11,13 @@ class Parser
1011
def initialize(schema, options)
1112
@options = options
1213

13-
@options[:notices] ||= -> (schema_member_path) { [] }
14+
@options[:notices] ||= ->(_schema_member_path) { [] }
1415

15-
if schema.is_a?(GraphQL::Schema)
16-
@schema = schema
17-
else
18-
@schema = GraphQL::Schema.from_definition(schema)
19-
end
16+
@schema = if schema.is_a?(String)
17+
GraphQL::Schema.from_definition(schema)
18+
elsif schema < GraphQL::Schema
19+
schema
20+
end
2021

2122
@processed_schema = {
2223
operation_types: [],
@@ -27,80 +28,71 @@ def initialize(schema, options)
2728
union_types: [],
2829
input_object_types: [],
2930
scalar_types: [],
30-
directive_types: [],
31+
directive_types: []
3132
}
3233
end
3334

3435
def parse
3536
root_types = {}
36-
['query', 'mutation'].each do |operation|
37-
unless @schema.root_type_for_operation(operation).nil?
38-
root_types[operation] = @schema.root_type_for_operation(operation).name
39-
end
37+
%w[query mutation].each do |operation|
38+
root_types[operation] = @schema.root_type_for_operation(operation).graphql_name unless @schema.root_type_for_operation(operation).nil?
4039
end
4140
@processed_schema[:root_types] = root_types
4241

4342
@schema.types.each_value do |object|
4443
data = {}
4544

46-
data[:notices] = @options[:notices].call(object.name)
45+
data[:notices] = @options[:notices].call(object.graphql_name)
4746

48-
case object
49-
when ::GraphQL::ObjectType
50-
if object.name == root_types['query']
51-
data[:name] = object.name
52-
data[:description] = object.description
47+
if object < ::GraphQL::Schema::Object
48+
data[:name] = object.graphql_name
49+
data[:description] = object.description
5350

54-
data[:interfaces] = object.interfaces.map(&:name).sort
55-
data[:fields], data[:connections] = fetch_fields(object.fields, object.name)
51+
if data[:name] == root_types['query']
52+
data[:interfaces] = object.interfaces.map(&:graphql_name).sort
53+
data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)
5654

5755
@processed_schema[:operation_types] << data
58-
elsif object.name == root_types['mutation']
59-
data[:name] = object.name
60-
data[:description] = object.description
61-
56+
elsif data[:name] == root_types['mutation']
6257
@processed_schema[:operation_types] << data
6358

6459
object.fields.each_value do |mutation|
6560
h = {}
6661

67-
h[:notices] = @options[:notices].call([object.name, mutation.name].join('.'))
68-
h[:name] = mutation.name
62+
h[:notices] = @options[:notices].call([object.graphql_name, mutation.graphql_name].join('.'))
63+
h[:name] = mutation.graphql_name
6964
h[:description] = mutation.description
70-
h[:input_fields], _ = fetch_fields(mutation.arguments, [object.name, mutation.name].join('.'))
65+
h[:input_fields], = fetch_fields(mutation.arguments, [object.graphql_name, mutation.graphql_name].join('.'))
7166

7267
return_type = mutation.type
7368
if return_type.unwrap.respond_to?(:fields)
74-
h[:return_fields], _ = fetch_fields(return_type.unwrap.fields, return_type.name)
69+
h[:return_fields], = fetch_fields(return_type.unwrap.fields, return_type.graphql_name)
7570
else # it is a scalar return type
76-
h[:return_fields], _ = fetch_fields({ "#{return_type.name}" => mutation }, return_type.name)
71+
h[:return_fields], = fetch_fields({ return_type.graphql_name => mutation }, return_type.graphql_name)
7772
end
7873

7974
@processed_schema[:mutation_types] << h
8075
end
8176
else
82-
data[:name] = object.name
83-
data[:description] = object.description
84-
85-
data[:interfaces] = object.interfaces.map(&:name).sort
86-
data[:fields], data[:connections] = fetch_fields(object.fields, object.name)
77+
data[:interfaces] = object.interfaces.map(&:graphql_name).sort
78+
data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)
8779

8880
@processed_schema[:object_types] << data
8981
end
90-
when ::GraphQL::InterfaceType
91-
data[:name] = object.name
82+
elsif object < ::GraphQL::Schema::Interface
83+
data[:name] = object.graphql_name
9284
data[:description] = object.description
93-
data[:fields], data[:connections] = fetch_fields(object.fields, object.name)
85+
data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)
9486

9587
@processed_schema[:interface_types] << data
96-
when ::GraphQL::EnumType
97-
data[:name] = object.name
88+
elsif object < ::GraphQL::Schema::Enum
89+
data[:name] = object.graphql_name
9890
data[:description] = object.description
9991

10092
data[:values] = object.values.values.map do |val|
10193
h = {}
102-
h[:notices] = @options[:notices].call([object.name, val.name].join('.'))
103-
h[:name] = val.name
94+
h[:notices] = @options[:notices].call([object.graphql_name, val.graphql_name].join('.'))
95+
h[:name] = val.graphql_name
10496
h[:description] = val.description
10597
unless val.deprecation_reason.nil?
10698
h[:is_deprecated] = true
@@ -110,38 +102,38 @@ def parse
110102
end
111103

112104
@processed_schema[:enum_types] << data
113-
when ::GraphQL::UnionType
114-
data[:name] = object.name
105+
elsif object < ::GraphQL::Schema::Union
106+
data[:name] = object.graphql_name
115107
data[:description] = object.description
116-
data[:possible_types] = object.possible_types.map(&:name).sort
108+
data[:possible_types] = object.possible_types.map(&:graphql_name).sort
117109

118110
@processed_schema[:union_types] << data
119-
when ::GraphQL::InputObjectType
120-
data[:name] = object.name
111+
elsif object < GraphQL::Schema::InputObject
112+
data[:name] = object.graphql_name
121113
data[:description] = object.description
122114

123-
data[:input_fields], _ = fetch_fields(object.input_fields, object.name)
115+
data[:input_fields], = fetch_fields(object.arguments, object.graphql_name)
124116

125117
@processed_schema[:input_object_types] << data
126-
when ::GraphQL::ScalarType
127-
data[:name] = object.name
118+
elsif object < GraphQL::Schema::Scalar
119+
data[:name] = object.graphql_name
128120
data[:description] = object.description
129121

130122
@processed_schema[:scalar_types] << data
131123
else
132-
raise TypeError, "I'm not sure what #{object.class} is!"
124+
raise TypeError, "I'm not sure what #{object.class} < #{object.superclass.name} is!"
133125
end
134126
end
135127

136128
@schema.directives.each_value do |directive|
137129
data = {}
138-
data[:notices] = @options[:notices].call(directive.name)
130+
data[:notices] = @options[:notices].call(directive.graphql_name)
139131

140-
data[:name] = directive.name
132+
data[:name] = directive.graphql_name
141133
data[:description] = directive.description
142134
data[:locations] = directive.locations
143135

144-
data[:arguments], _ = fetch_fields(directive.arguments, directive.name)
136+
data[:arguments], = fetch_fields(directive.arguments, directive.graphql_name)
145137

146138
@processed_schema[:directive_types] << data
147139
end
@@ -151,9 +143,7 @@ def parse
151143
@processed_schema[:interface_types].each do |interface|
152144
interface[:implemented_by] = []
153145
@processed_schema[:object_types].each do |obj|
154-
if obj[:interfaces].include?(interface[:name])
155-
interface[:implemented_by] << obj[:name]
156-
end
146+
interface[:implemented_by] << obj[:name] if obj[:interfaces].include?(interface[:name])
157147
end
158148
end
159149

@@ -169,8 +159,8 @@ def fetch_fields(object_fields, parent_path)
169159
object_fields.each_value do |field|
170160
hash = {}
171161

172-
hash[:notices] = @options[:notices].call([parent_path, field.name].join('.'))
173-
hash[:name] = field.name
162+
hash[:notices] = @options[:notices].call([parent_path, field.graphql_name].join('.'))
163+
hash[:name] = field.graphql_name
174164
hash[:description] = field.description
175165
if field.respond_to?(:deprecation_reason) && !field.deprecation_reason.nil?
176166
hash[:is_deprecated] = true
@@ -183,17 +173,15 @@ def fetch_fields(object_fields, parent_path)
183173
if field.respond_to?(:arguments)
184174
field.arguments.each_value do |arg|
185175
h = {}
186-
h[:name] = arg.name
176+
h[:name] = arg.graphql_name
187177
h[:description] = arg.description
188178
h[:type] = generate_type(arg.type)
189-
if arg.default_value?
190-
h[:default_value] = arg.default_value
191-
end
179+
h[:default_value] = arg.default_value if arg.default_value?
192180
hash[:arguments] << h
193181
end
194182
end
195183

196-
if !argument?(field) && field.connection?
184+
if !argument?(field) && connection?(field)
197185
connections << hash
198186
else
199187
fields << hash
@@ -204,26 +192,26 @@ def fetch_fields(object_fields, parent_path)
204192
end
205193

206194
def generate_type(type)
207-
name = type.unwrap.to_s
208-
path = case type.unwrap
209-
when ::GraphQL::ObjectType
195+
name = type.unwrap.graphql_name
196+
197+
path = if type.unwrap < GraphQL::Schema::Object
210198
if name == 'Query'
211199
'operation'
212200
else
213201
'object'
214202
end
215-
when ::GraphQL::ScalarType
203+
elsif type.unwrap < GraphQL::Schema::Scalar
216204
'scalar'
217-
when ::GraphQL::InterfaceType
205+
elsif type.unwrap < GraphQL::Schema::Interface
218206
'interface'
219-
when ::GraphQL::EnumType
207+
elsif type.unwrap < GraphQL::Schema::Enum
220208
'enum'
221-
when ::GraphQL::InputObjectType
209+
elsif type.unwrap < GraphQL::Schema::InputObject
222210
'input_object'
223-
when ::GraphQL::UnionType
211+
elsif type.unwrap < GraphQL::Schema::Union
224212
'union'
225213
else
226-
raise TypeError, "Unknown type: `#{type.unwrap.class}`"
214+
raise TypeError, "Unknown type for `#{name}`: `#{type.unwrap.class}`"
227215
end
228216

229217
{
@@ -234,12 +222,18 @@ def generate_type(type)
234222
end
235223

236224
def argument?(field)
237-
field.is_a?(::GraphQL::Argument)
225+
field.is_a?(::GraphQL::Schema::Argument)
226+
end
227+
228+
def connection?(field)
229+
field.respond_to?(:connection?) && field.connection?
238230
end
239231

240232
def sort_by_name!
241233
@processed_schema.each_pair do |key, value|
234+
next if value.empty?
242235
next if key == :operation_types || key == :root_types
236+
243237
value.sort_by! { |o| o[:name] }
244238
end
245239
end

test/graphql-docs/generator_test.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# frozen_string_literal: true
2+
23
require 'test_helper'
34

45
class GeneratorTest < Minitest::Test
56
class CustomRenderer
6-
def initialize(_, _)
7-
end
7+
def initialize(_, _); end
88

99
def render(contents, type: nil, name: nil, filename: nil)
1010
to_html(contents)
1111
end
1212

1313
def to_html(contents)
1414
return '' if contents.nil?
15+
1516
contents.sub(/CodeOfConduct/i, 'CoC!!!!!')
1617
end
1718
end
@@ -191,7 +192,7 @@ def test_that_markdown_preserves_whitespace
191192

192193
contents = File.read File.join(@output_dir, 'index.html')
193194

194-
assert_match %r{ "nest2": \{}, contents
195+
assert_match(/ "nest2": \{/, contents)
195196
end
196197

197198
def test_that_empty_html_lines_not_interpreted_by_markdown
@@ -203,7 +204,7 @@ def test_that_empty_html_lines_not_interpreted_by_markdown
203204

204205
contents = File.read File.join(@output_dir, 'operation', 'query', 'index.html')
205206

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

209210
def test_that_non_empty_html_lines_not_interpreted_by_markdown

0 commit comments

Comments
 (0)