Skip to content

Commit

Permalink
Merge pull request #66 from gjtorikian/support-erb-landings
Browse files Browse the repository at this point in the history
Support ERB templates for landing pages
  • Loading branch information
gjtorikian committed Jan 4, 2019
2 parents 8ac6d63 + c07bd1f commit 2cbd789
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ GraphQLDocs.build(options)

If your `render` method returns `nil`, the `Generator` will not attempt to write any HTML file.

### Templates

The layouts for the individual GraphQL pages are ERB templates, but you can also use ERB templates for your static landing pages.

If you want to add additional variables for your landing pages, you can add define a `variables` hash within the `landing_pages` option.

### Helper methods

In your ERB layouts, there are several helper methods you can use. The helper methods are:
Expand Down
4 changes: 3 additions & 1 deletion lib/graphql-docs/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ module Configuration
enum: "#{File.dirname(__FILE__)}/landing_pages/enum.md",
union: "#{File.dirname(__FILE__)}/landing_pages/union.md",
input_object: "#{File.dirname(__FILE__)}/landing_pages/input_object.md",
scalar: "#{File.dirname(__FILE__)}/landing_pages/scalar.md"
scalar: "#{File.dirname(__FILE__)}/landing_pages/scalar.md",

variables: {} # only used for ERB landing pages
},

classes: {
Expand Down
18 changes: 17 additions & 1 deletion lib/graphql-docs/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,23 @@ def initialize(parsed_schema, options)
elsif !File.exist?(@options[:landing_pages][sym])
raise IOError, "`#{sym}` landing page #{@options[:landing_pages][sym]} was not found"
end
instance_variable_set("@graphql_#{sym}_landing_page", File.read(@options[:landing_pages][sym]))

landing_page_contents = File.read(@options[:landing_pages][sym])
metadata = ''

if File.extname((@options[:landing_pages][sym])) == '.erb'
opts = @options.merge(@options[:landing_pages][:variables]).merge(helper_methods)
if has_yaml?(landing_page_contents)
metadata, landing_page = split_into_metadata_and_contents(landing_page_contents, parse: false)
erb_template = ERB.new(landing_page)
else
erb_template = ERB.new(landing_page_contents)
end

landing_page_contents = erb_template.result(OpenStruct.new(opts).instance_eval { binding })
end

instance_variable_set("@graphql_#{sym}_landing_page", metadata + landing_page_contents)
end
end

Expand Down
8 changes: 6 additions & 2 deletions lib/graphql-docs/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def graphql_scalar_types
@parsed_schema[:scalar_types] || []
end

def split_into_metadata_and_contents(contents)
def split_into_metadata_and_contents(contents, parse: true)
opts = {}
pieces = yaml_split(contents)
if pieces.size < 4
Expand All @@ -72,7 +72,11 @@ def split_into_metadata_and_contents(contents)
end
# Parse
begin
meta = YAML.load(pieces[2]) || {}
if parse
meta = YAML.load(pieces[2]) || {}
else
meta = pieces[2]
end
rescue Exception => e # rubocop:disable Lint/RescueException
raise "Could not parse YAML for #{name}: #{e.message}"
end
Expand Down
6 changes: 6 additions & 0 deletions test/graphql-docs/fixtures/landing_pages/object.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Variable Objects
---
# Objects

<%= some_var %> in GraphQL represent the resources that you can access.
15 changes: 15 additions & 0 deletions test/graphql-docs/generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ def test_that_missing_landing_pages_are_reported
end
end

def test_that_erb_landing_pages_work
options = deep_copy(GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS)
options[:output_dir] = @output_dir
options[:landing_pages][:object] = File.join(fixtures_dir, 'landing_pages', 'object.erb')
options[:landing_pages][:variables] = { some_var: 'wowie!!' }

generator = GraphQLDocs::Generator.new(@tiny_results, options)
generator.generate

object = File.read File.join(@output_dir, 'object', 'index.html')

assert_match /Variable Objects/, object
assert_match /wowie!!/, object
end

def test_that_broken_yaml_is_caught
options = deep_copy(GraphQLDocs::Configuration::GRAPHQLDOCS_DEFAULTS)
options[:landing_pages][:index] = File.join(fixtures_dir, 'landing_pages', 'broken_yaml.md')
Expand Down

0 comments on commit 2cbd789

Please sign in to comment.