Skip to content

Commit 65757c7

Browse files
committed
Handle unnamed operations
1 parent 48efadb commit 65757c7

5 files changed

Lines changed: 33 additions & 12 deletions

File tree

packages/presets/near-operation-file/src/fragment-resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function buildFragmentRegistry(
101101
}
102102

103103
const fragmentName = fragment.name.value;
104-
const filePath = generateFilePath(documentRecord.location);
104+
const filePath = generateFilePath(documentRecord.location, fragmentName);
105105
const possibleTypes = getPossibleTypes(schemaObject, schemaType);
106106
const possibleTypeNames = possibleTypes.map(t => t.name);
107107
const imports = createFragmentImports(baseVisitor, fragment.name.value, possibleTypeNames);

packages/presets/near-operation-file/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export type NearOperationFileConfig = {
202202
importTypesNamespace?: string;
203203

204204
/**
205-
* @description Optional, generates a file per operation
205+
* @description Optional, generates one file per operation, using the operation name as the filename. Note: if your documents are in `.graphql` files and there are multiple operations or fragments in a single file, the generated filename will be based on the first operation or fragment found.
206206
* @default false
207207
*
208208
* @exampleMarkdown
@@ -264,12 +264,14 @@ export const preset: Types.OutputPreset<NearOperationFileConfig> = {
264264
schemaObject,
265265
{
266266
baseDir,
267-
generateFilePath(location, operationName) {
267+
generateFilePath(location, customFilename) {
268268
const newFilePath = defineFilepathSubfolder(location, folder);
269269

270270
return appendFileNameToFilePath(
271271
newFilePath,
272-
filePerOperation ? operationName : fileName,
272+
filePerOperation
273+
? customFilename // Note: Unnamed operations will cause `operationName` to be undefined. In such case, the generated filename will be based on the source document file.
274+
: fileName,
273275
extension,
274276
);
275277
},

packages/presets/near-operation-file/src/resolve-document-imports.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FragmentDefinitionNode, GraphQLSchema } from 'graphql';
1+
import { FragmentDefinitionNode, GraphQLSchema, Kind, OperationDefinitionNode } from 'graphql';
22
import { isUsingTypes, Types } from '@graphql-codegen/plugin-helpers';
33
import {
44
FragmentImport,
@@ -25,7 +25,7 @@ export type DocumentImportResolverOptions = {
2525
/**
2626
* Generates a target file path from the source `document.location`
2727
*/
28-
generateFilePath: (location: string, operationName: string) => string;
28+
generateFilePath: (location: string, customFilename?: string) => string;
2929
/**
3030
* Schema base types source
3131
*/
@@ -69,12 +69,11 @@ export function resolveDocumentImports<T>(
6969

7070
return documents.map(documentFile => {
7171
try {
72-
// FIXME
73-
const operationName =
74-
documentFile.document.definitions.find(d => d.kind === 'OperationDefinition')?.name
75-
?.value ?? documentFile.document.definitions[0].name.value;
72+
const operationOrFragmentName = documentFile.document.definitions.find(
73+
d => d.kind === Kind.OPERATION_DEFINITION || d.kind === Kind.FRAGMENT_DEFINITION,
74+
)?.name?.value;
7675

77-
const generatedFilePath = generateFilePath(documentFile.location, operationName);
76+
const generatedFilePath = generateFilePath(documentFile.location, operationOrFragmentName);
7877
const importStatements: string[] = [];
7978
const { externalFragments, fragmentImports } = resolveFragments(
8079
generatedFilePath,

packages/presets/near-operation-file/tests/fixtures/file-per-operation.1.graphql.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@
1313
}
1414
}
1515
`;
16+
17+
/* GraphQL */ `
18+
query {
19+
anon: user {
20+
__typename
21+
}
22+
}
23+
`;

packages/presets/near-operation-file/tests/near-operation-file.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ describe('near-operation-file preset', () => {
14381438
},
14391439
});
14401440

1441-
expect(result.length).toBe(5);
1441+
expect(result.length).toBe(6);
14421442

14431443
const file1a = result.find(file => file.filename.endsWith('User1a.generated.ts'));
14441444
expect(file1a.content).toMatchInlineSnapshot(`
@@ -1458,6 +1458,18 @@ describe('near-operation-file preset', () => {
14581458
"
14591459
`);
14601460

1461+
// Unnamed operations falls back to source doc filename
1462+
const file1c = result.find(file =>
1463+
file.filename.endsWith('file-per-operation.1.graphql.generated.ts'),
1464+
);
1465+
expect(file1c.content).toMatchInlineSnapshot(`
1466+
"export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never; }>;
1467+
1468+
1469+
export type Unnamed_1_Query = { __typename?: 'Query', anon?: { __typename: 'User' } | null };
1470+
"
1471+
`);
1472+
14611473
const file2 = result.find(file => file.filename.endsWith('User2.generated.ts'));
14621474
expect(file2.content).toMatchInlineSnapshot(`
14631475
"export type User2QueryVariables = Exact<{ [key: string]: never; }>;

0 commit comments

Comments
 (0)