Class: Graphql::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/graphql/install_generator.rb

Overview

Add GraphQL to a Rails app with rails g graphql:install.

Setup a folder structure for GraphQL:

- app/
  - graphql/
    - resolvers/
    - types/
      - query_type.rb
    - loaders/
    - mutations/
    - {app_name}_schema.rb

(Add .gitkeeps by default, support --skip-keeps)

Add a controller for serving GraphQL queries:

app/controllers/graphql_controller.rb

Add a route for that controller:

# config/routes.rb
post "/graphql", to: "graphql#execute"

Accept a --relay option which adds The root node(id: ID!) field.

Accept a --batch option which adds GraphQL::Batch setup.

Use --no-graphiql to skip graphiql-rails installation.

Constant Summary collapse

GRAPHIQL_ROUTE =
<<-RUBY
if Rails.env.development?
    mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
  end
RUBY

Instance Method Summary collapse

Instance Method Details

#create_folder_structureObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/generators/graphql/install_generator.rb', line 78

def create_folder_structure
  create_dir("app/graphql/mutations")
  create_dir("app/graphql/types")
  template("query_type.erb", "app/graphql/types/query_type.rb")
  template("schema.erb", "app/graphql/#{schema_name.underscore}.rb")
  template("graphql_controller.erb", "app/controllers/graphql_controller.rb")
  route('post "/graphql", to: "graphql#execute"')

  if !options[:skip_graphiql]
    gem("graphiql-rails", group: :development)
    route(GRAPHIQL_ROUTE)
  end

  if options[:batch]
    gem("graphql-batch")
    create_dir("app/graphql/loaders")
  end
end