Skip to content

Instantly share code, notes, and snippets.

@msroot
Created October 25, 2017 00:13
Show Gist options
  • Save msroot/6a6457092663ed815d250f1e5aa99274 to your computer and use it in GitHub Desktop.
Save msroot/6a6457092663ed815d250f1e5aa99274 to your computer and use it in GitHub Desktop.
quick graphql implementation for grape gem
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
# a = [:id, jobs: [:id, country: :id]].to_query('__only__')
# => "__only__%5B%5D=id&__only__%5B%5D%5Bjobs%5D%5B%5D=id&__only__%5B%5D%5Bjobs%5D%5B%5D%5Bcountry%5D=id"
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment