Skip to content

Commit 76d5e05

Browse files
authored
[near-operation-file] Make near operation file preset support typescript-operations v6 (#1385)
* Remove baseTypesPath required check * Implement tests for no baseTypesPath use case * Add changeset * Add breaking change marker
1 parent 33c064a commit 76d5e05

5 files changed

Lines changed: 57 additions & 14 deletions

File tree

.changeset/shaggy-pugs-show.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/near-operation-file-preset': major
3+
---
4+
5+
BREAKING CHANGE: Remove the required baseTypesPath condition to support typescript-operations v6

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type FragmentImportFromFn = (
2525

2626
export type NearOperationFileConfig = {
2727
/**
28-
* @description Required, should point to the base schema types file.
28+
* @description points to the base schema types file.
2929
* The key of the output is used a the base path for this file.
3030
*
3131
* If you wish to use an NPM package or a local workspace package, make sure to prefix the package name with `~`.
@@ -39,7 +39,7 @@ export type NearOperationFileConfig = {
3939
* generates: {
4040
* 'path/to/file.ts': {
4141
* preset: 'near-operation-file',
42-
* plugins: ['typescript-operations'],
42+
* plugins: ['...'],
4343
* presetConfig: {
4444
* baseTypesPath: 'types.ts'
4545
* },
@@ -49,7 +49,7 @@ export type NearOperationFileConfig = {
4949
* export default config;
5050
* ```
5151
*/
52-
baseTypesPath: string;
52+
baseTypesPath?: string;
5353
/**
5454
* @description Overrides all external fragments import types by using a specific file path or a package name.
5555
*
@@ -220,18 +220,13 @@ export const preset: Types.OutputPreset<NearOperationFileConfig> = {
220220
const fileName = options.presetConfig.fileName || '';
221221
const extension = options.presetConfig.extension || '.generated.ts';
222222
const folder = options.presetConfig.folder || '';
223-
const importTypesNamespace = options.presetConfig.importTypesNamespace || 'Types';
223+
const baseTypesPath = options.presetConfig.baseTypesPath || '';
224+
const importTypesNamespace =
225+
options.presetConfig.importTypesNamespace ||
226+
(options.presetConfig.baseTypesPath ? 'Types' : undefined); // When there is `baseTypesPath`, we assume there'd be a type import, so we default `importTypesNamespace` value to `Types` for convenience.
224227
const importAllFragmentsFrom: FragmentImportFromFn | string | null =
225228
options.presetConfig.importAllFragmentsFrom || null;
226229

227-
const { baseTypesPath } = options.presetConfig;
228-
229-
if (!baseTypesPath) {
230-
throw new Error(
231-
`Preset "near-operation-file" requires you to specify "baseTypesPath" configuration and point it to your base types file (generated by "typescript" plugin)!`,
232-
);
233-
}
234-
235230
const shouldAbsolute = !baseTypesPath.startsWith('~');
236231

237232
const pluginMap: { [name: string]: CodegenPlugin } = {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface ResolveDocumentImportResult {
5555
export function resolveDocumentImports<T>(
5656
presetOptions: Types.PresetFnArgs<T>,
5757
schemaObject: GraphQLSchema,
58-
importResolverOptions: DocumentImportResolverOptions,
58+
importResolverOptions: DocumentImportResolverOptions & { schemaTypesSource: ImportSource }, // Original type is `schemaTypesSource: string | ImportSource`. We manually override `schemaTypesSource` type because we only use the `ImportSource` use case
5959
dedupeFragments = false,
6060
): Array<ResolveDocumentImportResult> {
6161
const resolveFragments = buildFragmentResolver(
@@ -84,7 +84,10 @@ export function resolveDocumentImports<T>(
8484
],
8585
};
8686

87-
if (isUsingTypes(externalFragmentsInjectedDocument, [], schemaObject)) {
87+
if (
88+
isUsingTypes(externalFragmentsInjectedDocument, [], schemaObject) &&
89+
schemaTypesSource.namespace
90+
) {
8891
const schemaTypesImportStatement = generateImportStatement({
8992
baseDir,
9093
emitLegacyCommonJSImports: presetOptions.config.emitLegacyCommonJSImports,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query User {
2+
user {
3+
id
4+
}
5+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,41 @@ describe('near-operation-file preset', () => {
15401540
"import { AnimalFragment_Cat } from './issue-1112-interface.generated'",
15411541
);
15421542
});
1543+
1544+
it('generates correctly without baseTypesPath for standalone typescript-operations', async () => {
1545+
const result = await executeCodegen({
1546+
schema: /* GraphQL */ `
1547+
type Query {
1548+
user: User
1549+
}
1550+
1551+
type User {
1552+
id: ID!
1553+
name: String!
1554+
}
1555+
`,
1556+
documents: path.join(__dirname, 'fixtures/no-base-types-path.graphql'),
1557+
generates: {
1558+
[__dirname]: {
1559+
preset,
1560+
plugins: ['typescript-operations'],
1561+
},
1562+
},
1563+
});
1564+
1565+
const generatedFile = result.find(({ filename }) =>
1566+
filename.endsWith('fixtures/no-base-types-path.generated.ts'),
1567+
);
1568+
1569+
// FIXME: when typescript-operations v6 releases, this test will be updated
1570+
expect(generatedFile.content).toMatchInlineSnapshot(`
1571+
"export type UserQueryVariables = Exact<{ [key: string]: never; }>;
1572+
1573+
1574+
export type UserQuery = { __typename?: 'Query', user?: { __typename?: 'User', id: string } | null };
1575+
"
1576+
`);
1577+
});
15431578
});
15441579

15451580
const getFragmentImportsFromResult = (result: Types.GenerateOptions[], index = 0) =>

0 commit comments

Comments
 (0)