Skip to content

Commit 929e533

Browse files
authored
Merge pull request #82 from gjtorikian/latest-gql
Update to latest GQL
2 parents 56cba3a + fe9e288 commit 929e533

File tree

16 files changed

+222
-248
lines changed

16 files changed

+222
-248
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ language: ruby
33
cache: bundler
44

55
rvm:
6-
- 2.3.6
7-
- 2.4.3
8-
- 2.5.0
9-
- 2.6.0
6+
- 2.4.9
7+
- 2.5.7
8+
- 2.6.5
9+
- 2.7.0
1010

1111
git:
1212
depth: 10

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
source 'https://rubygems.org'
34

45
# Specify your gem's dependencies in graphql-docs.gemspec

Rakefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'bundler/gem_tasks'
34
require 'rake/testtask'
45

@@ -31,7 +32,7 @@ task :console do
3132
require 'graphql-docs'
3233

3334
def reload!
34-
files = $LOADED_FEATURES.select { |feat| feat =~ /\/graphql-docs\// }
35+
files = $LOADED_FEATURES.select { |feat| feat =~ %r{/graphql-docs/} }
3536
files.each { |file| load file }
3637
end
3738

@@ -46,7 +47,7 @@ task :generate_sample do
4647

4748
options = {}
4849
options[:delete_output] = true
49-
options[:base_url] = '/graphql-docs'
50+
options[:base_url] = ENV.fetch('GQL_DOCS_BASE_URL', '')
5051
options[:filename] = File.join(File.dirname(__FILE__), 'test', 'graphql-docs', 'fixtures', 'gh-schema.graphql')
5152

5253
GraphQLDocs.build(options)
@@ -66,6 +67,7 @@ end
6667

6768
desc 'Generate and publish docs to gh-pages'
6869
task :publish do
70+
ENV['GQL_DOCS_BASE_URL'] = '/graphql-docs'
6971
Rake::Task[:generate_sample].invoke('https://www.gjtorikian.com/graphql-docs')
7072
Dir.mktmpdir do |tmp|
7173
system "mv output/* #{tmp}"

graphql-docs.gemspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# coding: utf-8
21
# frozen_string_literal: true
3-
lib = File.expand_path('../lib', __FILE__)
2+
3+
lib = File.expand_path('lib', __dir__)
44
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55
require 'graphql-docs/version'
66

@@ -21,14 +21,14 @@ Gem::Specification.new do |spec|
2121
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
2222
spec.require_paths = ['lib']
2323

24-
spec.add_dependency 'graphql', '~> 1.8'
24+
spec.add_dependency 'graphql', '~> 1.10'
2525

2626
# rendering
2727
spec.add_dependency 'commonmarker', '~> 0.16'
28-
spec.add_dependency 'html-pipeline', '~> 2.9'
29-
spec.add_dependency 'escape_utils', '~> 1.2'
28+
spec.add_dependency 'escape_utils', '~> 1.2'
3029
spec.add_dependency 'extended-markdown-filter', '~> 0.4'
3130
spec.add_dependency 'gemoji', '~> 3.0'
31+
spec.add_dependency 'html-pipeline', '~> 2.9'
3232
spec.add_dependency 'sass', '~> 3.4'
3333

3434
spec.add_development_dependency 'awesome_print'

lib/graphql-docs.rb

Lines changed: 16 additions & 24 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'
@@ -9,19 +10,16 @@
910
begin
1011
require 'awesome_print'
1112
require 'pry'
12-
rescue LoadError; end
13-
13+
rescue LoadError; end # rubocop:disable Lint/SuppressedException
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/configuration.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module GraphQLDocs
34
module Configuration
45
GRAPHQLDOCS_DEFAULTS = {
@@ -11,9 +12,9 @@ module Configuration
1112
output_dir: './output/',
1213
pipeline_config: {
1314
pipeline:
14-
%i(ExtendedMarkdownFilter
15-
EmojiFilter
16-
TableOfContentsFilter),
15+
%i[ExtendedMarkdownFilter
16+
EmojiFilter
17+
TableOfContentsFilter],
1718
context: {
1819
gfm: false,
1920
unsafe: true, # necessary for layout needs, given that it's all HTML templates
@@ -37,7 +38,7 @@ module Configuration
3738
unions: "#{File.dirname(__FILE__)}/layouts/graphql_unions.html",
3839
input_objects: "#{File.dirname(__FILE__)}/layouts/graphql_input_objects.html",
3940
scalars: "#{File.dirname(__FILE__)}/layouts/graphql_scalars.html",
40-
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html",
41+
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html"
4142
},
4243

4344
landing_pages: {
@@ -59,7 +60,7 @@ module Configuration
5960
field_entry: '',
6061
deprecation_notice: '',
6162
notice: '',
62-
notice_title: '',
63+
notice_title: ''
6364
}
6465
}.freeze
6566
end

lib/graphql-docs/generator.rb

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'erb'
34
require 'sass'
45

@@ -14,14 +15,13 @@ def initialize(parsed_schema, options)
1415

1516
@renderer = @options[:renderer].new(@parsed_schema, @options)
1617

17-
%i(operations objects mutations interfaces enums unions input_objects scalars directives).each do |sym|
18-
if !File.exist?(@options[:templates][sym])
19-
raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found"
20-
end
18+
%i[operations objects mutations interfaces enums unions input_objects scalars directives].each do |sym|
19+
raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found" unless File.exist?(@options[:templates][sym])
20+
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 directive).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])
@@ -33,7 +33,7 @@ def initialize(parsed_schema, options)
3333

