1

I'm using Rails 6 with Grape as API. I'm pretty new in Grape and I'm trying to learn how to add new endpoint using Grape. The idea is to get Index endpoint which is nested in v1/users/index

Here is my structure:

app/
  controllers/
    api/
      root.rb           - API::Root
      v1/
        base.rb         - API::V1::Base
        users/
             base.rb    - API::V1::Users::Base
             index.rb   - API::V1::Users::Index

api/root.rb

module API
  class Root < Grape::API
    default_format :json
    prefix :api

    # exception handling
    include Rescuers

    # helpers
    helpers ::API::Helpers::ParamsHelper

    # core API modules
    mount V1::Base
  end
end

api/v1/base.rb:

module API
  module V1
    class Base < Root
      version 'v1', using: :path
      content_type :json, 'application/vnd.api+json'

      # mount resource modules
      mount V1::Users::Base
    end
  end
end

api/v1/users/base.rb:

module API
  module V1
    module Users
      class Base < Grape::API
        version 'v1', using: :path
        content_type :json, 'application/vnd.api+json'

        # mount resource modules
        mount Users::Index
      end
    end
  end
end

api/v1/users/index.rb:

module API
  module V1
    module Users
      class Index < Grape::API
        desc 'Test'

        get do
          head 200
        end
      end
    end
  end
end

Here are my routes:

Rails.application.routes.draw do

  # API
  scope :api do
    mount API::Root, at: '/'
  end
end

I want to have this index.rb in my routes as GET v1/users/index but when I type rake routes I don't see it. This should not do anything, I want to understand what is the core requirements when it comes to creation endpoint with Grape.

mr_muscle
  • 2,536
  • 18
  • 61
  • 1
    `rake routes` is a rails rake task, that won't work with grape, you need something like this https://stackoverflow.com/a/44657601/5215927 – mr_sudaca May 26 '20 at 13:13
  • I don't think that's the case - when I tap `rails routes` I still see `api_root /api. API::Root` which is also grape routes – mr_muscle May 26 '20 at 13:20
  • 1
    yes, that's the "base" route for the api, but in order to retrieve the grape routes you need another script, maybe you can use this `https://github.com/syedmusamah/grape_on_rails_routes` – mr_sudaca May 26 '20 at 13:28
  • Isn't the purpose of Grape that you only have one mapping in `routes.rb` to the api, and then add extra routes in Grape – Eyeslandic May 26 '20 at 13:31
  • We have a very extensive API infrastructure and we use `grape_on_rails_routes` as @mr_sudaca suggested – engineersmnky May 26 '20 at 16:26
  • I would recommend not to use mounts of Grape. If you want to split your endpoints, use native Ruby modules https://nesteryuk.info/2020/04/12/grape-include-is-still-better-for-code-splitting.html – Dmytro Nesteriuk May 26 '20 at 16:45
  • @DmitriyNesteryuk that works fine for simple cases however mounting allows for a lot more configuration and also creates much DRYer mechanisms for redundant mechanisms like CRUD. [mount configuration](https://github.com/ruby-grape/grape#mount-configuration) – engineersmnky May 26 '20 at 17:16

0 Answers0