diff --git a/.gitignore b/.gitignore index 5f24d06..f09bf1e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ /test/graphql-docs/fixtures/output/ /.sass-cache/ /output/ +.byebug_history diff --git a/README.md b/README.md index e38ba40..796e2ff 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,12 @@ GraphQLDocs.build(filename: filename) # or pass in a string GraphQLDocs.build(schema: contents) + +# or a schema class +schema = GraphQL::Schema.define do + query query_type +end +GraphQLDocs.build(schema: schema) ``` ## Breakdown diff --git a/Rakefile b/Rakefile index 8c39b90..0c74162 100644 --- a/Rakefile +++ b/Rakefile @@ -17,12 +17,15 @@ task default: :test Rake::Task[:test].enhance { Rake::Task[:html_proofer].invoke } desc 'Invoke HTML-Proofer' -task html_proofer: [:generate_sample] do - require 'html-proofer' - output_dir = File.join(File.dirname(__FILE__), 'output') - - proofer_options = { disable_external: true, assume_extension: true } - HTMLProofer.check_directory(output_dir, proofer_options).run +task :html_proofer do + if ENV['CI'] + Rake::Task[:generate_sample].invoke('https://www.gjtorikian.com/graphql-docs') + require 'html-proofer' + output_dir = File.join(File.dirname(__FILE__), 'output') + + proofer_options = { disable_external: true, assume_extension: true } + HTMLProofer.check_directory(output_dir, proofer_options).run + end end desc 'Set up a console' diff --git a/graphql-docs.gemspec b/graphql-docs.gemspec index ae18fc2..0095a0b 100644 --- a/graphql-docs.gemspec +++ b/graphql-docs.gemspec @@ -33,7 +33,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'html-proofer', '~> 3.4' spec.add_development_dependency 'minitest', '~> 5.0' spec.add_development_dependency 'minitest-focus', '~> 1.1' - spec.add_development_dependency 'pry', '~> 0.10.0' + spec.add_development_dependency 'pry-byebug', '~> 3.6' spec.add_development_dependency 'rake' spec.add_development_dependency 'rubocop-github' spec.add_development_dependency 'webmock', '~> 2.3' diff --git a/lib/graphql-docs.rb b/lib/graphql-docs.rb index acecdc7..46921e8 100644 --- a/lib/graphql-docs.rb +++ b/lib/graphql-docs.rb @@ -1,4 +1,4 @@ -# rubocop:disable Style/FileName +# rubocop:disable Naming/FileName require 'graphql-docs/helpers' require 'graphql-docs/renderer' require 'graphql-docs/configuration' @@ -38,8 +38,8 @@ def build(options) schema = File.read(filename) else - unless schema.is_a?(String) - raise TypeError, "Expected `String`, got `#{schema.class}`" + if !schema.is_a?(String) && !schema.is_a?(GraphQL::Schema) + raise TypeError, "Expected `String` or `GraphQL::Schema`, got `#{schema.class}`" end schema = schema diff --git a/lib/graphql-docs/parser.rb b/lib/graphql-docs/parser.rb index 46313b6..a992296 100644 --- a/lib/graphql-docs/parser.rb +++ b/lib/graphql-docs/parser.rb @@ -11,7 +11,12 @@ def initialize(schema, options) @options[:notices] ||= -> (schema_member_path) { [] } - @schema = GraphQL::Schema.from_definition(schema) + if schema.is_a?(GraphQL::Schema) + @schema = schema + else + @schema = GraphQL::Schema.from_definition(schema) + end + @processed_schema = { operation_types: [], mutation_types: [], diff --git a/test/graphql-docs/graphql-docs_test.rb b/test/graphql-docs/graphql-docs_test.rb index 65d8782..089502d 100644 --- a/test/graphql-docs/graphql-docs_test.rb +++ b/test/graphql-docs/graphql-docs_test.rb @@ -1,4 +1,4 @@ -# rubocop:disable Style/FileName +# rubocop:disable Naming/FileName require 'test_helper' class GraphQLDocsTest < Minitest::Test diff --git a/test/graphql-docs/parser_test.rb b/test/graphql-docs/parser_test.rb index a8b43e9..a67b699 100644 --- a/test/graphql-docs/parser_test.rb +++ b/test/graphql-docs/parser_test.rb @@ -8,6 +8,30 @@ def setup @results = @parser.parse end + def test_it_accepts_schema_class + query_type = GraphQL::ObjectType.define do + name 'Query' + + field :test do + type types.Int + description "Title paragraph. + ``` + line1 + line2 + line3 + ```" + end + end + + schema = GraphQL::Schema.define do + query query_type + end + + results = GraphQLDocs::Parser.new(schema, {}).parse + assert_equal 'test', results[:operation_types][0][:fields][0][:name] + assert_equal "Title paragraph.\n ```\n line1\n line2\n line3\n ```", results[:operation_types][0][:fields][0][:description] + end + def test_types_are_sorted names = @results[:object_types].map { |t| t[:name] } assert_equal names.sort, names