Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GraphQL endpoint to be conditionally included. Closes #209 #210

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ bin/rails generate solidus_graphql_api:install
```

Unlike the REST API which has a variety of endpoints, the GraphQL API has a
single endpoint accessible under `/graphql`.
single endpoint accessible under `/graphql`. It will be automatically
inserted into your `routes.rb` file after running the generator.

For example in development you can use:

Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

Spree::Core::Engine.routes.draw do
post '/graphql', to: 'graphql#execute'
end
SolidusGraphqlApi::Engine.routes.draw do
waiting-for-dev marked this conversation as resolved.
Show resolved Hide resolved
post '/', to: 'graphql#execute'
end
15 changes: 15 additions & 0 deletions lib/generators/solidus_graphql_api/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module Generators
class InstallGenerator < Rails::Generators::Base
class_option :auto_run_migrations, type: :boolean, default: false
source_root File.expand_path('templates', __dir__)
argument :app_path, type: :string, default: Rails.root

MOUNT_GRAPHQL_ENGINE = "mount SolidusGraphqlApi::Engine, at: '/graphql'"

def copy_initializer
template 'initializer.rb', 'config/initializers/solidus_graphql_api.rb'
Expand All @@ -22,6 +25,18 @@ def run_migrations
puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
end
end

def install_routes
if Pathname(app_path).join('config', 'routes.rb').read.include? MOUNT_GRAPHQL_ENGINE
say_status :route_exist, MOUNT_GRAPHQL_ENGINE, :blue
else
route <<~RUBY
# This line installs graphql's main route to execute queries

mount SolidusGraphqlApi::Engine, at: '/graphql'
RUBY
end
end
end
end
end