Rails GraphQL path mount differently for public and private query/mutations - ruby-on-rails

I have a Ruby on Rails app and I have integrated Graphql. As I saw on the documentation we have to mount it to a controller that will handle query and mutations.
I currently have two controllers. One that require authorization (for private query/mutations that require an authenticated user) and another one to handle public mutations like (signup).
I want to create two routes for my GraphQL ide. So far I have been serving my private query/mutations to the endpoint /graphiql.
I want to create another one, like /graphiql/public that will serve my public query/mutations
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "graphql"
post "/graphql", to: "graphql#execute"
And I also want to add something like:
mount GraphiQL::Rails::Engine, at: "/graphiql/public", graphql_path: "/graphql/public"
post "/graphql/public", to: "graphql#execute_public"
Rails does not let me mount GraphiQL::Rails::Engine two times becasue it gets confused which one to use.
Please help me how to handle this situation.

Related

How to list directory folders in rails

Hi I am newbie in rails and wanted to know any way to list folders and files for a url say public . So example user types example.com/public he gets all folders and files listed as we do in php if we start a wamp server . I need this so I can create files and have their url shared to public i.e. simply send url link online like example.com/public/test.pdf . Currently I am getting page with no routes defined .enter image description here
Thanks.
Create a controller to serve the file using a route parameter, ex.
get '/public/:filename', to: 'file#serve'
Then use the send_file method in your controller.
class FileController < ApplicationController
def serve
# For authorization:
#authorize!
# For authentication: (a way or another)
#redirect_to :access_denied unless user_signed_in?
send_file "/www/yourapp/var/files/#{params[:filename]}"
end
end
This way the file can be anywhere within your app, or even on a Cloud storage. It also gives you the ability to use authentication or authorization to check if the user has access.
Please note that this code is very simple, but there are much more options like Fog gem for Cloud storage and everything else.
See https://apidock.com/rails/ActionController/Streaming/send_file for more information.

Rails file location and access

My root is site/home/ubuntu/workspace/
Is it possible to access file (from browser by HTTP request) located inside workspace/ without configuring routes and controllers?
Does the question on 1) depend on file extension?
you can access path to your file like this:
File.expand_path("somestuff.rb", "~/workspace")
for me this code produces path as follows:
"/home/foodie/workspace/somestuff.rb"
I have this structure:
# /home/username/Workspace/rails_project/app/controllers/application_controller.rb
require "#{Rails.root}/../test.rb
And
# /home/username/Workspace/test.rb
# Some ruby code
As you can see, test.rbfile is outside RubyOnRails Project.
For security reason, you can not access a file outside project rails from URL without defining a route. What you can do, is point a route to controller that, based on file name provided at url, require a file outside rails project.
# /home/username/Workspace/rails_project/config/routes.rb
get '/get_file/:file_name', to: 'files#show'
# /home/username/Workspace/rails_project/app/controllers/files_controller.rb
class FilesController < ApplicationController
def show
document = params[:file_name]
send_data "#{Rails.root}/../#{document}, filename: document
end
end
For more info, see:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
Without configuring routes and controllers, client can only access files in the public/ directory (it doesn't matter what the extension is). Bare in mind this: when your Rails app is run by webserver, its webroot will be the public directory, consequently to access public/file.ext you request should be webroot/file.ext

Accessing engine routes from Rails.application.routes.url_helpers

I would like to know how to access engine routes by using Rails.application.routes.url_helpers.
I have a factory object that creates a string including a dynamically generated url. Currently, I can generate urls by using Rails.application.routes.url_helpers.(INSERT PATH NAME).
However, it can only access the routes in the main app. I cannot access the routes of engines mounted on the main app.
Things I have tried
I've tried to use Rails.application.routes.engine_name where engine_name is the name under which the engine is mounted on the main app.
I've tried to use MyEngine::Engine.routes.url_helpers to access the routes in the Engine. It works, but I would like to use Rails.application.routes.url_helpers because there are many factory objects like this one, and they are all inheriting from the superclass that delgates url_helper to Rails.application.routes.
Any suggestions? Let me know if any clarification is needed.
You have to use engine route proxy method.
From your example call the url helper using the following syntax as example:
my_engine_engine.articles_path
To rename the proxy method helper, just edit the routes config file inside your rails application:
mount MyEngine::Engine => '/app', :as => 'my_engine'
so you can now call the previous example:
my_engine.articles_path
Assuming that your engine namespace is EngineName, it should be:
EngineName::Engine.routes.url_helpers.some_engine_path

Implementing hypermedia-driven API with Grape (or Sinatra)

I'm trying to implement a hypermedia-driven API using Grape mounted directly on top of Rack. Grape supports presenters ("entities") which
seem to be the proper place for providing all related hypermedia.
If I had Rails router available, I could simply pick route by its ears and toss it into my presenter logic. For example (ROAR approach):
link :self do
article_url(self)
end
But Grape itself doesn't provide easy access to routes, as they have no names or aliases akin to article_url.
Has anybody encountered a similar problem with Grape or Sinatra? Is there a clean and simple way of exposing resource links?
This is possible, but not quite as simply as the Rails url helpers.
From https://github.com/intridea/grape#describing-and-inspecting-an-api:
TwitterAPI::versions # yields [ 'v1', 'v2' ]
TwitterAPI::routes # yields an array of Grape::Route objects
TwitterAPI::routes[0].route_version # yields 'v1'
TwitterAPI::routes[0].route_description # etc.

Prepend path prefix to all rails routes

I have a setup where nginx serves a rails application inside a specific subfolder
eg. http://myserver/railsapp/ and everything inside gets proxied to rails, if the first subfolder is different, it servers static files from another folder.
I haven't been able to find how to specify this behaviour in rails in an intelligent way. I mean, what I want is to specify an option like Rails.server_prefix = /railsapp so that all the routes get prepended automagically, both on the incoming requests and on the generated links.
You probably want to use the router's scope method with the :path argument:
Rails.application.routes do
scope(:path => '/railsapp') do
# the rest of your routes go here
end
end
See the docs for more info.

Resources