|
1 | | -import { expect } from 'chai'; |
| 1 | +import { assert, expect } from 'chai'; |
2 | 2 | import { describe, it } from 'mocha'; |
3 | 3 |
|
4 | 4 | import { expectJSON } from '../../__testUtils__/expectJSON'; |
5 | 5 | import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick'; |
6 | 6 |
|
| 7 | +import { isAsyncIterable } from '../../jsutils/isAsyncIterable'; |
| 8 | + |
7 | 9 | import { parse } from '../../language/parser'; |
8 | 10 |
|
9 | 11 | import { GraphQLObjectType } from '../../type/definition'; |
@@ -50,6 +52,13 @@ class Root { |
50 | 52 | const numberHolderType = new GraphQLObjectType({ |
51 | 53 | fields: { |
52 | 54 | theNumber: { type: GraphQLInt }, |
| 55 | + promiseToGetTheNumber: { |
| 56 | + type: GraphQLInt, |
| 57 | + resolve: async (root) => { |
| 58 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 59 | + return root.theNumber; |
| 60 | + }, |
| 61 | + }, |
53 | 62 | }, |
54 | 63 | name: 'NumberHolder', |
55 | 64 | }); |
@@ -191,4 +200,122 @@ describe('Execute: Handles mutation execution ordering', () => { |
191 | 200 | ], |
192 | 201 | }); |
193 | 202 | }); |
| 203 | + it('Mutation fields with @defer do not block next mutation', async () => { |
| 204 | + const document = parse(` |
| 205 | + mutation M { |
| 206 | + first: promiseToChangeTheNumber(newNumber: 1) { |
| 207 | + ...DeferFragment @defer(label: "defer-label") |
| 208 | + }, |
| 209 | + second: immediatelyChangeTheNumber(newNumber: 2) { |
| 210 | + theNumber |
| 211 | + } |
| 212 | + } |
| 213 | + fragment DeferFragment on NumberHolder { |
| 214 | + promiseToGetTheNumber |
| 215 | + } |
| 216 | + `); |
| 217 | + |
| 218 | + const rootValue = new Root(6); |
| 219 | + const mutationResult = await execute({ |
| 220 | + schema, |
| 221 | + document, |
| 222 | + rootValue, |
| 223 | + }); |
| 224 | + const patches = []; |
| 225 | + |
| 226 | + assert(isAsyncIterable(mutationResult)); |
| 227 | + for await (const patch of mutationResult) { |
| 228 | + patches.push(patch); |
| 229 | + } |
| 230 | + |
| 231 | + expect(patches).to.deep.equal([ |
| 232 | + { |
| 233 | + data: { |
| 234 | + first: {}, |
| 235 | + second: { theNumber: 2 }, |
| 236 | + }, |
| 237 | + hasNext: true, |
| 238 | + }, |
| 239 | + { |
| 240 | + label: 'defer-label', |
| 241 | + path: ['first'], |
| 242 | + data: { |
| 243 | + promiseToGetTheNumber: 2, |
| 244 | + }, |
| 245 | + hasNext: false, |
| 246 | + }, |
| 247 | + ]); |
| 248 | + }); |
| 249 | + it('Mutation inside of a fragment', async () => { |
| 250 | + const document = parse(` |
| 251 | + mutation M { |
| 252 | + ...MutationFragment |
| 253 | + second: immediatelyChangeTheNumber(newNumber: 2) { |
| 254 | + theNumber |
| 255 | + } |
| 256 | + } |
| 257 | + fragment MutationFragment on Mutation { |
| 258 | + first: promiseToChangeTheNumber(newNumber: 1) { |
| 259 | + theNumber |
| 260 | + }, |
| 261 | + } |
| 262 | + `); |
| 263 | + |
| 264 | + const rootValue = new Root(6); |
| 265 | + const mutationResult = await execute({ schema, document, rootValue }); |
| 266 | + |
| 267 | + expect(mutationResult).to.deep.equal({ |
| 268 | + data: { |
| 269 | + first: { theNumber: 1 }, |
| 270 | + second: { theNumber: 2 }, |
| 271 | + }, |
| 272 | + }); |
| 273 | + }); |
| 274 | + it('Mutation with @defer is not executed serially', async () => { |
| 275 | + const document = parse(` |
| 276 | + mutation M { |
| 277 | + ...MutationFragment @defer(label: "defer-label") |
| 278 | + second: immediatelyChangeTheNumber(newNumber: 2) { |
| 279 | + theNumber |
| 280 | + } |
| 281 | + } |
| 282 | + fragment MutationFragment on Mutation { |
| 283 | + first: promiseToChangeTheNumber(newNumber: 1) { |
| 284 | + theNumber |
| 285 | + }, |
| 286 | + } |
| 287 | + `); |
| 288 | + |
| 289 | + const rootValue = new Root(6); |
| 290 | + const mutationResult = await execute({ |
| 291 | + schema, |
| 292 | + document, |
| 293 | + rootValue, |
| 294 | + }); |
| 295 | + const patches = []; |
| 296 | + |
| 297 | + assert(isAsyncIterable(mutationResult)); |
| 298 | + for await (const patch of mutationResult) { |
| 299 | + patches.push(patch); |
| 300 | + } |
| 301 | + |
| 302 | + expect(patches).to.deep.equal([ |
| 303 | + { |
| 304 | + data: { |
| 305 | + second: { theNumber: 2 }, |
| 306 | + }, |
| 307 | + hasNext: true, |
| 308 | + }, |
| 309 | + { |
| 310 | + label: 'defer-label', |
| 311 | + path: [], |
| 312 | + data: { |
| 313 | + first: { |
| 314 | + theNumber: 1, |
| 315 | + }, |
| 316 | + }, |
| 317 | + hasNext: false, |
| 318 | + }, |
| 319 | + ]); |
| 320 | + }); |
194 | 321 | }); |
0 commit comments