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

Fix GraphQLFileLoader #import syntax handling #1808

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}