Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for module import paths #4723

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-cows-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/import': minor
---

Minor bump to support import installed modules
8 changes: 7 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
12 changes: 12 additions & 0 deletions packages/import/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
): Map<string, Set<DefinitionNode>> {
// Support paths to npm modules
if (isRequirePath(filePath)) {
filePath = resolveRequirePath(filePath);
}
if (!isAbsolute(filePath) && !(filePath in predefinedImports)) {
filePath = resolveFilePath(cwd, filePath);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/import/tests/schema/fixtures/require-paths/a.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type A {
field: String
}
5 changes: 5 additions & 0 deletions packages/import/tests/schema/fixtures/require-paths/b.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# import A from "require:@graphql-tools/import/tests/schema/fixtures/require-paths/a.graphql"

type B {
a: A
}
14 changes: 14 additions & 0 deletions packages/import/tests/schema/import-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/schema-loading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down