@@ -1296,6 +1296,166 @@ describe('Cache', () => {
1296
1296
) ;
1297
1297
} ) ;
1298
1298
1299
+ describe ( "cache.updateQuery and cache.updateFragment" , ( ) => {
1300
+ it ( 'should be batched' , ( ) => {
1301
+ const cache = new InMemoryCache ( {
1302
+ typePolicies : {
1303
+ Person : {
1304
+ keyFields : [ "name" ] ,
1305
+ } ,
1306
+ } ,
1307
+ } ) ;
1308
+
1309
+ type QueryData = {
1310
+ me : {
1311
+ __typename : string ;
1312
+ name : string ;
1313
+ } ,
1314
+ } ;
1315
+
1316
+ const query : TypedDocumentNode < QueryData > = gql `query { me { name } }` ;
1317
+ const results : QueryData [ ] = [ ] ;
1318
+
1319
+ const cancel = cache . watch ( {
1320
+ query,
1321
+ optimistic : true ,
1322
+ callback ( diff ) {
1323
+ results . push ( diff . result ! ) ;
1324
+ } ,
1325
+ } ) ;
1326
+
1327
+ cache . updateQuery ( { query } , data => {
1328
+ expect ( data ) . toBe ( null ) ;
1329
+
1330
+ cache . writeQuery ( {
1331
+ query,
1332
+ data : {
1333
+ me : {
1334
+ __typename : "Person" ,
1335
+ name : "Ben" ,
1336
+ } ,
1337
+ } ,
1338
+ } ) ;
1339
+
1340
+ return {
1341
+ me : {
1342
+ __typename : "Person" ,
1343
+ name : "Ben Newman" ,
1344
+ } ,
1345
+ } ;
1346
+ } ) ;
1347
+
1348
+ expect ( results ) . toEqual ( [
1349
+ { me : { __typename : "Person" , name : "Ben Newman" } } ,
1350
+ ] ) ;
1351
+
1352
+ expect ( cache . extract ( ) ) . toEqual ( {
1353
+ 'Person:{"name":"Ben Newman"}' : {
1354
+ __typename : "Person" ,
1355
+ name : "Ben Newman" ,
1356
+ } ,
1357
+ 'Person:{"name":"Ben"}' : {
1358
+ __typename : "Person" ,
1359
+ name : "Ben" ,
1360
+ } ,
1361
+ ROOT_QUERY : {
1362
+ __typename : "Query" ,
1363
+ me : {
1364
+ __ref : 'Person:{"name":"Ben Newman"}' ,
1365
+ } ,
1366
+ } ,
1367
+ } ) ;
1368
+
1369
+ const usernameFragment = gql `
1370
+ fragment UsernameFragment on Person {
1371
+ username
1372
+ }
1373
+ ` ;
1374
+
1375
+ const bnId = cache . identify ( {
1376
+ __typename : "Person" ,
1377
+ name : "Ben Newman" ,
1378
+ } ) ;
1379
+
1380
+ cache . updateFragment ( {
1381
+ id : bnId ,
1382
+ fragment : usernameFragment ,
1383
+ returnPartialData : true ,
1384
+ } , data => {
1385
+ expect ( data ) . toEqual ( {
1386
+ __typename : "Person" ,
1387
+ } ) ;
1388
+
1389
+ cache . writeQuery ( {
1390
+ query,
1391
+ data : {
1392
+ me : {
1393
+ __typename : "Person" ,
1394
+ name : "Brian Kim" ,
1395
+ } ,
1396
+ } ,
1397
+ } ) ;
1398
+
1399
+ cache . writeFragment ( {
1400
+ id : cache . identify ( {
1401
+ __typename : "Person" ,
1402
+ name : "Brian Kim" ,
1403
+ } ) ,
1404
+ fragment : usernameFragment ,
1405
+ data : {
1406
+ username : "brainkim" ,
1407
+ } ,
1408
+ } ) ;
1409
+
1410
+ expect ( results . length ) . toBe ( 1 ) ;
1411
+
1412
+ return {
1413
+ ...data ,
1414
+ name : "Ben Newman" ,
1415
+ username : "benjamn" ,
1416
+ } ;
1417
+ } ) ;
1418
+
1419
+ // Still just two results, thanks to cache.update{Query,Fragment} using
1420
+ // cache.batch behind the scenes.
1421
+ expect ( results ) . toEqual ( [
1422
+ { me : { __typename : "Person" , name : "Ben Newman" } } ,
1423
+ { me : { __typename : "Person" , name : "Brian Kim" } } ,
1424
+ ] ) ;
1425
+
1426
+ expect ( cache . extract ( ) ) . toEqual ( {
1427
+ 'Person:{"name":"Ben"}' : {
1428
+ __typename : "Person" ,
1429
+ name : "Ben" ,
1430
+ } ,
1431
+ 'Person:{"name":"Ben Newman"}' : {
1432
+ __typename : "Person" ,
1433
+ name : "Ben Newman" ,
1434
+ username : "benjamn" ,
1435
+ } ,
1436
+ 'Person:{"name":"Brian Kim"}' : {
1437
+ __typename : "Person" ,
1438
+ name : "Brian Kim" ,
1439
+ username : "brainkim" ,
1440
+ } ,
1441
+ ROOT_QUERY : {
1442
+ __typename : "Query" ,
1443
+ me : {
1444
+ __ref : 'Person:{"name":"Brian Kim"}' ,
1445
+ } ,
1446
+ } ,
1447
+ __META : {
1448
+ extraRootIds : [
1449
+ 'Person:{"name":"Ben Newman"}' ,
1450
+ 'Person:{"name":"Brian Kim"}' ,
1451
+ ] ,
1452
+ } ,
1453
+ } ) ;
1454
+
1455
+ cancel ( ) ;
1456
+ } ) ;
1457
+ } ) ;
1458
+
1299
1459
describe ( 'cache.restore' , ( ) => {
1300
1460
it ( 'replaces cache.{store{Reader,Writer},maybeBroadcastWatch}' , ( ) => {
1301
1461
const cache = new InMemoryCache ;
0 commit comments