Skip to content

Commit

Permalink
Merge pull request #49 from gjtorikian/accept-a-schema
Browse files Browse the repository at this point in the history
Accept a schema
  • Loading branch information
gjtorikian committed May 11, 2018
2 parents ac1a639 + 9597cda commit 4b3e128
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
/test/graphql-docs/fixtures/output/
/.sass-cache/
/output/
.byebug_history
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion graphql-docs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions lib/graphql-docs.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rubocop:disable Style/FileName
# rubocop:disable Naming/FileName
require 'graphql-docs/helpers'
require 'graphql-docs/renderer'
require 'graphql-docs/configuration'
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/graphql-docs/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
2 changes: 1 addition & 1 deletion test/graphql-docs/graphql-docs_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rubocop:disable Style/FileName
# rubocop:disable Naming/FileName
require 'test_helper'

class GraphQLDocsTest < Minitest::Test
Expand Down
24 changes: 24 additions & 0 deletions test/graphql-docs/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b3e128

Please sign in to comment.