@@ -85,11 +85,15 @@ describe('messages/subscribe', () => {
85
85
assert . deepEqual ( state , {
86
86
post : [
87
87
{ ConnectionId, Data : JSON . stringify ( { type : 'connection_ack' } ) } ,
88
- { ConnectionId, Data : JSON . stringify ( { type : 'error' , id : 'abcdefg' , payload : [ {
89
- message : 'Cannot query field "HIHOWEAREYOU" on type "Query".' ,
90
- locations : [ { line :1 , column :3 } ] ,
88
+ {
89
+ ConnectionId, Data : JSON . stringify ( {
90
+ type : 'error' , id : 'abcdefg' , payload : [ {
91
+ message : 'Cannot query field "HIHOWEAREYOU" on type "Query".' ,
92
+ locations : [ { line : 1 , column : 3 } ] ,
93
+ } ,
94
+ ] ,
95
+ } ) ,
91
96
} ,
92
- ] } ) } ,
93
97
] ,
94
98
delete : [ ] ,
95
99
} )
@@ -102,7 +106,7 @@ describe('messages/subscribe', () => {
102
106
const server = await mockServerContext ( {
103
107
apiGatewayManagementApi : {
104
108
// eslint-disable-next-line @typescript-eslint/no-empty-function
105
- postToConnection : ( ) => ( { promise : async ( ) => { if ( sendErr ) { throw new Error ( 'postToConnection Error' ) } } } ) ,
109
+ postToConnection : ( ) => ( { promise : async ( ) => { if ( sendErr ) { throw new Error ( 'postToConnection Error' ) } } } ) ,
106
110
// eslint-disable-next-line @typescript-eslint/no-empty-function
107
111
deleteConnection : ( ) => ( { promise : async ( ) => { } } ) ,
108
112
} ,
@@ -112,7 +116,7 @@ describe('messages/subscribe', () => {
112
116
await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
113
117
sendErr = true
114
118
await subscribe ( { server, event, message : JSON . parse ( event . body ) } )
115
- assert . match ( error . message , / p o s t T o C o n n e c t i o n E r r o r / )
119
+ assert . match ( error . message , / p o s t T o C o n n e c t i o n E r r o r / )
116
120
} )
117
121
118
122
describe ( 'callbacks' , ( ) => {
@@ -132,7 +136,7 @@ describe('messages/subscribe', () => {
132
136
hello : ( ) => 'Hello World!' ,
133
137
} ,
134
138
Subscription : {
135
- greetings :{
139
+ greetings : {
136
140
subscribe : pubsubSubscribe ( 'greetings' , {
137
141
onSubscribe ( ) {
138
142
onSubscribe . push ( 'We did it!' )
@@ -146,13 +150,8 @@ describe('messages/subscribe', () => {
146
150
} ,
147
151
}
148
152
149
- const schema = makeExecutableSchema ( {
150
- typeDefs,
151
- resolvers,
152
- } )
153
- const server = await mockServerContext ( {
154
- schema,
155
- } )
153
+ const schema = makeExecutableSchema ( { typeDefs, resolvers } )
154
+ const server = await mockServerContext ( { schema } )
156
155
const event : any = { requestContext : { connectedAt : 1628889984369 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'el4MNdOJy' , requestId : '0yd7bkvXz' , requestTimeEpoch : 1628889984774 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"1234","type":"subscribe","payload":{"query":"subscription { greetings }"}}' }
157
156
158
157
await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
@@ -172,6 +171,96 @@ describe('messages/subscribe', () => {
172
171
assert . isEmpty ( subscriptions )
173
172
} )
174
173
174
+ it ( 'fires onSubscribe with variable args' , async ( ) => {
175
+ const collectedArgs : any [ ] = [ ]
176
+
177
+ const typeDefs = `
178
+ type Query {
179
+ hello(name: String!): String
180
+ }
181
+ type Subscription {
182
+ greetings(name: String!): String
183
+ }
184
+ `
185
+ const resolvers = {
186
+ Query : {
187
+ hello : ( _ , { name } ) => `Hello ${ name } !` ,
188
+ } ,
189
+ Subscription : {
190
+ greetings : {
191
+ subscribe : pubsubSubscribe ( 'greetings' , {
192
+ onSubscribe ( _ , args ) {
193
+ collectedArgs . push ( args )
194
+ } ,
195
+ } ) ,
196
+ resolve : ( { payload } ) => {
197
+ return payload
198
+ } ,
199
+ } ,
200
+ } ,
201
+ }
202
+
203
+ const schema = makeExecutableSchema ( { typeDefs, resolvers } )
204
+ const server = await mockServerContext ( { schema } )
205
+ const event : any = { requestContext : { connectedAt : 1628889984369 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'el4MNdOJy' , requestId : '0yd7bkvXz' , requestTimeEpoch : 1628889984774 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"1234","type":"subscribe","payload":{"query":"subscription($name: String!) { greetings(name: $name) }", "variables":{"name":"Jonas"}}}' }
206
+
207
+ await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
208
+ await subscribe ( { server, event, message : JSON . parse ( event . body ) } )
209
+ assert . deepEqual ( collectedArgs [ 0 ] , { name : 'Jonas' } )
210
+ const [ subscription ] = await collect ( server . models . subscription . query ( {
211
+ IndexName : 'ConnectionIndex' ,
212
+ ExpressionAttributeNames : { '#a' : 'connectionId' } ,
213
+ ExpressionAttributeValues : { ':1' : event . requestContext . connectionId } ,
214
+ KeyConditionExpression : '#a = :1' ,
215
+ } ) )
216
+ assert . containSubset ( subscription , { connectionId, subscriptionId : '1234' , subscription : JSON . parse ( event . body ) . payload } )
217
+ } )
218
+
219
+ it ( 'fires onSubscribe with inline args' , async ( ) => {
220
+ const collectedArgs : any [ ] = [ ]
221
+
222
+ const typeDefs = `
223
+ type Query {
224
+ hello(name: String!): String
225
+ }
226
+ type Subscription {
227
+ greetings(name: String!): String
228
+ }
229
+ `
230
+ const resolvers = {
231
+ Query : {
232
+ hello : ( _ , { name } ) => `Hello ${ name } !` ,
233
+ } ,
234
+ Subscription : {
235
+ greetings : {
236
+ subscribe : pubsubSubscribe ( 'greetings' , {
237
+ onSubscribe ( _ , args ) {
238
+ collectedArgs . push ( args )
239
+ } ,
240
+ } ) ,
241
+ resolve : ( { payload } ) => {
242
+ return payload
243
+ } ,
244
+ } ,
245
+ } ,
246
+ }
247
+
248
+ const schema = makeExecutableSchema ( { typeDefs, resolvers } )
249
+ const server = await mockServerContext ( { schema } )
250
+ const event : any = { requestContext : { connectedAt : 1628889984369 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'el4MNdOJy' , requestId : '0yd7bkvXz' , requestTimeEpoch : 1628889984774 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"1234","type":"subscribe","payload":{"query":"subscription { greetings(name: \\"Jonas\\") }"}}' }
251
+
252
+ await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
253
+ await subscribe ( { server, event, message : JSON . parse ( event . body ) } )
254
+ assert . deepEqual ( collectedArgs [ 0 ] , { name : 'Jonas' } )
255
+ const [ subscription ] = await collect ( server . models . subscription . query ( {
256
+ IndexName : 'ConnectionIndex' ,
257
+ ExpressionAttributeNames : { '#a' : 'connectionId' } ,
258
+ ExpressionAttributeValues : { ':1' : event . requestContext . connectionId } ,
259
+ KeyConditionExpression : '#a = :1' ,
260
+ } ) )
261
+ assert . containSubset ( subscription , { connectionId, subscriptionId : '1234' , subscription : JSON . parse ( event . body ) . payload } )
262
+ } )
263
+
175
264
it ( 'fires onAfterSubscribe after subscribing' , async ( ) => {
176
265
const events : string [ ] = [ ]
177
266
@@ -188,7 +277,7 @@ describe('messages/subscribe', () => {
188
277
hello : ( ) => 'Hello World!' ,
189
278
} ,
190
279
Subscription : {
191
- greetings :{
280
+ greetings : {
192
281
subscribe : pubsubSubscribe ( 'greetings' , {
193
282
onSubscribe ( ) {
194
283
events . push ( 'onSubscribe' )
@@ -204,13 +293,8 @@ describe('messages/subscribe', () => {
204
293
} ,
205
294
}
206
295
207
- const schema = makeExecutableSchema ( {
208
- typeDefs,
209
- resolvers,
210
- } )
211
- const server = await mockServerContext ( {
212
- schema,
213
- } )
296
+ const schema = makeExecutableSchema ( { typeDefs, resolvers } )
297
+ const server = await mockServerContext ( { schema } )
214
298
const event : any = { requestContext : { connectedAt : 1628889984369 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'el4MNdOJy' , requestId : '0yd7bkvXz' , requestTimeEpoch : 1628889984774 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"1234","type":"subscribe","payload":{"query":"subscription { greetings }"}}' }
215
299
216
300
await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
0 commit comments