
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
graphql_preload_queries
Advanced tools
This gem permits your graphql application to define association preloads to improve app performance by removing N+1 query issues.
Object Type
class UserType < Types::BaseObject
field :id, Int, null: true
field :name, String, null: true
field :parents, [Types::UserType], null: false, preload: true
field :friends, [Types::UserType], null: false, preload: :user_friends
end
preload:
accepts:
true
: Will use field key as the association namefield :parents, ..., preload: true
will preload parents
associationSymbol
: Custom association namefield :friends, ..., preload: :user_friends
will preload user_friends
associationString
: Tied associationsfield :excluded_friends, ..., preload: 'excluded_friends.user'
will preload excluded_friends -> user
associationHash
: Deep preload definitionsfield :best_friends, ..., preload: { preload: :user_friends, parents: :parents }'
Will preload user_friends
and user_friends.parents
only if query includes inner definition, like user(id: 10) { bestFriends { id parents { ... } } }
Will not preload user_friends.parents
if query does not include inner definition, like user(id: 10) { bestFriends { id } }
Preloads in query results
BEFORE
# queries/users.rb
def users(ids:)
users = User.where(id: ids)
end
Does not apply preloads to the root query.
AFTER
def users(ids:)
user = include_gql_preloads(User.where(id: id))
end
Root query applies all defined preloads
include_gql_preloads(collection, query_key: nil, type_klass: nil)
: Will include all preloads configured in type_klass
(UserType) based on the gql query.
collection
(ActiveRecordCollection) Query resultsquery_key
(String | Sym, default: method name) Field result keytype_klass:
(GQL TypeClass, default: calculates using query_key)Preloads in mutation results
# mutations/users/disable.rb
#...
field :users, [Types::UserType], null: true
def resolve(ids:)
affected_users = User.where(id: ids)
affected_users = include_gql_preloads(affected_users, query_key: :users)
{ users: affected_users }
end
include_gql_preloads(collection, query_key: , type_klass: nil)
: Will include all preloads configured in type_klass
(UserType) based on the gql query.
collection
(ActiveRecordCollection) Query resultsquery_key
(String | Sym) Field result keytype_klass:
(GQL TypeClass, default: calculates using query_key)Add this line to your application's Gemfile:
gem 'graphql_preload_queries'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install graphql_preload_queries
For debugging mode:
# config/initializers/gql_preload.rb
GraphqlPreloadQueries::DEBUG = true
Bug reports and pull requests are welcome on GitHub at https://github.com/owen2345/graphql_preload_queries. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that graphql_preload_queries 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.