Skip to content

Commit b49acfb

Browse files
Fix GraphQLFileLoader #import syntax handling (#1808)
1 parent 7d6b1f9 commit b49acfb

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

packages/loaders/graphql-file/src/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
import { isAbsolute, resolve } from 'path';
1111
import { readFile, readFileSync, pathExists, pathExistsSync } from 'fs-extra';
1212
import { cwd as processCwd } from 'process';
13+
import { isExecutableDefinitionNode, Kind } from 'graphql';
1314
import { processImport } from '@graphql-tools/import';
15+
import { mergeTypeDefs } from '@graphql-tools/merge';
1416

1517
const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
1618

@@ -97,9 +99,21 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
9799

98100
handleFileContent(rawSDL: string, pointer: string, options: GraphQLFileLoaderOptions) {
99101
if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
102+
const document = processImport(pointer, options.cwd);
103+
const typeSystemDefinitions = document.definitions
104+
.filter(d => !isExecutableDefinitionNode(d))
105+
.map(definition => ({
106+
kind: Kind.DOCUMENT,
107+
definitions: [definition],
108+
}));
109+
const mergedTypeDefs = mergeTypeDefs(typeSystemDefinitions, { useSchemaDefinition: false });
110+
const executableDefinitions = document.definitions.filter(isExecutableDefinitionNode);
100111
return {
101112
location: pointer,
102-
document: processImport(pointer, options.cwd),
113+
document: {
114+
...mergedTypeDefs,
115+
definitions: [...mergedTypeDefs.definitions, ...executableDefinitions],
116+
},
103117
};
104118
}
105119
return parseGraphQLSDL(pointer, rawSDL.trim(), options);

packages/loaders/graphql-file/tests/loader.spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { join } from 'path';
22

3+
import { print } from 'graphql'
34
import { Source } from '@graphql-tools/utils';
45

56
import { GraphQLFileLoader } from '../src';
67
import { runTests } from '../../../testing/utils';
8+
import '../../../testing/to-be-similar-gql-doc'
79

810
describe('GraphQLFileLoader', () => {
911
const loader = new GraphQLFileLoader();
@@ -59,9 +61,32 @@ describe('GraphQLFileLoader', () => {
5961
expect(result.document).toBeDefined();
6062
});
6163

62-
it('should load file with #import expression', async () => {
64+
it('should load type definitions document with #import expression', async () => {
6365
const result: Source = await load(getPointer('type-defs-with-import.graphql'), {});
64-
expect(result.document?.definitions.length).toBe(2);
66+
expect(print(result.document!)).toBeSimilarGqlDoc(/* GraphQL */`
67+
type Query {
68+
a: A
69+
}
70+
71+
type A {
72+
b: String
73+
}
74+
`)
75+
});
76+
77+
it('should load executable document with #import expression', async () => {
78+
const result: Source = await load(getPointer('executable.graphql'), {});
79+
expect(print(result.document!)).toBeSimilarGqlDoc(/* GraphQL */`
80+
query MyQuery {
81+
a {
82+
...AFragment
83+
}
84+
}
85+
86+
fragment AFragment on A {
87+
b
88+
}
89+
`)
6590
});
6691
});
6792
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import AFragment from "./fragment.graphql"
2+
3+
query MyQuery {
4+
a {
5+
...AFragment
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment AFragment on A {
2+
b
3+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
type Mutation {
2-
sayGoodbye: String
1+
type A {
2+
b: String
33
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#import Mutation from "./import-me.graphql"
1+
#import A from "./import-me.graphql"
22

33
type Query {
4-
hello: String
4+
a: A
55
}

0 commit comments

Comments
 (0)