Skip to content

Commit

Permalink
Fix GraphQLFileLoader #import syntax handling (#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrearden committed Jul 22, 2020
1 parent 7d6b1f9 commit b49acfb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
16 changes: 15 additions & 1 deletion packages/loaders/graphql-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
import { isAbsolute, resolve } from 'path';
import { readFile, readFileSync, pathExists, pathExistsSync } from 'fs-extra';
import { cwd as processCwd } from 'process';
import { isExecutableDefinitionNode, Kind } from 'graphql';
import { processImport } from '@graphql-tools/import';
import { mergeTypeDefs } from '@graphql-tools/merge';

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

Expand Down Expand Up @@ -97,9 +99,21 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio

handleFileContent(rawSDL: string, pointer: string, options: GraphQLFileLoaderOptions) {
if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
const document = processImport(pointer, options.cwd);
const typeSystemDefinitions = document.definitions
.filter(d => !isExecutableDefinitionNode(d))
.map(definition => ({
kind: Kind.DOCUMENT,
definitions: [definition],
}));
const mergedTypeDefs = mergeTypeDefs(typeSystemDefinitions, { useSchemaDefinition: false });
const executableDefinitions = document.definitions.filter(isExecutableDefinitionNode);
return {
location: pointer,
document: processImport(pointer, options.cwd),
document: {
...mergedTypeDefs,
definitions: [...mergedTypeDefs.definitions, ...executableDefinitions],
},
};
}
return parseGraphQLSDL(pointer, rawSDL.trim(), options);
Expand Down
29 changes: 27 additions & 2 deletions packages/loaders/graphql-file/tests/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { join } from 'path';

import { print } from 'graphql'
import { Source } from '@graphql-tools/utils';

import { GraphQLFileLoader } from '../src';
import { runTests } from '../../../testing/utils';
import '../../../testing/to-be-similar-gql-doc'

describe('GraphQLFileLoader', () => {
const loader = new GraphQLFileLoader();
Expand Down Expand Up @@ -59,9 +61,32 @@ describe('GraphQLFileLoader', () => {
expect(result.document).toBeDefined();
});

it('should load file with #import expression', async () => {
it('should load type definitions document with #import expression', async () => {
const result: Source = await load(getPointer('type-defs-with-import.graphql'), {});
expect(result.document?.definitions.length).toBe(2);
expect(print(result.document!)).toBeSimilarGqlDoc(/* GraphQL */`
type Query {
a: A
}
type A {
b: String
}
`)
});

it('should load executable document with #import expression', async () => {
const result: Source = await load(getPointer('executable.graphql'), {});
expect(print(result.document!)).toBeSimilarGqlDoc(/* GraphQL */`
query MyQuery {
a {
...AFragment
}
}
fragment AFragment on A {
b
}
`)
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import AFragment from "./fragment.graphql"

query MyQuery {
a {
...AFragment
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment AFragment on A {
b
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type Mutation {
sayGoodbye: String
type A {
b: String
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import Mutation from "./import-me.graphql"
#import A from "./import-me.graphql"

type Query {
hello: String
a: A
}

0 comments on commit b49acfb

Please sign in to comment.