rmosolgo/graphql-ruby

Using query params in SQL select

maxrosecollins opened this issue · 2 comments

Is your feature request related to a problem? Please describe.

If you are writing a query in GraphQL to pick specific fields doesn't it make sense to query the same in the database?

We have tables with a lot of columns and we found a huge performance increase when not using SELECT * FROM table and actually specifying the columns we want.

For example

Query

{
  post(id: 1) {
    id
    title
  }
}
module Resolvers
  class PostResolver < BaseResolver
    type Types::PostType, null: false
    argument :id, ID

    def resolve(id:)
      ::Post.find(id)
    end
  end
end

Describe the solution you'd like

module Resolvers
  class PostResolver < BaseResolver
    type Types::PostType, null: false
    argument :id, ID

    def resolve(id:)
      ::Post.select('id, title').find(id)
    end
  end
end

How can I access these params in the resolver?

Hi! Lookahead is built for this. I wrote up a simple example here: https://gist.github.com/rmosolgo/d84fb3e707661e6ea6c6a3c18e5f6084

Please give that approach a try and let me know if you run into any trouble with it!

@rmosolgo Sorry! I will try it out, thanks!