@@ -64,7 +64,7 @@ describe('messages/subscribe', () => {
64
64
assert . include ( subscriptions , { connectionId, subscriptionId : '1234' } )
65
65
} )
66
66
67
- it ( 'disconnects on error' , async ( ) => {
67
+ it ( 'sends errors on error' , async ( ) => {
68
68
const event : any = { requestContext : { connectedAt : 1628889982819 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'b6o5BPxb3' , requestId : 'MaEe0DVon' , requestTimeEpoch : 1628889983319 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"abcdefg","type":"subscribe","payload":{"query":"{ HIHOWEAREYOU }"}}' }
69
69
const state : { delete : { ConnectionId : string } [ ] , post : { ConnectionId : string , Data : string } [ ] } = { post : [ ] , delete : [ ] }
70
70
const server = await mockServerContext ( {
@@ -80,82 +80,87 @@ describe('messages/subscribe', () => {
80
80
assert . deepEqual ( state , {
81
81
post : [
82
82
{ ConnectionId, Data : JSON . stringify ( { type : 'connection_ack' } ) } ,
83
+ { ConnectionId, Data : JSON . stringify ( { type : 'error' , id : 'abcdefg' , payload : [ {
84
+ message : 'Cannot query field "HIHOWEAREYOU" on type "Query".' ,
85
+ locations : [ { line :1 , column :3 } ] ,
86
+ } ,
87
+ ] } ) } ,
83
88
] ,
84
- delete : [
85
- { ConnectionId } ,
86
- ] ,
89
+ delete : [ ] ,
87
90
} )
88
91
} )
89
- it ( 'calls the global error callback on error ' , async ( ) => {
90
- const event : any = { requestContext : { connectedAt : 1628889982819 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'b6o5BPxb3' , requestId : 'MaEe0DVon' , requestTimeEpoch : 1628889983319 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"abcdefg","type":"subscribe","payload":{"query":"{ HIHOWEAREYOU }"}}' }
92
+ it ( 'calls the global error callback server errors ' , async ( ) => {
93
+ const event : any = { requestContext : { connectedAt : 1628889982819 , connectionId, domainName : 'localhost:3339' , eventType : 'MESSAGE' , messageDirection : 'IN' , messageId : 'b6o5BPxb3' , requestId : 'MaEe0DVon' , requestTimeEpoch : 1628889983319 , routeKey : '$default' , stage : 'testing' } , isBase64Encoded : false , body : '{"id":"abcdefg","type":"subscribe","payload":{"query":"{ hello }"}}' }
91
94
let error : any = null
95
+ let sendErr = false
92
96
const server = await mockServerContext ( {
93
97
apiGatewayManagementApi : {
94
98
// eslint-disable-next-line @typescript-eslint/no-empty-function
95
- postToConnection : ( ) => ( { promise : async ( ) => { } } ) ,
99
+ postToConnection : ( ) => ( { promise : async ( ) => { if ( sendErr ) { throw new Error ( 'postToConnection Error' ) } } } ) ,
96
100
// eslint-disable-next-line @typescript-eslint/no-empty-function
97
101
deleteConnection : ( ) => ( { promise : async ( ) => { } } ) ,
98
102
} ,
99
103
// eslint-disable-next-line @typescript-eslint/no-empty-function
100
104
onError : err => ( error = err ) ,
101
105
} )
102
106
await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
107
+ sendErr = true
103
108
await subscribe ( { server, event, message : JSON . parse ( event . body ) } )
104
- assert . match ( error . message , / C a n n o t q u e r y f i e l d " H I H O W E A R E Y O U " o n t y p e " Q u e r y " / )
109
+ 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 / )
105
110
} )
106
111
describe ( 'callbacks' , ( ) => {
107
- it ( 'fires onSubscribe before subscribing' , async ( ) => {
108
-
109
- const onSubscribe : string [ ] = [ ]
112
+ // this test doesn't make sense anymore because these error are now sent as error messages
113
+ // it('fires onSubscribe before subscribing', async () => {
110
114
111
- const typeDefs = `
112
- type Query {
113
- hello: String
114
- }
115
- type Subscription {
116
- greetings: String
117
- }
118
- `
119
- const resolvers = {
120
- Query : {
121
- hello : ( ) => 'Hello World!' ,
122
- } ,
123
- Subscription : {
124
- greetings :{
125
- subscribe : pubsubSubscribe ( 'greetings' , {
126
- onSubscribe ( ) {
127
- onSubscribe . push ( 'We did it!' )
128
- throw new Error ( 'don\'t subscribe!' )
129
- } ,
130
- } ) ,
131
- resolve : ( { payload } ) => {
132
- return payload
133
- } ,
134
- } ,
135
- } ,
136
- }
115
+ // const onSubscribe: string[] = []
137
116
138
- const schema = makeExecutableSchema ( {
139
- typeDefs,
140
- resolvers,
141
- } )
142
- const server = await mockServerContext ( {
143
- schema,
144
- } )
145
- 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 }"}}' }
117
+ // const typeDefs = `
118
+ // type Query {
119
+ // hello: String
120
+ // }
121
+ // type Subscription {
122
+ // greetings: String
123
+ // }
124
+ // `
125
+ // const resolvers = {
126
+ // Query: {
127
+ // hello: () => 'Hello World!',
128
+ // },
129
+ // Subscription: {
130
+ // greetings:{
131
+ // subscribe: pubsubSubscribe('greetings', {
132
+ // onSubscribe() {
133
+ // onSubscribe.push('We did it!')
134
+ // throw new Error('don\'t subscribe!')
135
+ // },
136
+ // }),
137
+ // resolve: ({ payload }) => {
138
+ // return payload
139
+ // },
140
+ // },
141
+ // },
142
+ // }
146
143
147
- await connection_init ( { server, event : connectionInitEvent , message : JSON . parse ( connectionInitEvent . body ) } )
148
- try {
149
- await subscribe ( { server, event, message : JSON . parse ( event . body ) } )
150
- throw new Error ( 'should not have subscribed' )
151
- } catch ( error ) {
152
- assert . equal ( error . message , 'don\'t subscribe!' )
153
- }
154
- assert . deepEqual ( onSubscribe , [ 'We did it!' ] )
155
- const subscriptions = await collect ( server . mapper . query ( server . model . Subscription , { connectionId : equals ( event . requestContext . connectionId ) } , { indexName : 'ConnectionIndex' } ) )
156
- assert . isEmpty ( subscriptions )
144
+ // const schema = makeExecutableSchema({
145
+ // typeDefs,
146
+ // resolvers,
147
+ // })
148
+ // const server = await mockServerContext({
149
+ // schema,
150
+ // })
151
+ // 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
152
158
- } )
153
+ // await connection_init({ server, event: connectionInitEvent, message: JSON.parse(connectionInitEvent.body) })
154
+ // try {
155
+ // await subscribe({ server, event, message: JSON.parse(event.body) })
156
+ // throw new Error('should not have subscribed')
157
+ // } catch (error) {
158
+ // assert.equal(error.message, 'don\'t subscribe!')
159
+ // }
160
+ // assert.deepEqual(onSubscribe, ['We did it!'])
161
+ // const subscriptions = await collect(server.mapper.query(server.model.Subscription, { connectionId: equals(event.requestContext.connectionId) }, { indexName: 'ConnectionIndex' }))
162
+ // assert.isEmpty(subscriptions)
163
+ // })
159
164
it ( 'fires onAfterSubscribe after subscribing' , async ( ) => {
160
165
const events : string [ ] = [ ]
161
166
0 commit comments