3434
if File.extname((@options[:landing_pages][sym])) == '.erb'
3535
opts = @options.merge(@options[:landing_pages][:variables]).merge(helper_methods)
36-
if has_yaml?(landing_page_contents)
36+
if yaml?(landing_page_contents)
3737
metadata, landing_page = split_into_metadata_and_contents(landing_page_contents, parse: false)
3838
erb_template = ERB.new(landing_page)
3939
else
@@ -60,45 +60,25 @@ def generate
6060
create_graphql_scalar_pages
6161
create_graphql_directive_pages
6262

63-
unless @graphql_index_landing_page.nil?
64-
write_file('static', 'index', @graphql_index_landing_page, trim: false)
65-
end
63+
write_file('static', 'index', @graphql_index_landing_page, trim: false) unless @graphql_index_landing_page.nil?
6664

67-
unless @graphql_object_landing_page.nil?
68-
write_file('static', 'object', @graphql_object_landing_page, trim: false)
69-
end
65+
write_file('static', 'object', @graphql_object_landing_page, trim: false) unless @graphql_object_landing_page.nil?
7066

71-
if !@graphql_query_landing_page.nil? && !has_query
72-
write_file('operation', 'query', @graphql_query_landing_page, trim: false)
73-
end
67+
write_file('operation', 'query', @graphql_query_landing_page, trim: false) if !@graphql_query_landing_page.nil? && !has_query
7468

75-
unless @graphql_mutation_landing_page.nil?
76-
write_file('operation', 'mutation', @graphql_mutation_landing_page, trim: false)
77-
end
69+
write_file('operation', 'mutation', @graphql_mutation_landing_page, trim: false) unless @graphql_mutation_landing_page.nil?
7870

79-
unless @graphql_interface_landing_page.nil?
80-
write_file('static', 'interface', @graphql_interface_landing_page, trim: false)
81-
end
71+
write_file('static', 'interface', @graphql_interface_landing_page, trim: false) unless @graphql_interface_landing_page.nil?
8272

83-
unless @graphql_enum_landing_page.nil?
84-
write_file('static', 'enum', @graphql_enum_landing_page, trim: false)
85-
end
73+
write_file('static', 'enum', @graphql_enum_landing_page, trim: false) unless @graphql_enum_landing_page.nil?
8674

87-
unless @graphql_union_landing_page.nil?
88-
write_file('static', 'union', @graphql_union_landing_page, trim: false)
89-
end
75+
write_file('static', 'union', @graphql_union_landing_page, trim: false) unless @graphql_union_landing_page.nil?
9076

91-
unless @graphql_input_object_landing_page.nil?
92-
write_file('static', 'input_object', @graphql_input_object_landing_page, trim: false)
93-
end
77+
write_file('static', 'input_object', @graphql_input_object_landing_page, trim: false) unless @graphql_input_object_landing_page.nil?
9478

95-
unless @graphql_scalar_landing_page.nil?
96-
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false)
97-
end
79+
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false) unless @graphql_scalar_landing_page.nil?
9880

