diff --git a/Gemfile b/Gemfile index 00c6abf..27aaae1 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 1b21f03..d294d88 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -149,6 +152,7 @@ DEPENDENCIES byebug graphiql-rails (~> 1.4.1) graphql! + graphql-active_record_batcher graphql-batch listen (~> 3.0.5) pg @@ -162,4 +166,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.14.3 + 1.14.4 diff --git a/app/models/graph/schema.rb b/app/models/graph/schema.rb index 59ab357..5b1d2b8 100644 --- a/app/models/graph/schema.rb +++ b/app/models/graph/schema.rb @@ -1,5 +1,7 @@ module Graph Schema = GraphQL::Schema.define do + enable_active_record_batching + query Graph::Types::Query mutation Graph::Mutations::Mutation @@ -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 end diff --git a/app/models/graph/types/person.rb b/app/models/graph/types/person.rb index 959093f..f964ea8 100644 --- a/app/models/graph/types/person.rb +++ b/app/models/graph/types/person.rb @@ -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] @@ -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 + 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 diff --git a/app/models/graph/types/rating.rb b/app/models/graph/types/rating.rb index 9219497..6c83cd7 100644 --- a/app/models/graph/types/rating.rb +++ b/app/models/graph/types/rating.rb @@ -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 diff --git a/app/models/graph/types/species.rb b/app/models/graph/types/species.rb index 60de9ff..f430dc9 100644 --- a/app/models/graph/types/species.rb +++ b/app/models/graph/types/species.rb @@ -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] @@ -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 diff --git a/db/schema.graphql b/db/schema.graphql index 343d2dc..7297408 100644 --- a/db/schema.graphql +++ b/db/schema.graphql @@ -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