
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
graphql-active_record_batcher
Advanced tools
GraphQL::ActiveRecordBatcher
is a toolkit to batch record loading as well as preload
ActiveRecord association durings GraphQL Execution.
It is meant to be used with the graphql
gem and uses graphql-batch
under the hood to
preload and batch calls to your database.
Add this line to your application's Gemfile:
gem 'graphql-active_record_batcher'
And then execute:
$ bundle
Or install it yourself as:
$ gem install graphql-active_record_batcher
Schema = GraphQL::Schema.define do
# Enable preloading on a per-schema basis
enable_active_record_batching
query Query
mutation Mutation
end
Using GraphQL without preloading associations results in under performant API and
too many queries to the database. Take for example a Movie type which has a characters
associations, and this GraphQL query:
query {
movie(id: "1") {
characters {
name
}
}
movie(id: "2") {
characters {
name
}
}
}
This query would result in 4 calls to your database:
1: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 1]
2: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 2]
3: SELECT "characters".* FROM "characters" WHERE "characters"."movie_id" = ? [["movie_id", 1]
4: SELECT "characters".* FROM "characters" WHERE "characters"."movie_id" = ? [["movie_id", 2]
GraphQL::ActiveRecordBatcher
lets you preload associations by using the preload
definition during field definition:
StarWarsMovie = GraphQL::ObjectType.define do
name "StarWarsMovie"
description "A StarWars Movie"
# Define which active record model represents
# the parent object
model Movie
field :characters, !types[!Character] do
preloads :characters
resolve ->(movie, _, _) { movie.characters }
end
end
Associations will now be preloaded and only 3 queries are used this time:
1: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 1]
2: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 2]
3: SELECT "characters".* FROM "characters" WHERE "characters"."movie_d" IN (1, 2)
If a certain call loads more than one association, you can use an array to express the ones to preload.
field :characters, !types[!Character] do
preloads [:characters, :planets]
resolve ->(movie, _, _) { movie.characters }
end
node
fieldThe gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that graphql-active_record_batcher 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 clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.