@@ -109,11 +109,7 @@ class PayloadType < GraphQL::Schema::Enum
109
109
110
110
class StreamInput < GraphQL ::Schema ::InputObject
111
111
argument :user_id , ID , required : true , camelize : false
112
- argument :payload_type , PayloadType , required : false , default_value : "ONE" , prepare : :downcase
113
-
114
- def downcase ( value )
115
- value ? value . downcase : value
116
- end
112
+ argument :payload_type , PayloadType , required : false , default_value : "ONE" , prepare : -> ( e , ctx ) { e ? e . downcase : e }
117
113
end
118
114
119
115
class EventSubscription < GraphQL ::Schema ::Subscription
@@ -174,7 +170,7 @@ class FromDefinitionInMemoryBackend < InMemoryBackend
174
170
type Subscription {
175
171
payload(id: ID!): Payload!
176
172
event(stream: StreamInput): Payload
177
- eventSubscription(userId: ID, payloadType: PayloadType): EventSubscriptionPayload
173
+ eventSubscription(userId: ID, payloadType: PayloadType = ONE ): EventSubscriptionPayload
178
174
myEvent(payloadType: PayloadType): Payload
179
175
failedEvent(id: ID!): Payload!
180
176
}
@@ -409,14 +405,23 @@ def str
409
405
# Subscribe with explicit null
410
406
schema . execute ( query_str , context : { socket : "4" } , variables : { "type" => nil } , root_value : root_object )
411
407
408
+ # The class-based schema has a "prepare" behavior, so it expects these downcased values in `.trigger`
409
+ if schema == ClassBasedInMemoryBackend ::Schema
410
+ one = "one"
411
+ two = "two"
412
+ else
413
+ one = "ONE"
414
+ two = "TWO"
415
+ end
416
+
412
417
# Trigger the subscription with coerceable args, different orders:
413
- schema . subscriptions . trigger ( "event" , { "stream" => { "user_id" => 3 , "payloadType" => "ONE" } } , OpenStruct . new ( str : "" , int : 1 ) )
414
- schema . subscriptions . trigger ( "event" , { "stream" => { "payloadType" => "ONE" , "user_id" => "3" } } , OpenStruct . new ( str : "" , int : 2 ) )
418
+ schema . subscriptions . trigger ( "event" , { "stream" => { "user_id" => 3 , "payloadType" => one } } , OpenStruct . new ( str : "" , int : 1 ) )
419
+ schema . subscriptions . trigger ( "event" , { "stream" => { "payloadType" => one , "user_id" => "3" } } , OpenStruct . new ( str : "" , int : 2 ) )
415
420
# This is a non-trigger
416
- schema . subscriptions . trigger ( "event" , { "stream" => { "user_id" => "3" , "payloadType" => "TWO" } } , OpenStruct . new ( str : "" , int : 3 ) )
421
+ schema . subscriptions . trigger ( "event" , { "stream" => { "user_id" => "3" , "payloadType" => two } } , OpenStruct . new ( str : "" , int : 3 ) )
417
422
# These get default value of ONE (underscored / symbols are ok)
418
423
schema . subscriptions . trigger ( "event" , { stream : { user_id : "3" } } , OpenStruct . new ( str : "" , int : 4 ) )
419
- # Trigger with null updates subscriptionss to null
424
+ # Trigger with null updates subscriptions to null
420
425
schema . subscriptions . trigger ( "event" , { "stream" => { "user_id" => 3 , "payloadType" => nil } } , OpenStruct . new ( str : "" , int : 5 ) )
421
426
422
427
assert_equal [ 1 , 2 , 4 ] , deliveries [ "1" ] . map { |d | d [ "data" ] [ "e1" ] [ "int" ] }
@@ -499,29 +504,49 @@ def str
499
504
eventSubscription(userId: "4", payloadType: ONE) { payload { int } }
500
505
}
501
506
GRAPHQL
507
+
508
+ query_str_3 = <<-GRAPHQL
509
+ subscription {
510
+ eventSubscription(userId: "4") { payload { int } }
511
+ }
512
+ GRAPHQL
502
513
# Value from variable
503
514
schema . execute ( query_str , context : { socket : "1" } , variables : { "type" => "ONE" } , root_value : root_object )
504
515
# Default value for variable
505
516
schema . execute ( query_str , context : { socket : "1" } , root_value : root_object )
506
517
# Query string literal value
507
518
schema . execute ( query_str_2 , context : { socket : "1" } , root_value : root_object )
519
+ # Schema default value
520
+ schema . execute ( query_str_3 , context : { socket : "1" } , root_value : root_object )
508
521
509
522
# There's no way to add `prepare:` when using SDL, so only the Ruby-defined schema has it
510
- expected_keys = if schema == ClassBasedInMemoryBackend ::Schema && TESTING_INTERPRETER
511
- # Unfortunately, on the non-interpreter runtime, `prepare:` was _not_ applied here.
512
- [
513
- ":eventSubscription:payloadType:one:userId:3" ,
514
- ":eventSubscription:payloadType:one:userId:4" ,
515
- ":eventSubscription:payloadType:two:userId:3" ,
516
- ]
523
+ expected_sub_count = if schema == ClassBasedInMemoryBackend ::Schema
524
+ if TESTING_INTERPRETER
525
+ {
526
+ ":eventSubscription:payloadType:one:userId:3" => 1 ,
527
+ ":eventSubscription:payloadType:one:userId:4" => 2 ,
528
+ ":eventSubscription:payloadType:two:userId:3" => 1 ,
529
+ }
530
+ else
531
+ # Unfortunately, on the non-interpreter runtime, `prepare:` was _not_ applied here,
532
+ {
533
+ ":eventSubscription:payloadType:ONE:userId:3" => 1 ,
534
+ ":eventSubscription:payloadType:ONE:userId:4" => 2 ,
535
+ ":eventSubscription:payloadType:TWO:userId:3" => 1 ,
536
+ }
537
+ end
517
538
else
518
- [
519
- ":eventSubscription:payloadType:ONE:userId:3" ,
520
- ":eventSubscription:payloadType:ONE:userId:4" ,
521
- ":eventSubscription:payloadType:TWO:userId:3" ,
522
- ]
539
+ {
540
+ ":eventSubscription:payloadType:ONE:userId:3" => 1 ,
541
+ ":eventSubscription:payloadType:ONE:userId:4" => 2 ,
542
+ ":eventSubscription:payloadType:TWO:userId:3" => 1 ,
543
+ }
544
+ end
545
+ count_by_topic = Hash . new ( 0 )
546
+ active_subscriptions . each do |k , v |
547
+ count_by_topic [ k ] += v . size
523
548
end
524
- assert_equal expected_keys , active_subscriptions . keys . sort
549
+ assert_equal expected_sub_count , count_by_topic
525
550
end
526
551
527
552
it "doesn't apply for plain fields" do
@@ -536,29 +561,41 @@ def str
536
561
event(stream: { user_id: "4", payloadType: ONE}) { int }
537
562
}
538
563
GRAPHQL
564
+
565
+ query_str_3 = <<-GRAPHQL
566
+ subscription {
567
+ event(stream: { user_id: "4" }) { int }
568
+ }
569
+ GRAPHQL
539
570
# Value from variable
540
571
schema . execute ( query_str , context : { socket : "1" } , variables : { "type" => "ONE" } , root_value : root_object )
541
572
# Default value for variable
542
573
schema . execute ( query_str , context : { socket : "1" } , root_value : root_object )
543
574
# Query string literal value
544
575
schema . execute ( query_str_2 , context : { socket : "1" } , root_value : root_object )
576
+ # Schema default value
577
+ schema . execute ( query_str_3 , context : { socket : "1" } , root_value : root_object )
545
578
546
579
547
580
# There's no way to add `prepare:` when using SDL, so only the Ruby-defined schema has it
548
- expected_keys = if schema == ClassBasedInMemoryBackend ::Schema
549
- [
550
- ":event:stream:payloadType:one:user_id:3" ,
551
- ":event:stream:payloadType:one :user_id:4" ,
552
- ":event:stream:payloadType:two :user_id:3" ,
553
- ]
581
+ expected_sub_count = if schema == ClassBasedInMemoryBackend ::Schema
582
+ {
583
+ ":event:stream:payloadType:one:user_id:3" => 1 ,
584
+ ":event:stream:payloadType:two :user_id:3" => 1 ,
585
+ ":event:stream:payloadType:one :user_id:4" => 2 ,
586
+ }
554
587
else
555
- [
556
- ":event:stream:payloadType:ONE:user_id:3" ,
557
- ":event:stream:payloadType:ONE:user_id:4" ,
558
- ":event:stream:payloadType:TWO:user_id:3" ,
559
- ]
588
+ {
589
+ ":event:stream:payloadType:ONE:user_id:3" => 1 ,
590
+ ":event:stream:payloadType:TWO:user_id:3" => 1 ,
591
+ ":event:stream:payloadType:ONE:user_id:4" => 2 ,
592
+ }
593
+ end
594
+ count_by_topic = Hash . new ( 0 )
595
+ active_subscriptions . each do |k , v |
596
+ count_by_topic [ k ] += v . size
560
597
end
561
- assert_equal expected_keys , active_subscriptions . keys . sort
598
+ assert_equal expected_sub_count , count_by_topic
562
599
end
563
600
end
564
601
@@ -592,9 +629,9 @@ def str
592
629
593
630
it "sends query errors to the subscriptions" do
594
631
query_str = <<-GRAPHQL
595
- subscription($type: PayloadType) {
596
- myEvent(payloadType: $type) { str }
597
- }
632
+ subscription($type: PayloadType) {
633
+ myEvent(payloadType: $type) { str }
634
+ }
598
635
GRAPHQL
599
636
600
637
schema . execute ( query_str , context : { socket : "1" , me : "1" } , variables : { "type" => "ONE" } , root_value : root_object )
0 commit comments