ruby-grape / ruby-grape/grape

Graphql like implementation and food for thought

Open
#1,696 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discuss! feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

In Grape Entity there is the only method that lets you declare what attributes you want in your response
This is hardcoded by the endpoint and it is not configured by the client.

Bundle::Entity.represent( @bundle, only: [:id, jobs: [:id, country: :id]] )

What if the client asks what attributes he needs in his response?
Pass the attributes you need in your response as parameters and get back only what you need so you have an graphql like support with the minimum effort!

How this can be done?

  • In each Grape::Formatter select only the __only__ attributes

Why you dont pass the __only__ to Entity's only?

  • Because you are limited only when you use an Entity and most of endpoinds can return a hash, array etc

This is just a food for thought and I would like to know what the pros and cons of this implementation

An Implementation Example:

module Grape
  module Formatter
    module Json
      class << self
        def call(object, _env)
          only = _env[Grape::Env::RACK_REQUEST_QUERY_HASH]["__only__"]

          if object.respond_to?(:to_json)
            return only.present? ? object.to_json(only: only) : object.to_json
          end
          
         MultiJson.dump(object)
        end
      end
    end
  end
end

Create params from a hash:

[:id, jobs: [:id, country: :id]].to_query('__only__')

It will generate:

#=> "__only__%5B%5D=id&__only__%5B%5D%5Bjobs%5D%5B%5D=id&__only__%5B%5D%5Bjobs%5D%5B%5D%5Bcountry%5D=id"

Request with the __only__ params

http://localhost:9292/bundles/68?only%5B%5D=id&only%5B%5D%5Bjobs%5D%5B%5D=id&only%5B%5D%5Bjobs%5D%5B%5D%5Bcountry%5D=id

Get back only the attributes you need

{
"id": 68,
  "jobs": [
   { "id": 11743,
   "country": {  "id": 1} }
  ]
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Grape::Formatter implementations, especially Grape::Formatter::Json#call, alongside Grape Entity's only handling and the documented __only__ query examples. The issue needs an agreed scope for supported response types, parameter parsing, and formatter behavior before implementation; done would include tests defining the accepted nested selection syntax and returned fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.