|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { expectJSON } from '../../__testUtils__/expectJSON.js'; |
| 5 | + |
| 6 | +import { parse } from '../../language/parser.js'; |
| 7 | + |
| 8 | +import { buildSchema } from '../../utilities/buildASTSchema.js'; |
| 9 | + |
| 10 | +import { execute } from '../execute.js'; |
| 11 | + |
| 12 | +const schema = buildSchema(` |
| 13 | + type Todo { |
| 14 | + id: ID |
| 15 | + text: String |
| 16 | + author: User |
| 17 | + } |
| 18 | +
|
| 19 | + type User { |
| 20 | + id: ID |
| 21 | + name: String |
| 22 | + } |
| 23 | +
|
| 24 | + type Query { |
| 25 | + todo: Todo |
| 26 | + } |
| 27 | +
|
| 28 | + type Mutation { |
| 29 | + foo: String |
| 30 | + bar: String |
| 31 | + } |
| 32 | +`); |
| 33 | + |
| 34 | +describe('Execute: Cancellation', () => { |
| 35 | + it('should stop the execution when aborted during object field completion', async () => { |
| 36 | + const abortController = new AbortController(); |
| 37 | + const document = parse(` |
| 38 | + query { |
| 39 | + todo { |
| 40 | + id |
| 41 | + author { |
| 42 | + id |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + `); |
| 47 | + |
| 48 | + const resultPromise = execute({ |
| 49 | + document, |
| 50 | + schema, |
| 51 | + abortSignal: abortController.signal, |
| 52 | + rootValue: { |
| 53 | + todo: async () => |
| 54 | + Promise.resolve({ |
| 55 | + id: '1', |
| 56 | + text: 'Hello, World!', |
| 57 | + /* c8 ignore next */ |
| 58 | + author: () => expect.fail('Should not be called'), |
| 59 | + }), |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + abortController.abort('Aborted'); |
| 64 | + |
| 65 | + const result = await resultPromise; |
| 66 | + |
| 67 | + expectJSON(result).toDeepEqual({ |
| 68 | + data: { |
| 69 | + todo: null, |
| 70 | + }, |
| 71 | + errors: [ |
| 72 | + { |
| 73 | + message: 'Aborted', |
| 74 | + path: ['todo', 'id'], |
| 75 | + locations: [{ line: 4, column: 11 }], |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should stop the execution when aborted during nested object field completion', async () => { |
| 82 | + const abortController = new AbortController(); |
| 83 | + const document = parse(` |
| 84 | + query { |
| 85 | + todo { |
| 86 | + id |
| 87 | + author { |
| 88 | + id |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + `); |
| 93 | + |
| 94 | + const resultPromise = execute({ |
| 95 | + document, |
| 96 | + schema, |
| 97 | + abortSignal: abortController.signal, |
| 98 | + rootValue: { |
| 99 | + todo: { |
| 100 | + id: '1', |
| 101 | + text: 'Hello, World!', |
| 102 | + /* c8 ignore next 3 */ |
| 103 | + author: async () => |
| 104 | + Promise.resolve(() => expect.fail('Should not be called')), |
| 105 | + }, |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + abortController.abort('Aborted'); |
| 110 | + |
| 111 | + const result = await resultPromise; |
| 112 | + |
| 113 | + expectJSON(result).toDeepEqual({ |
| 114 | + data: { |
| 115 | + todo: { |
| 116 | + id: '1', |
| 117 | + author: null, |
| 118 | + }, |
| 119 | + }, |
| 120 | + errors: [ |
| 121 | + { |
| 122 | + message: 'Aborted', |
| 123 | + path: ['todo', 'author', 'id'], |
| 124 | + locations: [{ line: 6, column: 13 }], |
| 125 | + }, |
| 126 | + ], |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should stop deferred execution when aborted', async () => { |
| 131 | + const abortController = new AbortController(); |
| 132 | + const document = parse(` |
| 133 | + query { |
| 134 | + todo { |
| 135 | + id |
| 136 | + ... on Todo @defer { |
| 137 | + text |
| 138 | + author { |
| 139 | + id |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + `); |
| 145 | + |
| 146 | + const resultPromise = execute({ |
| 147 | + document, |
| 148 | + schema, |
| 149 | + rootValue: { |
| 150 | + todo: async () => |
| 151 | + Promise.resolve({ |
| 152 | + id: '1', |
| 153 | + text: 'hello world', |
| 154 | + /* c8 ignore next */ |
| 155 | + author: () => expect.fail('Should not be called'), |
| 156 | + }), |
| 157 | + }, |
| 158 | + abortSignal: abortController.signal, |
| 159 | + }); |
| 160 | + |
| 161 | + abortController.abort('Aborted'); |
| 162 | + |
| 163 | + const result = await resultPromise; |
| 164 | + |
| 165 | + expectJSON(result).toDeepEqual({ |
| 166 | + data: { |
| 167 | + todo: null, |
| 168 | + }, |
| 169 | + errors: [ |
| 170 | + { |
| 171 | + message: 'Aborted', |
| 172 | + path: ['todo', 'id'], |
| 173 | + locations: [{ line: 4, column: 11 }], |
| 174 | + }, |
| 175 | + ], |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should stop the execution when aborted mid-mutation', async () => { |
| 180 | + const abortController = new AbortController(); |
| 181 | + const document = parse(` |
| 182 | + mutation { |
| 183 | + foo |
| 184 | + bar |
| 185 | + } |
| 186 | + `); |
| 187 | + |
| 188 | + const resultPromise = execute({ |
| 189 | + document, |
| 190 | + schema, |
| 191 | + abortSignal: abortController.signal, |
| 192 | + rootValue: { |
| 193 | + foo: async () => Promise.resolve('baz'), |
| 194 | + /* c8 ignore next */ |
| 195 | + bar: () => expect.fail('Should not be called'), |
| 196 | + }, |
| 197 | + }); |
| 198 | + |
| 199 | + abortController.abort('Aborted'); |
| 200 | + |
| 201 | + const result = await resultPromise; |
| 202 | + |
| 203 | + expectJSON(result).toDeepEqual({ |
| 204 | + data: { |
| 205 | + foo: 'baz', |
| 206 | + bar: null, |
| 207 | + }, |
| 208 | + errors: [ |
| 209 | + { |
| 210 | + message: 'Aborted', |
| 211 | + path: ['bar'], |
| 212 | + locations: [{ line: 4, column: 9 }], |
| 213 | + }, |
| 214 | + ], |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should stop the execution when aborted pre-execute', async () => { |
| 219 | + const abortController = new AbortController(); |
| 220 | + const document = parse(` |
| 221 | + query { |
| 222 | + todo { |
| 223 | + id |
| 224 | + author { |
| 225 | + id |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + `); |
| 230 | + abortController.abort('Aborted'); |
| 231 | + const result = await execute({ |
| 232 | + document, |
| 233 | + schema, |
| 234 | + abortSignal: abortController.signal, |
| 235 | + rootValue: { |
| 236 | + /* c8 ignore next */ |
| 237 | + todo: () => expect.fail('Should not be called'), |
| 238 | + }, |
| 239 | + }); |
| 240 | + |
| 241 | + expectJSON(result).toDeepEqual({ |
| 242 | + errors: [ |
| 243 | + { |
| 244 | + message: 'Aborted', |
| 245 | + }, |
| 246 | + ], |
| 247 | + }); |
| 248 | + }); |
| 249 | +}); |
0 commit comments