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

graphql-active_record_batcher

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-active_record_batcher

  • 0.3.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

GraphQL::ActiveRecordBatcher

Code Climate

Test Coverage

Issue Count

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.

Installation

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

Usage

Set up preloading for a Schema

Schema = GraphQL::Schema.define do
  # Enable preloading on a per-schema basis
  enable_active_record_batching
  query Query
  mutation Mutation
end

Preloading Associations

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)
Preload multiple associations

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

TODO

  • Expose a way to batch finds
  • Expose a way or documentation on how to batch the node field
  • Accept an array of preloads
  • Accept nested preloads

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 20 Feb 2017

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