diff --git a/src/ExtractGQL.ts b/src/ExtractGQL.ts index cf1f93f..97e0c1b 100644 --- a/src/ExtractGQL.ts +++ b/src/ExtractGQL.ts @@ -61,7 +61,7 @@ export class ExtractGQL { public queryTransformers: QueryTransformer[] = []; // The file extension to load queries from - public extension: string; + public extension: Array = ['graphql']; // Whether to look for standalone .graphql files or template literals in JavaScript code public inJsCode: boolean = false; @@ -109,13 +109,13 @@ export class ExtractGQL { inputFilePath, outputFilePath = 'extracted_queries.json', queryTransformers = [], - extension = 'graphql', + extension = '', inJsCode = false, }: ExtractGQLOptions) { this.inputFilePath = inputFilePath; this.outputFilePath = outputFilePath; this.queryTransformers = queryTransformers; - this.extension = extension; + this.extension = this.extension.concat(extension.split(',').map(e => e.trim())) this.inJsCode = inJsCode; } @@ -193,8 +193,8 @@ export class ExtractGQL { return Promise.resolve().then(() => { const extension = ExtractGQL.getFileExtension(inputFile); - if (extension === this.extension) { - if (this.inJsCode) { + if (this.extension.indexOf(extension) > -1) { + if (this.inJsCode && extension === 'js') { // Read from a JS file return ExtractGQL.readFile(inputFile).then((result) => { const literalContents = findTaggedTemplateLiteralsInJS(result, this.literalTag); diff --git a/test/fixtures/arbitrary_filetypes/code.js b/test/fixtures/arbitrary_filetypes/code.js new file mode 100644 index 0000000..76b7f2f --- /dev/null +++ b/test/fixtures/arbitrary_filetypes/code.js @@ -0,0 +1,14 @@ +import firstName from './fragment.gql'; +import lastName from './fragment.graphql'; + +const query = gql` +query { + author { + ...firstName + ...lastName + } +} + +${firstName} +${lastName} +`; diff --git a/test/fixtures/arbitrary_filetypes/fragment.gql b/test/fixtures/arbitrary_filetypes/fragment.gql new file mode 100644 index 0000000..5438b28 --- /dev/null +++ b/test/fixtures/arbitrary_filetypes/fragment.gql @@ -0,0 +1,3 @@ +fragment firstName on Author { + firstName +} diff --git a/test/fixtures/arbitrary_filetypes/fragment.graphql b/test/fixtures/arbitrary_filetypes/fragment.graphql new file mode 100644 index 0000000..56205ae --- /dev/null +++ b/test/fixtures/arbitrary_filetypes/fragment.graphql @@ -0,0 +1,3 @@ +fragment lastName on Author { + lastName +} diff --git a/test/fixtures/multiple_filetypes/code.js b/test/fixtures/multiple_filetypes/code.js new file mode 100644 index 0000000..268627a --- /dev/null +++ b/test/fixtures/multiple_filetypes/code.js @@ -0,0 +1,19 @@ +import lastName from './fragment.graphql'; + +const frag = gql` +fragment firstName on Author { + firstName +} +`; + +const query = gql` +query { + author { + ...firstName + ...lastName + } +} + +${frag} +${lastName} +`; diff --git a/test/fixtures/multiple_filetypes/fragment.graphql b/test/fixtures/multiple_filetypes/fragment.graphql new file mode 100644 index 0000000..56205ae --- /dev/null +++ b/test/fixtures/multiple_filetypes/fragment.graphql @@ -0,0 +1,3 @@ +fragment lastName on Author { + lastName +} diff --git a/test/index.ts b/test/index.ts index 78e9b58..a914c16 100644 --- a/test/index.ts +++ b/test/index.ts @@ -446,6 +446,74 @@ describe('ExtractGQL', () => { ); }); }); + + it('should process both a JS file and a graphql file', () => { + const expectedQuery = gql` + query { + author { + ...firstName + ...lastName + } + } + fragment firstName on Author { + firstName + } + fragment lastName on Author { + lastName + } + `; + + const jsEgql = new ExtractGQL({ + inputFilePath: 'idk', + extension: 'js', + inJsCode: true, + outputFilePath: 'idk', + }); + + return jsEgql.processInputPath('./test/fixtures/multiple_filetypes') + .then((result: OutputMap) => { + const keys = Object.keys(result); + assert.equal(keys.length, 1); + assert.equal( + keys[0], + print(expectedQuery) + ); + }); + }); + + it('should process arbitrary file extensions', () => { + const expectedQuery = gql` + query { + author { + ...firstName + ...lastName + } + } + fragment firstName on Author { + firstName + } + fragment lastName on Author { + lastName + } + `; + + const jsEgql = new ExtractGQL({ + inputFilePath: 'idk', + extension: 'js,gql', + inJsCode: true, + outputFilePath: 'idk', + }); + + return jsEgql.processInputPath('./test/fixtures/arbitrary_filetypes') + .then((result: OutputMap) => { + const keys = Object.keys(result); + assert.equal(keys.length, 1); + assert.equal( + keys[0], + print(expectedQuery) + ); + }); + }); it('should process a JS file with queries', () => { const expectedQuery = gql`