Skip to content

Commit

Permalink
Fixes issue where base types file is not referenced (#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvestergaard authored and dotansimha committed Jul 11, 2019
1 parent d3973be commit 5030593
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/presets/near-operation-file/src/utils.ts
@@ -1,5 +1,5 @@
import { parse, dirname, relative, join, isAbsolute } from 'path';
import { DocumentNode, visit, FragmentSpreadNode, FragmentDefinitionNode, FieldNode } from 'graphql';
import { DocumentNode, visit, FragmentSpreadNode, FragmentDefinitionNode, FieldNode, Kind } from 'graphql';
import { FragmentNameToFile } from './index';

export function appendExtensionToFilePath(baseFilePath: string, extension: string) {
Expand Down Expand Up @@ -74,7 +74,7 @@ export function isUsingTypes(document: DocumentNode): boolean {
Field: (node: FieldNode) => {
const selections = node.selectionSet ? node.selectionSet.selections || [] : [];

if (selections.length === 0) {
if (selections.length === 0 || selections[0].kind === Kind.FRAGMENT_SPREAD) {
foundFields++;
}
},
Expand Down
Expand Up @@ -27,6 +27,13 @@ describe('near-operation-file preset', () => {
}
}
`);
const minimalOperationAst = parse(/* GraphQL */ `
query {
user {
...UserFields
}
}
`);
const fragmentAst = parse(/* GraphQL */ `
fragment UserFields on User {
id
Expand Down Expand Up @@ -131,6 +138,23 @@ describe('near-operation-file preset', () => {
expect(result.map(o => o.plugins)[0]).toEqual(expect.arrayContaining([{ add: `import * as Types from '../types';\n` }]));
});

it('Should prepend the "add" plugin with the correct import, when only using fragment spread', async () => {
const result = await preset.buildGeneratesSection({
baseOutputDir: './src/',
config: {},
presetConfig: {
cwd: '/some/deep/path',
baseTypesPath: 'types.ts',
},
schema: schemaDocumentNode,
documents: [{ filePath: '/some/deep/path/src/graphql/me-query.graphql', content: minimalOperationAst }, testDocuments[1]],
plugins: [{ typescript: {} }],
pluginMap: { typescript: {} as any },
});

expect(result.map(o => o.plugins)[0]).toEqual(expect.arrayContaining([{ add: `import * as Types from '../types';\n` }]));
});

it('Should NOT prepend the "add" plugin with Types import when selection set does not include direct fields', async () => {
const result = await preset.buildGeneratesSection({
baseOutputDir: './src/',
Expand Down

0 comments on commit 5030593

Please sign in to comment.