@@ -5,11 +5,13 @@ const { ExtractGQL } = require('persistgraphql/lib/src/ExtractGQL');
55const queryTransformers = require ( 'persistgraphql/lib/src/queryTransformers' ) ;
66const loadModuleRecursively = require ( './load-module-recursively' ) ;
77
8- module . exports = function graphQLpersistedDocumentLoader ( content ) {
8+ module . exports = function graphQLPersistedDocumentLoader ( content ) {
99 const deps = [ ] ;
1010 const context = this ;
1111 const options = loaderUtils . getOptions ( this ) || { } ;
1212
13+ // Create a fake sandbox to intercept the query dependencies when
14+ // executing the generated code from the graphql-tag loader.
1315 const sandbox = {
1416 require ( file ) {
1517 deps . push ( new Promise ( ( resolve , reject ) => {
@@ -27,24 +29,38 @@ module.exports = function graphQLpersistedDocumentLoader(content) {
2729 exports : null
2830 }
2931 } ;
32+ // Run the graphql-tag loader generated code to capture the dependencies.
3033 vm . runInNewContext ( content , sandbox ) ;
3134
35+ // Get the query document source from the exported module, and
36+ // save it so that we can use it from other modules that may depend on
37+ // this one.
3238 const doc = sandbox . module . exports ;
3339 this . _module . _graphQLQuerySource = doc . loc . source . body ;
3440
3541 if ( deps . length === 0 ) {
42+ // If no deps, just try to generate the document id from the
43+ // source returned from graphql-tag loader result. This will
44+ // add an id only if the source contains an operation.
3645 content = tryAddDocumentId ( options , content , this . _module . _graphQLQuerySource ) ;
3746 return content ;
3847 }
3948
4049 const callback = this . async ( ) ;
4150
51+ // If we have dependencies, we retrieve their query sources and
52+ // concatenate them with the one from this module, to create the
53+ // full query source.
4254 Promise . all ( deps ) . then ( ( modules ) => {
4355 modules . forEach ( ( mod , index ) => {
4456 this . _module . _graphQLQuerySource += mod . module . _graphQLQuerySource ;
4557 } ) ;
4658
4759 try {
60+ // Now that we have all our dependencies' sources concatenated
61+ // with this module's query source, we can send all that to
62+ // generate the document id, if the resulting source
63+ // is for an operation.
4864 content = tryAddDocumentId ( options , content , this . _module . _graphQLQuerySource ) ;
4965 } catch ( e ) {
5066 callback ( e ) ;
0 commit comments