lib/generators/graphql/install_generator.rb in graphql-1.6.4 vs lib/generators/graphql/install_generator.rb in graphql-1.6.5

- old
+ new

@@ -1,7 +1,8 @@ # frozen_string_literal: true require 'rails/generators/base' +require_relative 'core' module Graphql module Generators # Add GraphQL to a Rails app with `rails g graphql:install`. # @@ -38,10 +39,12 @@ # # Accept a `--batch` option which adds `GraphQL::Batch` setup. # # Use `--no-graphiql` to skip `graphiql-rails` installation. class InstallGenerator < Rails::Generators::Base + include Core + desc "Install GraphQL folder structure and boilerplate code" source_root File.expand_path('../templates', __FILE__) class_option :schema, type: :string, @@ -56,10 +59,15 @@ class_option :skip_graphiql, type: :boolean, default: false, desc: "Skip graphiql-rails installation" + class_option :skip_mutation_root_type, + type: :boolean, + default: false, + desc: "Skip creation of the mutation root type" + class_option :relay, type: :boolean, default: false, desc: "Include GraphQL::Relay installation" @@ -79,14 +87,19 @@ mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql" end RUBY def create_folder_structure - create_dir("app/graphql/mutations") create_dir("app/graphql/types") + template("schema.erb", schema_file_path) + + # Note: Yuo can't have a schema without the query type, otherwise introspection breaks template("query_type.erb", "app/graphql/types/query_type.rb") - template("schema.erb", "app/graphql/#{schema_name.underscore}.rb") + insert_root_type('query', 'QueryType') + + create_mutation_root_type unless options.skip_mutation_root_type? + template("graphql_controller.erb", "app/controllers/graphql_controller.rb") route('post "/graphql", to: "graphql#execute"') if options[:batch] gem("graphql-batch") @@ -118,27 +131,9 @@ end def gem(*args) @gemfile_modified = true super(*args) - end - - def create_dir(dir) - empty_directory(dir) - if !options[:skip_keeps] - create_file("#{dir}/.keep") - end - end - - def schema_name - @schema_name ||= begin - if options[:schema] - options[:schema] - else - require File.expand_path("config/application", destination_root) - "#{Rails.application.class.parent_name}Schema" - end - end end end end end