From c07bd1f4caae09a30d222df5f10e3e7514bede8e Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 4 Jan 2019 17:06:30 -0500 Subject: [PATCH] Support ERB templates for landing pages --- README.md | 6 ++++++ lib/graphql-docs/configuration.rb | 4 +++- lib/graphql-docs/generator.rb | 18 +++++++++++++++++- lib/graphql-docs/helpers.rb | 8 ++++++-- .../fixtures/landing_pages/object.erb | 6 ++++++ test/graphql-docs/generator_test.rb | 15 +++++++++++++++ 6 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 test/graphql-docs/fixtures/landing_pages/object.erb diff --git a/README.md b/README.md index 91d0595..61aab0a 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/graphql-docs/configuration.rb b/lib/graphql-docs/configuration.rb index 15ec21a..1fd72e4 100644 --- a/lib/graphql-docs/configuration.rb +++ b/lib/graphql-docs/configuration.rb @@ -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: { diff --git a/lib/graphql-docs/generator.rb b/lib/graphql-docs/generator.rb index af8cd2c..9864243 100644 --- a/lib/graphql-docs/generator.rb +++ b/lib/graphql-docs/generator.rb @@ -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 diff --git a/lib/graphql-docs/helpers.rb b/lib/graphql-docs/helpers.rb index 85edb5b..a69578b 100644 --- a/lib/graphql-docs/helpers.rb +++ b/lib/graphql-docs/helpers.rb @@ -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 @@ -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 diff --git a/test/graphql-docs/fixtures/landing_pages/object.erb b/test/graphql-docs/fixtures/landing_pages/object.erb new file mode 100644 index 0000000..6dfd5c6 --- /dev/null +++ b/test/graphql-docs/fixtures/landing_pages/object.erb @@ -0,0 +1,6 @@ +--- +title: Variable Objects +--- +# Objects + +<%= some_var %> in GraphQL represent the resources that you can access. diff --git a/test/graphql-docs/generator_test.rb b/test/graphql-docs/generator_test.rb index 62e8fe0..71439f4 100644 --- a/test/graphql-docs/generator_test.rb +++ b/test/graphql-docs/generator_test.rb @@ -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')