spec/support/star_wars/schema.rb in graphql-1.6.8 vs spec/support/star_wars/schema.rb in graphql-1.7.0

- old
+ new

@@ -263,10 +263,41 @@ # Used as the root for derived types: name "IntroduceShipFunction" function IntroduceShipFunction.new end + # GraphQL-Batch knockoff + class LazyLoader + def self.defer(ctx, model, id) + ids = ctx.namespace(:loading)[model] ||= [] + ids << id + self.new(model: model, id: id, context: ctx) + end + + def initialize(model:, id:, context:) + @model = model + @id = id + @context = context + end + + def value + loaded = @context.namespace(:loaded)[@model] ||= {} + if loaded.empty? + ids = @context.namespace(:loading)[@model] + # Example custom tracing + GraphQL::Tracing.trace("lazy_loader", { ids: ids, model: @model}) do + records = @model.where(id: ids) + records.each do |record| + loaded[record.id.to_s] = record + end + end + end + + loaded[@id] + end + end + class LazyWrapper def initialize(value = nil, &block) if block_given? @lazy_value = block else @@ -330,10 +361,17 @@ field :nodes, GraphQL::Relay::Node.plural_field field :nodesWithCustomResolver, GraphQL::Relay::Node.plural_field( resolve: ->(_, _, _) { [StarWars::DATA["Faction"]["1"], StarWars::DATA["Faction"]["2"]] } ) + + field :batchedBase, BaseType do + argument :id, !types.ID + resolve ->(o, a, c) { + LazyLoader.defer(c, Base, a["id"]) + } + end end MutationType = GraphQL::ObjectType.define do name "Mutation" # The mutation object exposes a field: @@ -388,9 +426,10 @@ id_from_object ->(object, type, ctx) do GraphQL::Schema::UniqueWithinType.encode(type.name, object.id) end lazy_resolve(LazyWrapper, :value) + lazy_resolve(LazyLoader, :value) instrument(:field, ClassNameRecorder.new(:before_built_ins)) instrument(:field, ClassNameRecorder.new(:after_built_ins), after_built_ins: true) end end