1- using System ;
1+ using System . Threading ;
2+ using System ;
23using System . Threading . Tasks ;
34using HotChocolate . Execution ;
5+ using HotChocolate . Language ;
46using HotChocolate . Types ;
57using Moq ;
68using Xunit ;
@@ -10,7 +12,7 @@ namespace HotChocolate.Subscriptions
1012 public class SubscriptionTests
1113 {
1214 [ Fact ]
13- public async Task Subscribe_RaiseEvent_ReceiveSubscriptionResult ( )
15+ public async Task Subscribe_RaiseEvent_No_Arguments ( )
1416 {
1517 // arrange
1618 var registry = new InMemoryEventRegistry ( ) ;
@@ -45,9 +47,106 @@ await executor.ExecuteAsync("subscription { foo }")
4547 await executor . ExecuteAsync ( "mutation { foo }" ) ;
4648
4749 // assert
48- IReadOnlyQueryResult result = await responseStream . ReadAsync ( ) ;
49- Assert . False ( responseStream . IsCompleted ) ;
50- Assert . Equal ( "bar" , result . Data [ "foo" ] ) ;
50+ using ( var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 30 ) ) )
51+ {
52+ IReadOnlyQueryResult result = await responseStream . ReadAsync ( cts . Token ) ;
53+ Assert . False ( responseStream . IsCompleted ) ;
54+ Assert . Equal ( "bar" , result . Data [ "foo" ] ) ;
55+ }
56+ }
57+
58+ [ Fact ]
59+ public async Task Subscribe_RaiseEvent_With_Argument ( )
60+ {
61+ // arrange
62+ var registry = new InMemoryEventRegistry ( ) ;
63+
64+ var services = new Mock < IServiceProvider > ( ) ;
65+ services . Setup ( t => t . GetService ( It . IsAny < Type > ( ) ) )
66+ . Returns ( new Func < Type , object > ( t =>
67+ {
68+ if ( t == typeof ( IEventRegistry )
69+ || t == typeof ( IEventSender ) )
70+ {
71+ return registry ;
72+ }
73+ return null ;
74+ } ) ) ;
75+
76+ ISchema schema = Schema . Create ( c =>
77+ {
78+ c . RegisterQueryType < DummyQuery > ( ) ;
79+ c . RegisterServiceProvider ( services . Object ) ;
80+ c . RegisterMutationType < MutationType > ( ) ;
81+ c . RegisterSubscriptionType < SubscriptionType > ( ) ;
82+ } ) ;
83+
84+ IQueryExecutor executor = schema . MakeExecutable ( ) ;
85+
86+ var responseStream =
87+ await executor . ExecuteAsync ( "subscription { bar(baz:\" 123\" ) }" )
88+ as IResponseStream ;
89+
90+ // act
91+ await executor . ExecuteAsync ( "mutation { bar(baz:\" 123\" ) }" ) ;
92+
93+ // assert
94+ using ( var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 30 ) ) )
95+ {
96+ IReadOnlyQueryResult result = await responseStream . ReadAsync ( cts . Token ) ;
97+ Assert . False ( responseStream . IsCompleted ) ;
98+ Assert . Equal ( "123" , result . Data [ "bar" ] ) ;
99+ }
100+ }
101+
102+ [ Fact ]
103+ public async Task Subscribe_RaiseEvent_With_Argument_As_Variables ( )
104+ {
105+ // arrange
106+ var registry = new InMemoryEventRegistry ( ) ;
107+
108+ var services = new Mock < IServiceProvider > ( ) ;
109+ services . Setup ( t => t . GetService ( It . IsAny < Type > ( ) ) )
110+ . Returns ( new Func < Type , object > ( t =>
111+ {
112+ if ( t == typeof ( IEventRegistry )
113+ || t == typeof ( IEventSender ) )
114+ {
115+ return registry ;
116+ }
117+ return null ;
118+ } ) ) ;
119+
120+ ISchema schema = Schema . Create ( c =>
121+ {
122+ c . RegisterQueryType < DummyQuery > ( ) ;
123+ c . RegisterServiceProvider ( services . Object ) ;
124+ c . RegisterMutationType < MutationType > ( ) ;
125+ c . RegisterSubscriptionType < SubscriptionType > ( ) ;
126+ } ) ;
127+
128+ IQueryExecutor executor = schema . MakeExecutable ( ) ;
129+
130+ var responseStream =
131+ await executor . ExecuteAsync ( QueryRequestBuilder . New ( )
132+ . SetQuery ( "subscription($a: String!) { bar(baz:$a) }" )
133+ . AddVariableValue ( "a" , "123" )
134+ . Create ( ) )
135+ as IResponseStream ;
136+
137+ // act
138+ await executor . ExecuteAsync ( QueryRequestBuilder . New ( )
139+ . SetQuery ( "mutation($a: String!) { bar(baz:$a) }" )
140+ . AddVariableValue ( "a" , "123" )
141+ . Create ( ) ) ;
142+
143+ // assert
144+ using ( var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 30 ) ) )
145+ {
146+ IReadOnlyQueryResult result = await responseStream . ReadAsync ( cts . Token ) ;
147+ Assert . False ( responseStream . IsCompleted ) ;
148+ Assert . Equal ( "123" , result . Data [ "bar" ] ) ;
149+ }
51150 }
52151
53152 public class DummyQuery
@@ -62,6 +161,9 @@ protected override void Configure(IObjectTypeDescriptor descriptor)
62161 {
63162 descriptor . Name ( "subscription" ) ;
64163 descriptor . Field ( "foo" ) . Resolver ( ( ) => "bar" ) ;
164+ descriptor . Field ( "bar" )
165+ . Argument ( "baz" , a => a . Type < NonNullType < StringType > > ( ) )
166+ . Resolver ( ctx => ctx . Argument < string > ( "baz" ) ) ;
65167 }
66168 }
67169
@@ -73,8 +175,17 @@ protected override void Configure(IObjectTypeDescriptor descriptor)
73175 descriptor . Name ( "mutation" ) ;
74176 descriptor . Field ( "foo" ) . Resolver ( ctx =>
75177 {
76- ctx . Service < IEventSender > ( )
77- . SendAsync ( new EventMessage ( "foo" ) ) ;
178+ ctx . Service < IEventSender > ( ) . SendAsync ( new EventMessage ( "foo" ) ) ;
179+ return "barmut" ;
180+ } ) ;
181+
182+ descriptor . Field ( "bar" )
183+ . Argument ( "baz" , a => a . Type < NonNullType < StringType > > ( ) )
184+ . Resolver ( ctx =>
185+ {
186+ IValueNode argumentValue = ctx . Argument < IValueNode > ( "baz" ) ;
187+ ctx . Service < IEventSender > ( ) . SendAsync (
188+ new EventMessage ( "bar" , new ArgumentNode ( "baz" , argumentValue ) ) ) ;
78189 return "barmut" ;
79190 } ) ;
80191 }
0 commit comments