99-
unless @graphql_directive_landing_page.nil?
100-
write_file('static', 'directive', @graphql_directive_landing_page, trim: false)
101-
end
81+
write_file('static', 'directive', @graphql_directive_landing_page, trim: false) unless @graphql_directive_landing_page.nil?
10282

10383
if @options[:use_default_styles]
10484
assets_dir = File.join(File.dirname(__FILE__), 'layouts', 'assets')
@@ -117,23 +97,23 @@ def generate
11797
def create_graphql_query_pages
11898
graphql_operation_types.each do |query_type|
11999
metadata = ''
120-
if query_type[:name] == graphql_root_types['query']
121-
unless @options[:landing_pages][:query].nil?
122-
query_landing_page = @options[:landing_pages][:query]
123-
query_landing_page = File.read(query_landing_page)
124-
if has_yaml?(query_landing_page)
125-
pieces = yaml_split(query_landing_page)
126-
pieces[2] = pieces[2].chomp
127-
metadata = pieces[1, 3].join("\n")
128-
query_landing_page = pieces[4]
129-
end
130-
query_type[:description] = query_landing_page
100+
next unless query_type[:name] == graphql_root_types['query']
101+
102+
unless @options[:landing_pages][:query].nil?
103+
query_landing_page = @options[:landing_pages][:query]
104+
query_landing_page = File.read(query_landing_page)
105+
if yaml?(query_landing_page)
106+
pieces = yaml_split(query_landing_page)
107+
pieces[2] = pieces[2].chomp
108+
metadata = pieces[1, 3].join("\n")
109+
query_landing_page = pieces[4]
131110
end
132-
opts = default_generator_options(type: query_type)
133-
contents = @graphql_operations_template.result(OpenStruct.new(opts).instance_eval { binding })
134-
write_file('operation', 'query', metadata + contents)
135-
return true
111+
query_type[:description] = query_landing_page
136112
end
113+
opts = default_generator_options(type: query_type)
114+
contents = @graphql_operations_template.result(OpenStruct.new(opts).instance_eval { binding })
115+
write_file('operation', 'query', metadata + contents)
116+
return true
137117
end
138118
false
139119
end
@@ -229,7 +209,7 @@ def write_file(type, name, contents, trim: true)
229209
FileUtils.mkdir_p(path)
230210
end
231211

232-
if has_yaml?(contents)
212+
if yaml?(contents)
233213
# Split data
234214
meta, contents = split_into_metadata_and_contents(contents)
235215
@options = @options.merge(meta)

lib/graphql-docs/helpers.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Helpers
1010

1111
def slugify(str)
1212
slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-')
13-
slug.gsub!(%r!^\-|\-$!i, '')
13+
slug.gsub!(/^\-|\-$/i, '')
1414
slug.downcase
1515
end
1616

@@ -22,6 +22,7 @@ def include(filename, opts = {})
2222

2323
def markdownify(string)
2424
return '' if string.nil?
25+
2526
type = @options[:pipeline_config][:context][:unsafe] ? :UNSAFE : :DEFAULT
2627
::CommonMarker.render_html(string, type).strip
2728
end
@@ -67,27 +68,23 @@ def graphql_directive_types
6768
end
6869

6970
def split_into_metadata_and_contents(contents, parse: true)
70-
opts = {}
7171
pieces = yaml_split(contents)
72-
if pieces.size < 4
73-
raise RuntimeError.new(
74-
"The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format.",
75-
)
76-
end
72+
raise "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format." if pieces.size < 4
73+
7774
# Parse
7875
begin
79-
if parse
80-
meta = YAML.load(pieces[2]) || {}
81-
else
82-
meta = pieces[2]
83-
end
76+
meta = if parse
77+
YAML.safe_load(pieces[2]) || {}
78+
else
79+
pieces[2]
80+
end
8481
rescue Exception => e # rubocop:disable Lint/RescueException
8582
raise "Could not parse YAML for #{name}: #{e.message}"
8683
end
8784
[meta, pieces[4]]
8885
end
8986

90-
def has_yaml?(contents)
87+
def yaml?(contents)
9188
contents =~ /\A-{3,5}\s*$/
9289
end
9390

@@ -114,6 +111,7 @@ def helper_methods
114111

115112
Helpers.instance_methods.each do |name|
116113
next if name == :helper_methods
114+
117115
@helper_methods[name] = method(name)
118116
end
119117

0 commit comments

Comments
 (0)