@@ -85,11 +85,15 @@ describe('messages/subscribe', () => {
8585 assert . deepEqual ( state , {
8686 post : [
8787 { 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+ } ) ,
9196 } ,
92- ] } ) } ,
9397 ] ,
9498 delete : [ ] ,
9599 } )
@@ -102,7 +106,7 @@ describe('messages/subscribe', () => {
102106 const server = await mockServerContext ( {
103107 apiGatewayManagementApi : {
104108 // 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' ) } } } ) ,
106110 // eslint-disable-next-line @typescript-eslint/no-empty-function
107111 deleteConnection : ( ) => ( { promise : async ( ) => { } } ) ,
108112 } ,
@@ -112,7 +116,7 @@ describe('messages/subscribe', () => {
112116 await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
113117 sendErr = true
114118 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 / )
116120 } )
117121
118122 describe ( 'callbacks' , ( ) => {
@@ -132,7 +136,7 @@ describe('messages/subscribe', () => {
132136 hello : ( ) => 'Hello World!' ,
133137 } ,
134138 Subscription : {
135- greetings :{
139+ greetings : {
136140 subscribe : pubsubSubscribe ( 'greetings' , {
137141 onSubscribe ( ) {
138142 onSubscribe . push ( 'We did it!' )
@@ -146,13 +150,8 @@ describe('messages/subscribe', () => {
146150 } ,
147151 }
148152
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 } )
156155 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 }"}}' }
157156
158157 await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
@@ -172,6 +171,96 @@ describe('messages/subscribe', () => {
172171 assert . isEmpty ( subscriptions )
173172 } )
174173
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+
175264 it ( 'fires onAfterSubscribe after subscribing' , async ( ) => {
176265 const events : string [ ] = [ ]
177266
@@ -188,7 +277,7 @@ describe('messages/subscribe', () => {
188277 hello : ( ) => 'Hello World!' ,
189278 } ,
190279 Subscription : {
191- greetings :{
280+ greetings : {
192281 subscribe : pubsubSubscribe ( 'greetings' , {
193282 onSubscribe ( ) {
194283 events . push ( 'onSubscribe' )
@@ -204,13 +293,8 @@ describe('messages/subscribe', () => {
204293 } ,
205294 }
206295
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 } )
214298 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 }"}}' }
215299
216300 await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
0 commit comments