Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
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.
Add this line to your application's Gemfile:
gem 'graphql-eager_load'
And then execute:
$ bundle install
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
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:
version.rb
Bug reports and pull requests are welcome on GitHub at https://github.com/hoverinc/graphql-eager-load.
MIT
FAQs
Unknown package
We found that graphql-eager_load demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.