Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Active record preload #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gem 'puma', '~> 3.0'
# Use GraphQL!
gem 'graphql', github: 'rmosolgo/graphql-ruby', ref: 'a655714240c59e86c84bb22b82d776be02eddb8f'
gem 'graphql-batch'
gem 'graphql-active_record_batcher'
# GraphiQL Interface
gem 'graphiql-rails', '~> 1.4.1'

Expand Down
14 changes: 9 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ GEM
activesupport (>= 4.1.0)
graphiql-rails (1.4.1)
rails
graphql-active_record_batcher (0.3.1)
activerecord
graphql
graphql-batch
promise.rb
graphql-batch (0.3.1)
graphql (>= 0.8, < 2)
promise.rb (~> 0.7.2)
i18n (0.8.0)
json (2.0.2)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -129,9 +133,8 @@ GEM
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.7.2)
execjs (>= 0.3.0)
json (>= 1.8.0)
uglifier (3.0.4)
execjs (>= 0.3.0, < 3)
web-console (3.4.0)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -149,6 +152,7 @@ DEPENDENCIES
byebug
graphiql-rails (~> 1.4.1)
graphql!
graphql-active_record_batcher
graphql-batch
listen (~> 3.0.5)
pg
Expand All @@ -162,4 +166,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.14.3
1.14.4
5 changes: 2 additions & 3 deletions app/models/graph/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Graph
Schema = GraphQL::Schema.define do
enable_active_record_batching

query Graph::Types::Query
mutation Graph::Mutations::Mutation

Expand All @@ -23,8 +25,5 @@ module Graph
model = Object.const_get(gid.model_name)
Graph::FindLoader.for(model).load(gid.model_id.to_i)
end

lazy_resolve(Promise, :sync)
instrument(:query, GraphQL::Batch::Setup)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your gem includes the right setup? What happens if instrument(:query, GraphQL::Batch::Setup) is included twice by accident? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does a NestedError 🤔. I've been trying to find a way to raise a useful error but since it's order dependant it's kinda hard :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if I could handle it better in the gem: use a Set instead of an Array to store instrumenters, that way a second call would do nothing.

end
11 changes: 8 additions & 3 deletions app/models/graph/types/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Types
Person = GraphQL::ObjectType.define do
name "Person"
description "An individual person or character within the Star Wars universe."
model ::Person

interfaces [GraphQL::Relay::Node.interface]

Expand Down Expand Up @@ -33,11 +34,15 @@ module Types
property: :hair_color

field :height, types.Int, "The height of the person in centimeters."

field :homeworld, Planet, "A planet that this person was born on or inhabits." do
resolve -> (person, _, _) do
Graph::AssociationLoader.for(::Person, :homeworld).load(person)
end
preloads :homeworld
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat :)

end

field :species, Species, "The species of this person." do
preloads :species
end

field :mass, types.Int, "The mass of the person in kilograms."
field :name, !types.String, "The name of this person."
field :skinColor, types.String, "The skin color of this person.", property: :skin_color
Expand Down
11 changes: 9 additions & 2 deletions app/models/graph/types/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ module Types
Rating = GraphQL::ObjectType.define do
name "Rating"
description "A rating made by a user on a film."
model ::Rating

interfaces [GraphQL::Relay::Node.interface]

field :user, !Graph::Types::User, "The critic."
field :film, !Graph::Types::Film, "The rated film."
field :user, !Graph::Types::User, "The critic." do
preloads :user
end

field :film, !Graph::Types::Film, "The rated film." do
preloads :film
end

field :rating, !types.Int, "A film rating from 0 to 5."
field :createdAt, !types.String,
"The ISO 8601 date format of the time that this resource was created.", property: :created_at
Expand Down
5 changes: 2 additions & 3 deletions app/models/graph/types/species.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Types
Species = GraphQL::ObjectType.define do
name "Species"
description "A type of person or character within the Star Wars Universe."
model ::Species

interfaces [GraphQL::Relay::Node.interface]

Expand Down Expand Up @@ -51,9 +52,7 @@ module Types

field :language, types.String, "The language commonly spoken by this species."
field :homeworld, Graph::Types::Planet, "A planet that this species originates from type." do
resolve -> (species, _, _) do
Graph::AssociationLoader.for(::Species, :homeworld).load(species)
end
preloads :homeworld
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions db/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ type Person implements Node {
# A planet that this person was born on or inhabits.
homeworld: Planet

# The species of this person.
species: Species

# The mass of the person in kilograms.
mass: Int

Expand Down