Skip to content

Commit

Permalink
Merge pull request #82 from gjtorikian/latest-gql
Browse files Browse the repository at this point in the history
Update to latest GQL
  • Loading branch information
gjtorikian committed Feb 24, 2020
2 parents 56cba3a + fe9e288 commit 929e533
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 248 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ language: ruby
cache: bundler

rvm:
- 2.3.6
- 2.4.3
- 2.5.0
- 2.6.0
- 2.4.9
- 2.5.7
- 2.6.5
- 2.7.0

git:
depth: 10
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in graphql-docs.gemspec
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'

Expand Down Expand Up @@ -31,7 +32,7 @@ task :console do
require 'graphql-docs'

def reload!
files = $LOADED_FEATURES.select { |feat| feat =~ /\/graphql-docs\// }
files = $LOADED_FEATURES.select { |feat| feat =~ %r{/graphql-docs/} }
files.each { |file| load file }
end

Expand All @@ -46,7 +47,7 @@ task :generate_sample do

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

GraphQLDocs.build(options)
Expand All @@ -66,6 +67,7 @@ end

desc 'Generate and publish docs to gh-pages'
task :publish do
ENV['GQL_DOCS_BASE_URL'] = '/graphql-docs'
Rake::Task[:generate_sample].invoke('https://www.gjtorikian.com/graphql-docs')
Dir.mktmpdir do |tmp|
system "mv output/* #{tmp}"
Expand Down
10 changes: 5 additions & 5 deletions graphql-docs.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
# frozen_string_literal: true
lib = File.expand_path('../lib', __FILE__)

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'graphql-docs/version'

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

spec.add_dependency 'graphql', '~> 1.8'
spec.add_dependency 'graphql', '~> 1.10'

# rendering
spec.add_dependency 'commonmarker', '~> 0.16'
spec.add_dependency 'html-pipeline', '~> 2.9'
spec.add_dependency 'escape_utils', '~> 1.2'
spec.add_dependency 'escape_utils', '~> 1.2'
spec.add_dependency 'extended-markdown-filter', '~> 0.4'
spec.add_dependency 'gemoji', '~> 3.0'
spec.add_dependency 'html-pipeline', '~> 2.9'
spec.add_dependency 'sass', '~> 3.4'

spec.add_development_dependency 'awesome_print'
Expand Down
40 changes: 16 additions & 24 deletions lib/graphql-docs.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'graphql-docs/helpers'
require 'graphql-docs/renderer'
require 'graphql-docs/configuration'
Expand All @@ -9,19 +10,16 @@
begin
require 'awesome_print'
require 'pry'
rescue LoadError; end

rescue LoadError; end # rubocop:disable Lint/SuppressedException
module GraphQLDocs
class << self
def build(options)
# do not let user provided values overwrite every single value
%i(templates landing_pages).each do |opt|
if options.key?(opt)
GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS[opt].each_pair do |key, value|
unless options[opt].key?(key)
options[opt][key] = value
end
end
%i[templates landing_pages].each do |opt|
next unless options.key?(opt)

GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS[opt].each_pair do |key, value|
options[opt][key] = value unless options[opt].key?(key)
end
end

Expand All @@ -30,28 +28,18 @@ def build(options)
filename = options[:filename]
schema = options[:schema]

if !filename.nil? && !schema.nil?
raise ArgumentError, 'Pass in `filename` or `schema`, but not both!'
end
raise ArgumentError, 'Pass in `filename` or `schema`, but not both!' if !filename.nil? && !schema.nil?

if filename.nil? && schema.nil?
raise ArgumentError, 'Pass in either `filename` or `schema`'
end
raise ArgumentError, 'Pass in either `filename` or `schema`' if filename.nil? && schema.nil?

if filename
unless filename.is_a?(String)
raise TypeError, "Expected `String`, got `#{filename.class}`"
end
raise TypeError, "Expected `String`, got `#{filename.class}`" unless filename.is_a?(String)

unless File.exist?(filename)
raise ArgumentError, "#{filename} does not exist!"
end
raise ArgumentError, "#{filename} does not exist!" unless File.exist?(filename)

schema = File.read(filename)
else
if !schema.is_a?(String) && !schema.is_a?(GraphQL::Schema)
raise TypeError, "Expected `String` or `GraphQL::Schema`, got `#{schema.class}`"
end
raise TypeError, "Expected `String` or `GraphQL::Schema`, got `#{schema.class}`" if !schema.is_a?(String) && !schema_type?(schema)

schema = schema
end
Expand All @@ -63,5 +51,9 @@ def build(options)

generator.generate
end

private def schema_type?(object)
object.respond_to?(:ancestors) && object.ancestors.include?(GraphQL::Schema)
end
end
end
11 changes: 6 additions & 5 deletions lib/graphql-docs/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module GraphQLDocs
module Configuration
GRAPHQLDOCS_DEFAULTS = {
Expand All @@ -11,9 +12,9 @@ module Configuration
output_dir: './output/',
pipeline_config: {
pipeline:
%i(ExtendedMarkdownFilter
EmojiFilter
TableOfContentsFilter),
%i[ExtendedMarkdownFilter
EmojiFilter
TableOfContentsFilter],
context: {
gfm: false,
unsafe: true, # necessary for layout needs, given that it's all HTML templates
Expand All @@ -37,7 +38,7 @@ module Configuration
unions: "#{File.dirname(__FILE__)}/layouts/graphql_unions.html",
input_objects: "#{File.dirname(__FILE__)}/layouts/graphql_input_objects.html",
scalars: "#{File.dirname(__FILE__)}/layouts/graphql_scalars.html",
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html",
directives: "#{File.dirname(__FILE__)}/layouts/graphql_directives.html"
},

landing_pages: {
Expand All @@ -59,7 +60,7 @@ module Configuration
field_entry: '',
deprecation_notice: '',
notice: '',
notice_title: '',
notice_title: ''
}
}.freeze
end
Expand Down
84 changes: 32 additions & 52 deletions lib/graphql-docs/generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'erb'
require 'sass'

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

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

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

instance_variable_set("@graphql_#{sym}_template", ERB.new(File.read(@options[:templates][sym])))
end

%i(index object query mutation interface enum union input_object scalar directive).each do |sym|
%i[index object query mutation interface enum union input_object scalar directive].each do |sym|
if @options[:landing_pages][sym].nil?
instance_variable_set("@#{sym}_landing_page", nil)
elsif !File.exist?(@options[:landing_pages][sym])
Expand All @@ -33,7 +33,7 @@ def initialize(parsed_schema, options)

if File.extname((@options[:landing_pages][sym])) == '.erb'
opts = @options.merge(@options[:landing_pages][:variables]).merge(helper_methods)
if has_yaml?(landing_page_contents)
if yaml?(landing_page_contents)
metadata, landing_page = split_into_metadata_and_contents(landing_page_contents, parse: false)
erb_template = ERB.new(landing_page)
else
Expand All @@ -60,45 +60,25 @@ def generate
create_graphql_scalar_pages
create_graphql_directive_pages

unless @graphql_index_landing_page.nil?
write_file('static', 'index', @graphql_index_landing_page, trim: false)
end
write_file('static', 'index', @graphql_index_landing_page, trim: false) unless @graphql_index_landing_page.nil?

unless @graphql_object_landing_page.nil?
write_file('static', 'object', @graphql_object_landing_page, trim: false)
end
write_file('static', 'object', @graphql_object_landing_page, trim: false) unless @graphql_object_landing_page.nil?

if !@graphql_query_landing_page.nil? && !has_query
write_file('operation', 'query', @graphql_query_landing_page, trim: false)
end
write_file('operation', 'query', @graphql_query_landing_page, trim: false) if !@graphql_query_landing_page.nil? && !has_query

unless @graphql_mutation_landing_page.nil?
write_file('operation', 'mutation', @graphql_mutation_landing_page, trim: false)
end
write_file('operation', 'mutation', @graphql_mutation_landing_page, trim: false) unless @graphql_mutation_landing_page.nil?

unless @graphql_interface_landing_page.nil?
write_file('static', 'interface', @graphql_interface_landing_page, trim: false)
end
write_file('static', 'interface', @graphql_interface_landing_page, trim: false) unless @graphql_interface_landing_page.nil?

unless @graphql_enum_landing_page.nil?
write_file('static', 'enum', @graphql_enum_landing_page, trim: false)
end
write_file('static', 'enum', @graphql_enum_landing_page, trim: false) unless @graphql_enum_landing_page.nil?

unless @graphql_union_landing_page.nil?
write_file('static', 'union', @graphql_union_landing_page, trim: false)
end
write_file('static', 'union', @graphql_union_landing_page, trim: false) unless @graphql_union_landing_page.nil?

unless @graphql_input_object_landing_page.nil?
write_file('static', 'input_object', @graphql_input_object_landing_page, trim: false)
end
write_file('static', 'input_object', @graphql_input_object_landing_page, trim: false) unless @graphql_input_object_landing_page.nil?

unless @graphql_scalar_landing_page.nil?
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false)
end
write_file('static', 'scalar', @graphql_scalar_landing_page, trim: false) unless @graphql_scalar_landing_page.nil?

unless @graphql_directive_landing_page.nil?
write_file('static', 'directive', @graphql_directive_landing_page, trim: false)
end
write_file('static', 'directive', @graphql_directive_landing_page, trim: false) unless @graphql_directive_landing_page.nil?

if @options[:use_default_styles]
assets_dir = File.join(File.dirname(__FILE__), 'layouts', 'assets')
Expand All @@ -117,23 +97,23 @@ def generate
def create_graphql_query_pages
graphql_operation_types.each do |query_type|
metadata = ''
if query_type[:name] == graphql_root_types['query']
unless @options[:landing_pages][:query].nil?
query_landing_page = @options[:landing_pages][:query]
query_landing_page = File.read(query_landing_page)
if has_yaml?(query_landing_page)
pieces = yaml_split(query_landing_page)
pieces[2] = pieces[2].chomp
metadata = pieces[1, 3].join("\n")
query_landing_page = pieces[4]
end
query_type[:description] = query_landing_page
next unless query_type[:name] == graphql_root_types['query']

unless @options[:landing_pages][:query].nil?
query_landing_page = @options[:landing_pages][:query]
query_landing_page = File.read(query_landing_page)
if yaml?(query_landing_page)
pieces = yaml_split(query_landing_page)
pieces[2] = pieces[2].chomp
metadata = pieces[1, 3].join("\n")
query_landing_page = pieces[4]
end
opts = default_generator_options(type: query_type)
contents = @graphql_operations_template.result(OpenStruct.new(opts).instance_eval { binding })
write_file('operation', 'query', metadata + contents)
return true
query_type[:description] = query_landing_page
end
opts = default_generator_options(type: query_type)
contents = @graphql_operations_template.result(OpenStruct.new(opts).instance_eval { binding })
write_file('operation', 'query', metadata + contents)
return true
end
false
end
Expand Down Expand Up @@ -229,7 +209,7 @@ def write_file(type, name, contents, trim: true)
FileUtils.mkdir_p(path)
end

if has_yaml?(contents)
if yaml?(contents)
# Split data
meta, contents = split_into_metadata_and_contents(contents)
@options = @options.merge(meta)
Expand Down
24 changes: 11 additions & 13 deletions lib/graphql-docs/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Helpers

def slugify(str)
slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-')
slug.gsub!(%r!^\-|\-$!i, '')
slug.gsub!(/^\-|\-$/i, '')
slug.downcase
end

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

def markdownify(string)
return '' if string.nil?

type = @options[:pipeline_config][:context][:unsafe] ? :UNSAFE : :DEFAULT
::CommonMarker.render_html(string, type).strip
end
Expand Down Expand Up @@ -67,27 +68,23 @@ def graphql_directive_types
end

def split_into_metadata_and_contents(contents, parse: true)
opts = {}
pieces = yaml_split(contents)
if pieces.size < 4
raise RuntimeError.new(
"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.",
)
end
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

# Parse
begin
if parse
meta = YAML.load(pieces[2]) || {}
else
meta = pieces[2]
end
meta = if parse
YAML.safe_load(pieces[2]) || {}
else
pieces[2]
end
rescue Exception => e # rubocop:disable Lint/RescueException
raise "Could not parse YAML for #{name}: #{e.message}"
end
[meta, pieces[4]]
end

def has_yaml?(contents)
def yaml?(contents)
contents =~ /\A-{3,5}\s*$/
end

Expand All @@ -114,6 +111,7 @@ def helper_methods

Helpers.instance_methods.each do |name|
next if name == :helper_methods

@helper_methods[name] = method(name)
end

Expand Down
Loading

0 comments on commit 929e533

Please sign in to comment.