@@ -5,20 +5,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
66exports . GraphQLQueryPurifier = void 0 ;
77const fs_1 = __importDefault ( require ( "fs" ) ) ;
8- const graphql_1 = require ( "graphql" ) ;
98const path_1 = __importDefault ( require ( "path" ) ) ;
109// @ts -ignore
1110const glob_1 = __importDefault ( require ( "glob" ) ) ;
1211const merge_1 = require ( "./merge" ) ;
12+ const merge_allowed_1 = require ( "./merge-allowed" ) ;
1313class GraphQLQueryPurifier {
1414 /**
1515 * Constructs a GraphQLQueryPurifier instance.
1616 * @param {Object } params - Configuration parameters.
1717 * @param {string } params.gqlPath - Path to the directory containing .gql files.
1818 * @param {boolean } [params.allowAll=false] - Whether to allow all queries.
1919 * @param {boolean } [params.allowStudio=false] - Whether to allow Apollo Studio introspection queries.
20+ * @param {boolean } [params.debug=false] - Flag to enable logging of input/output.
2021 */
21- constructor ( { gqlPath, allowAll = false , allowStudio = false , } ) {
22+ constructor ( { gqlPath, allowAll = false , allowStudio = false , debug = false , } ) {
2223 /**
2324 * Middleware function to filter incoming GraphQL queries based on the allowed list.
2425 * If a query is not allowed, it's replaced with a minimal query.
@@ -49,7 +50,7 @@ class GraphQLQueryPurifier {
4950 }
5051 if ( req . body && req . body . query ) {
5152 // Use mergeQueries to filter the incoming request query
52- const filteredQuery = ( 0 , merge_1 . mergeQueries ) ( req . body . query , allowedQueries ) ;
53+ const filteredQuery = ( 0 , merge_1 . mergeQueries ) ( req . body . query , allowedQueries , this . debug ) ;
5354 if ( ! filteredQuery . trim ( ) ) {
5455 console . warn ( `Query was blocked due to security rules: ${ req . body . query } ` ) ;
5556 req . body . query = '{ __typename }' ;
@@ -67,6 +68,7 @@ class GraphQLQueryPurifier {
6768 this . startWatchingFiles ( ) ;
6869 this . allowAll = allowAll ;
6970 this . allowStudio = allowStudio ;
71+ this . debug = debug ;
7072 }
7173 startWatchingFiles ( ) {
7274 fs_1 . default . watch ( this . gqlPath , { recursive : true } , ( eventType , filename ) => {
@@ -82,41 +84,20 @@ class GraphQLQueryPurifier {
8284 */
8385 loadQueries ( ) {
8486 const files = glob_1 . default . sync ( `${ this . gqlPath } /**/*.gql` . replace ( / \\ / g, '/' ) ) ;
85- files . forEach ( ( file ) => {
86- if ( path_1 . default . extname ( file ) === '.gql' ) {
87- const content = fs_1 . default . readFileSync ( file , 'utf8' ) . trim ( ) ;
88- if ( ! content ) {
89- console . warn ( `Warning: Empty or invalid GraphQL file found: ${ file } ` ) ;
90- return ;
91- }
92- try {
93- const parsedQuery = ( 0 , graphql_1 . parse ) ( content ) ;
94- parsedQuery . definitions . forEach ( ( definition ) => {
95- if ( definition . kind === 'OperationDefinition' ) {
96- const operationDefinition = definition ;
97- let queryName = operationDefinition . name ?. value ;
98- if ( ! queryName ) {
99- // Extract the name from the first field of the selection set
100- const firstField = operationDefinition . selectionSet . selections [ 0 ] ;
101- if ( firstField && firstField . kind === 'Field' ) {
102- queryName = firstField . name . value ;
103- }
104- }
105- if ( queryName ) {
106- this . queryMap [ queryName ] = content ;
107- }
108- }
109- } ) ;
110- }
111- catch ( error ) {
112- if ( error instanceof graphql_1 . GraphQLError ) {
113- console . error ( `Error parsing GraphQL file ${ file } : ${ error . message } ` ) ;
114- }
115- else {
116- console . error ( `Unexpected error processing file ${ file } : ${ error } ` ) ;
117- }
118- }
87+ const fileContents = files
88+ . map ( ( file ) => {
89+ const content = fs_1 . default . readFileSync ( file , 'utf8' ) . trim ( ) ;
90+ if ( ! content ) {
91+ console . warn ( `Warning: Empty or invalid GraphQL file found: ${ file } ` ) ;
92+ return '' ;
11993 }
94+ return content ;
95+ } )
96+ . filter ( ( content ) => content !== '' ) ;
97+ const mergedQueries = ( 0 , merge_allowed_1 . mergeAllowedGraphQLQueries ) ( fileContents ) ;
98+ this . queryMap = { } ;
99+ mergedQueries . forEach ( ( query , resolver ) => {
100+ this . queryMap [ resolver ] = query ;
120101 } ) ;
121102 }
122103}
0 commit comments