Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-eager_load

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-eager_load

  • 0.2.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Graphql::EagerLoad

This gem assumes you are using Rails and ActiveRecord. And that your GraphQL types and fields map as closely to your data model as possible. It uses those assumptions, or conventions, to handle N+1 prevention for you.

It does so by traversing your graphql query looking for fields on types that match associations on those types' corresponding models. For each field found to be an association, this gem adds those associations to an ActiveRecord::QueryMethods#includes hash. That hash can then be used to eager load the associations of records returned in your graphql resolver.

Installation

Add this line to your application's Gemfile:

gem 'graphql-eager_load'

And then execute:

$ bundle install

Usage

module Resolvers
  class Users < Resolvers::Base
    eager_load_model ::User

    type Types::User.connection_type, null: false

    def resolve
      User.active.includes(associations_to_eager_load)
    end
  end
end

The .eager_load_model and #associations_to_eager_load methods are provided by this gem.

If your type defines a custom method for a field that maps to an ActiveRecord association, this gem will ignore that association and let your method handle it. If you still want to eager load the association in that case you need to let the gem know.

module Types
  class User < Types::Base
    field :estimates, [Types::Estimate], null: false
    field :profile_photo, Types::File, null: true
    
    def profile_photo
      object.profile_photo if object.profile_photo.attached?
    end
    
    def self.allow_include_builder_fields
      [:profile_photo]
    end
  end
end

Given this query:

users {
  id
  estimates {
    id
  }
  profilePhoto {
    redirectUrl
    filename
  }
}

The output of the #associations_to_eager_load helper method would be {estimates: {}, profile_photo: {blob: {}}. Without the .allow_include_builder_fields class method the output would be {estimates: {}}.

If you have a field that is derived from an association, but the association is not included in the query, you can define a #custom_associations_for_fields method to specify which associations to include for a specific field.

module Types
  class User < Types::Base
    field :estimates, [Types::Estimate], null: false
    field :org_names, [String], null: true
    
    def org_names
      object.orgs.map(&:name)
    end
    
    def self.custom_associations_for_fields
      {
        org_names: [:org]
      }
    end
  end
end

Development

After checking out the repo, run bundle to install dependencies. Then, run bundle exec rake spec to run the tests. You can also run bundle exec bin/console for an interactive prompt that will allow you to experiment.

To release a new version:

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hoverinc/graphql-eager-load.

License

MIT

FAQs

Package last updated on 04 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc