@@ -1442,6 +1442,167 @@ describe('Codegen Executor', () => {
14421442 } ) ;
14431443 } ) ;
14441444
1445+ describe ( 'documentsReadOnly' , ( ) => {
1446+ it ( 'should pass documentsReadOnly to preset buildGeneratesSection' , async ( ) => {
1447+ let capturedDocumentsReadOnly : Types . DocumentFile [ ] | undefined ;
1448+
1449+ const capturePreset : Types . OutputPreset = {
1450+ buildGeneratesSection : options => {
1451+ capturedDocumentsReadOnly = options . documentsReadOnly ;
1452+ return [
1453+ {
1454+ filename : 'out1/result.ts' ,
1455+ pluginMap : { typescript : require ( '@graphql-codegen/typescript' ) } ,
1456+ plugins : [ { typescript : { } } ] ,
1457+ schema : options . schema ,
1458+ documents : options . documents ,
1459+ config : options . config ,
1460+ } ,
1461+ ] ;
1462+ } ,
1463+ } ;
1464+
1465+ await executeCodegen ( {
1466+ schema : SIMPLE_TEST_SCHEMA ,
1467+ documents : `query root { f }` ,
1468+ documentsReadOnly : `fragment Frag on MyType { f }` ,
1469+ generates : {
1470+ 'out1/' : { preset : capturePreset } ,
1471+ } ,
1472+ } ) ;
1473+
1474+ expect ( capturedDocumentsReadOnly ) . toBeDefined ( ) ;
1475+ expect ( capturedDocumentsReadOnly ) . toHaveLength ( 1 ) ;
1476+ } ) ;
1477+
1478+ it ( 'should not include documentsReadOnly content in regular documents' , async ( ) => {
1479+ let capturedDocuments : Types . DocumentFile [ ] | undefined ;
1480+ let capturedDocumentsReadOnly : Types . DocumentFile [ ] | undefined ;
1481+
1482+ const capturePreset : Types . OutputPreset = {
1483+ buildGeneratesSection : options => {
1484+ capturedDocuments = options . documents ;
1485+ capturedDocumentsReadOnly = options . documentsReadOnly ;
1486+ return [
1487+ {
1488+ filename : 'out1/result.ts' ,
1489+ pluginMap : { typescript : require ( '@graphql-codegen/typescript' ) } ,
1490+ plugins : [ { typescript : { } } ] ,
1491+ schema : options . schema ,
1492+ documents : options . documents ,
1493+ config : options . config ,
1494+ } ,
1495+ ] ;
1496+ } ,
1497+ } ;
1498+
1499+ await executeCodegen ( {
1500+ schema : SIMPLE_TEST_SCHEMA ,
1501+ documents : `query root { f }` ,
1502+ documentsReadOnly : `query readOnlyQuery { f }` ,
1503+ generates : {
1504+ 'out1/' : { preset : capturePreset } ,
1505+ } ,
1506+ } ) ;
1507+
1508+ expect ( capturedDocuments ) . toHaveLength ( 1 ) ;
1509+ expect ( capturedDocumentsReadOnly ) . toHaveLength ( 1 ) ;
1510+
1511+ const documentNames = capturedDocuments . flatMap (
1512+ d => d . document ?. definitions . map ( ( def : any ) => def . name ?. value ) ?? [ ]
1513+ ) ;
1514+ const readOnlyNames = capturedDocumentsReadOnly . flatMap (
1515+ d => d . document ?. definitions . map ( ( def : any ) => def . name ?. value ) ?? [ ]
1516+ ) ;
1517+
1518+ expect ( documentNames ) . toContain ( 'root' ) ;
1519+ expect ( documentNames ) . not . toContain ( 'readOnlyQuery' ) ;
1520+ expect ( readOnlyNames ) . toContain ( 'readOnlyQuery' ) ;
1521+ expect ( readOnlyNames ) . not . toContain ( 'root' ) ;
1522+ } ) ;
1523+
1524+ it ( 'should not include documentsReadOnly operations in non-preset plugin output' , async ( ) => {
1525+ const { result } = await executeCodegen ( {
1526+ schema : SIMPLE_TEST_SCHEMA ,
1527+ documents : `query root { f }` ,
1528+ documentsReadOnly : `query readOnlyQuery { f }` ,
1529+ generates : {
1530+ 'out1.ts' : { plugins : [ 'typescript-operations' ] } ,
1531+ } ,
1532+ } ) ;
1533+
1534+ expect ( result ) . toHaveLength ( 1 ) ;
1535+ // Only the regular document operation should be generated
1536+ expect ( result [ 0 ] . content ) . toContain ( 'RootQuery' ) ;
1537+ expect ( result [ 0 ] . content ) . not . toContain ( 'ReadOnlyQuery' ) ;
1538+ } ) ;
1539+
1540+ it ( 'should support output-level documentsReadOnly' , async ( ) => {
1541+ let capturedDocumentsReadOnly : Types . DocumentFile [ ] | undefined ;
1542+
1543+ const capturePreset : Types . OutputPreset = {
1544+ buildGeneratesSection : options => {
1545+ capturedDocumentsReadOnly = options . documentsReadOnly ;
1546+ return [
1547+ {
1548+ filename : 'out1/result.ts' ,
1549+ pluginMap : { typescript : require ( '@graphql-codegen/typescript' ) } ,
1550+ plugins : [ { typescript : { } } ] ,
1551+ schema : options . schema ,
1552+ documents : options . documents ,
1553+ config : options . config ,
1554+ } ,
1555+ ] ;
1556+ } ,
1557+ } ;
1558+
1559+ await executeCodegen ( {
1560+ schema : SIMPLE_TEST_SCHEMA ,
1561+ generates : {
1562+ 'out1/' : {
1563+ preset : capturePreset ,
1564+ documentsReadOnly : `fragment Frag on MyType { f }` ,
1565+ } ,
1566+ } ,
1567+ } ) ;
1568+
1569+ expect ( capturedDocumentsReadOnly ) . toHaveLength ( 1 ) ;
1570+ } ) ;
1571+
1572+ it ( 'should merge root and output-level documentsReadOnly' , async ( ) => {
1573+ let capturedDocumentsReadOnly : Types . DocumentFile [ ] | undefined ;
1574+
1575+ const capturePreset : Types . OutputPreset = {
1576+ buildGeneratesSection : options => {
1577+ capturedDocumentsReadOnly = options . documentsReadOnly ;
1578+ return [
1579+ {
1580+ filename : 'out1/result.ts' ,
1581+ pluginMap : { typescript : require ( '@graphql-codegen/typescript' ) } ,
1582+ plugins : [ { typescript : { } } ] ,
1583+ schema : options . schema ,
1584+ documents : options . documents ,
1585+ config : options . config ,
1586+ } ,
1587+ ] ;
1588+ } ,
1589+ } ;
1590+
1591+ await executeCodegen ( {
1592+ schema : SIMPLE_TEST_SCHEMA ,
1593+ documentsReadOnly : `fragment RootFrag on MyType { f }` ,
1594+ generates : {
1595+ 'out1/' : {
1596+ preset : capturePreset ,
1597+ documentsReadOnly : `fragment OutputFrag on MyType { f }` ,
1598+ } ,
1599+ } ,
1600+ } ) ;
1601+
1602+ expect ( capturedDocumentsReadOnly ) . toHaveLength ( 2 ) ;
1603+ } ) ;
1604+ } ) ;
1605+
14451606 it ( 'should not run out of memory when generating very complex types (issue #7720)' , async ( ) => {
14461607 const { result } = await executeCodegen ( {
14471608 schema : [ '../../dev-test/gatsby/schema.graphql' ] ,
0 commit comments