1- "use strict" ; var e = this && this . __importDefault || function ( e ) { return e && e . __esModule ?e :{ default :e } } ; Object . defineProperty ( exports , "__esModule" , { value :! 0 } ) , exports . GraphQLQueryPurifier = void 0 ; const r = e ( require ( "fs" ) ) , i = e ( require ( "glob" ) ) , t = require ( "graphql" ) , u = e ( require ( "path" ) ) , s = require ( "./merge" ) ; class a { constructor ( e ) { this . filter = ( e , r , i ) => { if ( e . body && e . body . query ) { const r = Object . values ( this . queryMap ) ; e . body . query = ( 0 , s . mergeQueries ) ( e . body . query , r ) } i ( ) } , this . gqlPath = e , this . queryMap = { } , this . loadQueries ( ) } loadQueries ( ) { i . default . sync ( `${ this . gqlPath } /**/*.gql` ) . forEach ( ( e => { if ( ".gql" === u . default . extname ( e ) ) { const i = r . default . readFileSync ( e , "utf8" ) , u = ( 0 , t . parse ) ( i ) . definitions . find ( ( e => "OperationDefinition" === e . kind ) ) , s = u ?. name ?. value ; s && ( this . queryMap [ s ] = i ) } } ) ) } } exports . GraphQLQueryPurifier = a ;
1+ "use strict" ;
2+ var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
3+ return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
4+ } ;
5+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6+ exports . GraphQLQueryPurifier = void 0 ;
7+ const fs_1 = __importDefault ( require ( "fs" ) ) ;
8+ const glob_1 = __importDefault ( require ( "glob" ) ) ;
9+ const graphql_1 = require ( "graphql" ) ;
10+ const path_1 = __importDefault ( require ( "path" ) ) ;
11+ const merge_1 = require ( "./merge" ) ;
12+ class GraphQLQueryPurifier {
13+ constructor ( { gqlPath, allowAll = false , allowStudio = false , } ) {
14+ this . filter = ( req , res , next ) => {
15+ if ( this . allowAll )
16+ return next ( ) ;
17+ if ( req . body . operationName === 'IntrospectionQuery' ||
18+ req . body . extensions ?. persistedQuery ) {
19+ if ( this . allowStudio ) {
20+ return next ( ) ;
21+ }
22+ else {
23+ return res
24+ . status ( 403 )
25+ . send ( 'Access to studio is disabled by GraphQLQueryPurifier, pass { allowStudio: true }' ) ;
26+ }
27+ }
28+ // Get all allowed queries as an array of strings
29+ const allowedQueries = Object . values ( this . queryMap ) ;
30+ if ( allowedQueries . length === 0 ) {
31+ console . warn ( 'Warning: No GraphQL queries were loaded in GraphQLQueryPurifier. ' +
32+ 'Ensure that your .gql files are located in the specified directory: ' +
33+ this . gqlPath ) ;
34+ return res
35+ . status ( 500 )
36+ . send ( 'GraphQL queries are not loaded. Please check server logs for more details.' ) ;
37+ }
38+ if ( req . body && req . body . query ) {
39+ // Use mergeQueries to filter the incoming request query
40+ req . body . query = ( 0 , merge_1 . mergeQueries ) ( req . body . query , allowedQueries ) ;
41+ }
42+ next ( ) ;
43+ } ;
44+ this . gqlPath = gqlPath ;
45+ this . queryMap = { } ;
46+ this . loadQueries ( ) ;
47+ this . allowAll = allowAll ;
48+ this . allowStudio = allowStudio ;
49+ }
50+ loadQueries ( ) {
51+ const files = glob_1 . default . sync ( `${ this . gqlPath } /**/*.gql` ) ;
52+ files . forEach ( ( file ) => {
53+ if ( path_1 . default . extname ( file ) === '.gql' ) {
54+ const content = fs_1 . default . readFileSync ( file , 'utf8' ) ;
55+ const parsedQuery = ( 0 , graphql_1 . parse ) ( content ) ;
56+ parsedQuery . definitions . forEach ( ( definition ) => {
57+ if ( definition . kind === 'OperationDefinition' ) {
58+ const operationDefinition = definition ;
59+ let queryName = operationDefinition . name ?. value ;
60+ if ( ! queryName ) {
61+ // Extract the name from the first field of the selection set
62+ const firstField = operationDefinition . selectionSet . selections [ 0 ] ;
63+ if ( firstField && firstField . kind === 'Field' ) {
64+ queryName = firstField . name . value ;
65+ }
66+ }
67+ if ( queryName ) {
68+ this . queryMap [ queryName ] = content ;
69+ }
70+ }
71+ } ) ;
72+ }
73+ } ) ;
74+ }
75+ }
76+ exports . GraphQLQueryPurifier = GraphQLQueryPurifier ;
0 commit comments