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

Fixes issue where base types file is not referenced #2137

Merged
merged 1 commit into from Jul 11, 2019
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
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