Skip to content

A collection of common GraphQL sources for working with Ruby on Rails.

License

Notifications You must be signed in to change notification settings

ksylvest/graphql-sources

Repository files navigation

GraphQL::Sources

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add graphql-sources

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install graphql-sources

The GraphQL::Dataloader plugin must be installed in the schema:

class AppSchema < GraphQL::Schema
  use GraphQL::Dataloader
  # ...
end

Usage

Loading belongs_to Associations

class Purchase < ActiveRecord::Base
  belongs_to :visitor
end
class Customer < ActiveRecord::Base
  has_many :purchases
end
class PurchaseType < GraphQL::Schema::Object
  field :customer, [CustomerType], null: false

  def customer
    dataloader
      .with(GraphQL::Sources::ActiveRecordObject, ::Profile, key: :id)
      .load(object.customer_id)
  end
end

Loading has_one Associations

class Profile < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  has_one :profile
end
class UserType < GraphQL::Schema::Object
  field :profile, [ProfileType], null: false

  def profile
    dataloader
      .with(GraphQL::Sources::ActiveRecordObject, ::Profile, key: :user_id)
      .load(object.id)
  end
end
SELECT "profiles".*
FROM "profiles"
WHERE "profiles"."user_id" IN (...)
ORDER BY "profiles"."id"

Loading has_many Associations

class User
  has_many :comments
end
class Comment
  belongs_to :user
end
class UserType < GraphQL::Schema::Object
  field :comments, [CommentType], null: false

  def comments
    dataloader
      .with(GraphQL::Sources::ActiveRecordCollection, ::Comment, key: :user_id)
      .load(object.id)
  end
end
SELECT "comments".*
FROM "comments"
WHERE "comments"."user_id" IN (...)
ORDER BY "comments"."id"

Loading has_one_attached Associations

class User
  has_one_attached :avatar
end
class UserType < GraphQL::Schema::Object
  field :avatar, AttachedType, null: false

  def avatar
    dataloader
      .with(GraphQL::Sources::ActiveStorageHasOneAttached, :avatar)
      .load(object)
  end
end

Loading has_many_attached Associations

class User
  has_many_attached :photos
end
class UserType < GraphQL::Schema::Object
  field :photos, [AttachedType], null: false

  def photos
    dataloader
      .with(GraphQL::Sources::ActiveStorageHasManyAttached, :photos)
      .load(object)
  end
end

Loading Counts

class Like
  belongs_to :post
end
class Post
  has_many :likes
end
class PostType < GraphQL::Schema::Object
  field :likes, Integer, null: false

  def likes
    dataloader
      .with(GraphQL::Sources::ActiveRecordCount, ::Like, key: :post_id)
      .load(object.id)
  end
end
SELECT "likes"."post_id", COUNT(*)
FROM "likes"
WHERE "likes"."post_id" IN (1, 2, 3, ...)
GROUP BY "likes"."post_id"

Loading with Rails.cache

class UserType < GraphQL::Schema::Object
  field :location, String, null: false

  def location
    dataloader
      .with(GraphQL::Sources::RailsCache)
      .load(key: "geocode:#{object.latest_ip}", fallback: -> { Geocode.for(object.latest_ip) })
  end
end

Status

CircleCI Maintainability Test Coverage

License

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

About

A collection of common GraphQL sources for working with Ruby on Rails.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages