From f5bdb5811e6eebde5a19a849d18c1dbc463e3842 Mon Sep 17 00:00:00 2001 From: Trevor Livingston Date: Tue, 20 Sep 2022 20:12:04 +0000 Subject: [PATCH 1/4] Adding support for module import paths --- jest.config.js | 8 +++++++- packages/import/src/index.ts | 12 ++++++++++++ .../tests/schema/fixtures/require-paths/a.graphql | 3 +++ .../tests/schema/fixtures/require-paths/b.graphql | 5 +++++ packages/import/tests/schema/import-schema.spec.ts | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 packages/import/tests/schema/fixtures/require-paths/a.graphql create mode 100644 packages/import/tests/schema/fixtures/require-paths/b.graphql diff --git a/jest.config.js b/jest.config.js index 10ab33a5124..fd287c6ce32 100644 --- a/jest.config.js +++ b/jest.config.js @@ -8,13 +8,19 @@ const tsconfig = require(TSCONFIG); const ESM_PACKAGES = ['graphql', 'graphql-upload', 'fs-capacitor']; +const moduleNameMap = { + //This line is to enable testing import with require.resolve, which would normally get intercepted + '^@graphql\-tools\/import\/(.*).graphql$': `${ROOT_DIR}/packages/import/$1.graphql`, + ...pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: `${ROOT_DIR}/` }) +}; + module.exports = { testEnvironment: 'node', rootDir: ROOT_DIR, restoreMocks: true, reporters: ['default'], modulePathIgnorePatterns: ['dist', 'test-assets', 'test-files', 'fixtures', '.bob'], - moduleNameMapper: pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: `${ROOT_DIR}/` }), + moduleNameMapper: moduleNameMap, collectCoverage: false, cacheDirectory: resolve(ROOT_DIR, `${CI ? '' : 'node_modules/'}.cache/jest`), transform: { diff --git a/packages/import/src/index.ts b/packages/import/src/index.ts index 81da855f431..d77024f0363 100644 --- a/packages/import/src/index.ts +++ b/packages/import/src/index.ts @@ -93,12 +93,24 @@ export function processImport( }; } +function isRequirePath(filePath: string): boolean { + return filePath.startsWith('require:'); +} + +function resolveRequirePath(filePath: string): string { + return require.resolve(filePath.slice(8)); +} + function visitFile( filePath: string, cwd: string, visitedFiles: VisitedFilesMap, predefinedImports: Record ): Map> { + // Support paths to npm modules + if (isRequirePath(filePath)) { + filePath = resolveRequirePath(filePath); + } if (!isAbsolute(filePath) && !(filePath in predefinedImports)) { filePath = resolveFilePath(cwd, filePath); } diff --git a/packages/import/tests/schema/fixtures/require-paths/a.graphql b/packages/import/tests/schema/fixtures/require-paths/a.graphql new file mode 100644 index 00000000000..207278c700f --- /dev/null +++ b/packages/import/tests/schema/fixtures/require-paths/a.graphql @@ -0,0 +1,3 @@ +type A { + field: String +} diff --git a/packages/import/tests/schema/fixtures/require-paths/b.graphql b/packages/import/tests/schema/fixtures/require-paths/b.graphql new file mode 100644 index 00000000000..02c95a6b64a --- /dev/null +++ b/packages/import/tests/schema/fixtures/require-paths/b.graphql @@ -0,0 +1,5 @@ +# import A from "require:@graphql-tools/import/tests/schema/fixtures/require-paths/a.graphql" + +type B { + a: A +} diff --git a/packages/import/tests/schema/import-schema.spec.ts b/packages/import/tests/schema/import-schema.spec.ts index d872bcb2b17..bb90f0ed034 100644 --- a/packages/import/tests/schema/import-schema.spec.ts +++ b/packages/import/tests/schema/import-schema.spec.ts @@ -650,6 +650,20 @@ describe('importSchema', () => { expect(actualSDL).toBeSimilarGqlDoc(expectedSDL); }); + test('require paths', () => { + const expectedSDL = /* GraphQL */ ` + type A { + field: String + } + + type B { + a: A + } + `; + const actualSDL = importSchema('./fixtures/require-paths/b.graphql'); + expect(actualSDL).toBeSimilarGqlDoc(expectedSDL); + }); + test('root field imports', () => { const expectedSDL = /* GraphQL */ ` type Query { From 81428c3ce494d5ad851c15968cfc53a63679f7c0 Mon Sep 17 00:00:00 2001 From: Trevor Livingston Date: Tue, 20 Sep 2022 20:33:22 +0000 Subject: [PATCH 2/4] added changeset --- .changeset/violet-cows-impress.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/violet-cows-impress.md diff --git a/.changeset/violet-cows-impress.md b/.changeset/violet-cows-impress.md new file mode 100644 index 00000000000..682dcc08210 --- /dev/null +++ b/.changeset/violet-cows-impress.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/import': minor +--- + +Minor bump to support import installed modules From 8adf5fe1362f4c902fa65ea0caf7e8e9c0a5119b Mon Sep 17 00:00:00 2001 From: Trevor Livingston Date: Tue, 20 Sep 2022 20:38:11 +0000 Subject: [PATCH 3/4] Adding documentation --- website/docs/schema-loading.mdx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/docs/schema-loading.mdx b/website/docs/schema-loading.mdx index b68a1ac2104..aac6b4f3b05 100644 --- a/website/docs/schema-loading.mdx +++ b/website/docs/schema-loading.mdx @@ -139,6 +139,14 @@ type Comment { } ``` +Import also supports paths to installed modules. Example: + +```graphql +# import Post, Comment from "require:blog-graphql-types/schema.graphql +``` + +Note: this functionality resolves using `require.resolve` on matching paths. + ### Binding to HTTP Server You can extend loaded schema with resolvers From be558cad70ee0ade4f159ce52935477effe7786b Mon Sep 17 00:00:00 2001 From: Trevor Livingston Date: Tue, 20 Sep 2022 20:39:40 +0000 Subject: [PATCH 4/4] Prettier --- jest.config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jest.config.js b/jest.config.js index fd287c6ce32..80be0dd9b9e 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,8 +10,8 @@ const ESM_PACKAGES = ['graphql', 'graphql-upload', 'fs-capacitor']; const moduleNameMap = { //This line is to enable testing import with require.resolve, which would normally get intercepted - '^@graphql\-tools\/import\/(.*).graphql$': `${ROOT_DIR}/packages/import/$1.graphql`, - ...pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: `${ROOT_DIR}/` }) + '^@graphql-tools/import/(.*).graphql$': `${ROOT_DIR}/packages/import/$1.graphql`, + ...pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: `${ROOT_DIR}/` }), }; module.exports = {