UNPKG

@graphql-codegen/near-operation-file-preset

Version:

GraphQL Code Generator preset for generating operation code near the operation file

195 lines (194 loc) 6.6 kB
import { FragmentDefinitionNode } from 'graphql'; import { Types } from '@graphql-codegen/plugin-helpers'; import { FragmentImport, ImportSource } from '@graphql-codegen/visitor-plugin-common'; import { DocumentImportResolverOptions, resolveDocumentImports } from './resolve-document-imports.js'; export { resolveDocumentImports, DocumentImportResolverOptions }; export type FragmentImportFromFn = (source: ImportSource<FragmentImport>, sourceFilePath: string) => ImportSource<FragmentImport>; export type NearOperationFileConfig = { /** * @description Required, should point to the base schema types file. * The key of the output is used a the base path for this file. * * If you wish to use an NPM package or a local workspace package, make sure to prefix the package name with `~`. * * @exampleMarkdown * ```ts filename="codegen.ts" {10} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations'], * presetConfig: { * baseTypesPath: 'types.ts' * }, * }, * }, * }; * export default config; * ``` */ baseTypesPath: string; /** * @description Overrides all external fragments import types by using a specific file path or a package name. * * If you wish to use an NPM package or a local workspace package, make sure to prefix the package name with `~`. * * @exampleMarkdown * ```ts filename="codegen.ts" {11} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations'], * presetConfig: { * baseTypesPath: 'types.ts', * importAllFragmentsFrom: '~types' * }, * }, * }, * }; * export default config; * ``` */ importAllFragmentsFrom?: string | FragmentImportFromFn; /** * @description Optional, sets a specific file name for the generated files. Use this to override the generated file name when generating files for example based on multiple .graphql files in separate directories. * * @exampleMarkdown * ```ts filename="codegen.ts" {11} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations', 'typescript-react-apollo'], * presetConfig: { * baseTypesPath: 'types.ts', * fileName: 'index', * }, * }, * }, * }; * export default config; * ``` */ fileName?: string; /** * @description Optional, sets the extension for the generated files. Use this to override the extension if you are using plugins that requires a different type of extensions (such as `typescript-react-apollo`) * @default .generated.ts * * @exampleMarkdown * ```ts filename="codegen.ts" {11} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations', 'typescript-react-apollo'], * presetConfig: { * baseTypesPath: 'types.ts', * extension: '.generated.tsx', * }, * }, * }, * }; * export default config; * ``` */ extension?: string; /** * @description Optional, override the `cwd` of the execution. We are using `cwd` to figure out the imports between files. Use this if your execution path is not your project root directory. * @default process.cwd() * * @exampleMarkdown * ```ts filename="codegen.ts" {11} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations'], * presetConfig: { * baseTypesPath: 'types.ts', * cwd: '/some/path' * }, * }, * }, * }; * export default config; * ``` */ cwd?: string; /** * @description Optional, defines a folder, (Relative to the source files) where the generated files will be created. * @default '' * * @exampleMarkdown * ```ts filename="codegen.ts" {11} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations'], * presetConfig: { * baseTypesPath: 'types.ts', * folder: '__generated__' * }, * }, * }, * }; * export default config; * ``` */ folder?: string; /** * @description Optional, override the name of the import namespace used to import from the `baseTypesPath` file. * @default Types * * @exampleMarkdown * ```ts filename="codegen.ts" {11} * import type { CodegenConfig } from '@graphql-codegen/cli'; * * const config: CodegenConfig = { * // ... * generates: { * 'path/to/file.ts': { * preset: 'near-operation-file', * plugins: ['typescript-operations'], * presetConfig: { * baseTypesPath: 'types.ts', * importTypesNamespace: 'SchemaTypes' * }, * }, * }, * }; * export default config; * ``` */ importTypesNamespace?: string; }; export type FragmentNameToFile = { [fragmentName: string]: { location: string; importsNames: string[]; onType: string; node: FragmentDefinitionNode; }; }; export declare const preset: Types.OutputPreset<NearOperationFileConfig>; export default preset;