Skip to content

Commit 412e141

Browse files
committed
Add tests for lazy skip in subscription and query
1 parent b4c4fa2 commit 412e141

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lib/graphql/execution/lazy.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def value
4848
end
4949
end
5050

51-
if @value.is_a?(StandardError)
51+
# `SKIP` was made into a subclass of `GraphQL::Error` to improve runtime performance
52+
# (fewer clauses in a hot `case` block), but now it requires special handling here.
53+
# I think it's still worth it for the performance win, but if the number of special
54+
# cases grows, then maybe it's worth rethinking somehow.
55+
if @value.is_a?(StandardError) && @value != GraphQL::Execution::Execute::SKIP
5256
raise @value
5357
else
5458
@value

spec/graphql/execution/interpreter_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22
require "spec_helper"
3+
require_relative "../subscriptions_spec"
34

45
describe GraphQL::Execution::Interpreter do
56
module InterpreterTest
@@ -547,6 +548,66 @@ def self.resolve_type(type, obj, ctx)
547548
end
548549
end
549550

551+
describe "Lazy skips" do
552+
class LazySkipSchema < GraphQL::Schema
553+
class Query < GraphQL::Schema::Object
554+
def self.authorized?(obj, ctx)
555+
-> { true }
556+
end
557+
field :skip, String, null: true
558+
559+
def skip
560+
context.skip
561+
end
562+
563+
field :lazy_skip, String, null: true
564+
def lazy_skip
565+
-> { context.skip }
566+
end
567+
end
568+
569+
class NothingSubscription < GraphQL::Schema::Subscription
570+
field :nothing, String, null: true
571+
def authorized?(*)
572+
-> { true }
573+
end
574+
575+
def update
576+
{ nothing: object }
577+
end
578+
end
579+
580+
class Subscription < GraphQL::Schema::Object
581+
field :nothing, subscription: NothingSubscription
582+
end
583+
584+
query Query
585+
subscription Subscription
586+
use InMemoryBackend::Subscriptions, extra: nil
587+
lazy_resolve Proc, :call
588+
end
589+
590+
focus
591+
it "skips properly" do
592+
res = LazySkipSchema.execute("{ skip }")
593+
assert_equal({}, res["data"])
594+
refute res.key?("errors")
595+
# This failed on 1.12.10, too
596+
# res = LazySkipSchema.execute("{ lazySkip }")
597+
# pp res
598+
# assert_equal({}, res["data"])
599+
# refute res.key?("errors")
600+
601+
res = LazySkipSchema.execute("subscription { nothing { nothing } }")
602+
pp res
603+
assert_equal({}, res["data"])
604+
refute res.key?("errors")
605+
LazySkipSchema.subscriptions.trigger(:nothing, {}, :nothing_at_all)
606+
key, updates = LazySkipSchema.subscriptions.deliveries.first
607+
assert_equal "nothing_at_all", updates[0]["data"]["nothing"]["nothing"]
608+
end
609+
end
610+
550611
describe "GraphQL::ExecutionErrors from non-null list fields" do
551612
module ListErrorTest
552613
class BaseField < GraphQL::Schema::Field

0 commit comments

Comments
 (0)