@nmarks/graphql-codegen-per-operation-file-preset 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +209 -0
- package/cjs/fragment-resolver.js +149 -0
- package/cjs/index.js +111 -0
- package/cjs/package.json +1 -0
- package/cjs/resolve-document-imports.js +58 -0
- package/cjs/utils.js +120 -0
- package/esm/fragment-resolver.js +146 -0
- package/esm/index.js +107 -0
- package/esm/resolve-document-imports.js +54 -0
- package/esm/utils.js +115 -0
- package/package.json +48 -0
- package/typings/fragment-resolver.d.cts +29 -0
- package/typings/fragment-resolver.d.ts +29 -0
- package/typings/index.d.cts +131 -0
- package/typings/index.d.ts +131 -0
- package/typings/resolve-document-imports.d.cts +48 -0
- package/typings/resolve-document-imports.d.ts +48 -0
- package/typings/utils.d.cts +23 -0
- package/typings/utils.d.ts +23 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
export type PerOperationFileConfig = {
|
|
3
|
+
/**
|
|
4
|
+
* @description Required, should point to the base schema types file.
|
|
5
|
+
* The key of the output is used a the base path for this file.
|
|
6
|
+
*
|
|
7
|
+
* If you wish to use an NPM package or a local workspace package, make sure to prefix the package name with `~`.
|
|
8
|
+
*
|
|
9
|
+
* @exampleMarkdown
|
|
10
|
+
* ```ts filename="codegen.ts"
|
|
11
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
12
|
+
*
|
|
13
|
+
* const config: CodegenConfig = {
|
|
14
|
+
* // ...
|
|
15
|
+
* generates: {
|
|
16
|
+
* 'path/to/file.ts': {
|
|
17
|
+
* preset: 'per-operation-file',
|
|
18
|
+
* plugins: ['typescript-operations'],
|
|
19
|
+
* presetConfig: {
|
|
20
|
+
* baseTypesPath: 'types.ts'
|
|
21
|
+
* },
|
|
22
|
+
* },
|
|
23
|
+
* },
|
|
24
|
+
* };
|
|
25
|
+
* export default config;
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
baseTypesPath: string;
|
|
29
|
+
/**
|
|
30
|
+
* @description Optional, sets the extension for the generated files.
|
|
31
|
+
* @default .ts
|
|
32
|
+
*
|
|
33
|
+
* @exampleMarkdown
|
|
34
|
+
* ```ts filename="codegen.ts"
|
|
35
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
36
|
+
*
|
|
37
|
+
* const config: CodegenConfig = {
|
|
38
|
+
* // ...
|
|
39
|
+
* generates: {
|
|
40
|
+
* 'path/to/file.ts': {
|
|
41
|
+
* preset: 'per-operation-file',
|
|
42
|
+
* plugins: ['typescript-operations'],
|
|
43
|
+
* presetConfig: {
|
|
44
|
+
* baseTypesPath: 'types.ts',
|
|
45
|
+
* extension: '.generated.ts',
|
|
46
|
+
* },
|
|
47
|
+
* },
|
|
48
|
+
* },
|
|
49
|
+
* };
|
|
50
|
+
* export default config;
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
extension?: string;
|
|
54
|
+
/**
|
|
55
|
+
* @description Optional, override the `cwd` of the execution.
|
|
56
|
+
* @default process.cwd()
|
|
57
|
+
*
|
|
58
|
+
* @exampleMarkdown
|
|
59
|
+
* ```ts filename="codegen.ts"
|
|
60
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
61
|
+
*
|
|
62
|
+
* const config: CodegenConfig = {
|
|
63
|
+
* // ...
|
|
64
|
+
* generates: {
|
|
65
|
+
* 'path/to/file.ts': {
|
|
66
|
+
* preset: 'per-operation-file',
|
|
67
|
+
* plugins: ['typescript-operations'],
|
|
68
|
+
* presetConfig: {
|
|
69
|
+
* baseTypesPath: 'types.ts',
|
|
70
|
+
* cwd: '/some/path'
|
|
71
|
+
* },
|
|
72
|
+
* },
|
|
73
|
+
* },
|
|
74
|
+
* };
|
|
75
|
+
* export default config;
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
cwd?: string;
|
|
79
|
+
/**
|
|
80
|
+
* @description Optional, defines a folder where the generated files will be created.
|
|
81
|
+
* @default '__generated__'
|
|
82
|
+
*
|
|
83
|
+
* @exampleMarkdown
|
|
84
|
+
* ```ts filename="codegen.ts"
|
|
85
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
86
|
+
*
|
|
87
|
+
* const config: CodegenConfig = {
|
|
88
|
+
* // ...
|
|
89
|
+
* generates: {
|
|
90
|
+
* 'path/to/file.ts': {
|
|
91
|
+
* preset: 'per-operation-file',
|
|
92
|
+
* plugins: ['typescript-operations'],
|
|
93
|
+
* presetConfig: {
|
|
94
|
+
* baseTypesPath: 'types.ts',
|
|
95
|
+
* folder: '__generated__'
|
|
96
|
+
* },
|
|
97
|
+
* },
|
|
98
|
+
* },
|
|
99
|
+
* };
|
|
100
|
+
* export default config;
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
folder?: string;
|
|
104
|
+
/**
|
|
105
|
+
* @description Optional, override the name of the import namespace used to import from the `baseTypesPath` file.
|
|
106
|
+
* @default Types
|
|
107
|
+
*
|
|
108
|
+
* @exampleMarkdown
|
|
109
|
+
* ```ts filename="codegen.ts"
|
|
110
|
+
* import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
111
|
+
*
|
|
112
|
+
* const config: CodegenConfig = {
|
|
113
|
+
* // ...
|
|
114
|
+
* generates: {
|
|
115
|
+
* 'path/to/file.ts': {
|
|
116
|
+
* preset: 'per-operation-file',
|
|
117
|
+
* plugins: ['typescript-operations'],
|
|
118
|
+
* presetConfig: {
|
|
119
|
+
* baseTypesPath: 'types.ts',
|
|
120
|
+
* importTypesNamespace: 'SchemaTypes'
|
|
121
|
+
* },
|
|
122
|
+
* },
|
|
123
|
+
* },
|
|
124
|
+
* };
|
|
125
|
+
* export default config;
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
importTypesNamespace?: string;
|
|
129
|
+
};
|
|
130
|
+
export declare const preset: Types.OutputPreset<PerOperationFileConfig>;
|
|
131
|
+
export default preset;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { FragmentDefinitionNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
3
|
+
import { FragmentImport, ImportDeclaration, ImportSource, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
|
|
4
|
+
import { Source } from '@graphql-tools/utils';
|
|
5
|
+
export type FragmentRegistry = {
|
|
6
|
+
[fragmentName: string]: {
|
|
7
|
+
location: string;
|
|
8
|
+
importNames: string[];
|
|
9
|
+
onType: string;
|
|
10
|
+
node: FragmentDefinitionNode;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export type DocumentImportResolverOptions = {
|
|
14
|
+
baseDir: string;
|
|
15
|
+
/**
|
|
16
|
+
* Schema base types source
|
|
17
|
+
*/
|
|
18
|
+
schemaTypesSource: string | ImportSource;
|
|
19
|
+
/**
|
|
20
|
+
* Should `import type` be used
|
|
21
|
+
*/
|
|
22
|
+
typesImport: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Folder for generated files
|
|
25
|
+
*/
|
|
26
|
+
folder: string;
|
|
27
|
+
/**
|
|
28
|
+
* Extension for generated files
|
|
29
|
+
*/
|
|
30
|
+
extension: string;
|
|
31
|
+
};
|
|
32
|
+
interface ResolveDocumentImportResult {
|
|
33
|
+
filename: string;
|
|
34
|
+
documents: [Source];
|
|
35
|
+
importStatements: string[];
|
|
36
|
+
fragmentImports: ImportDeclaration<FragmentImport>[];
|
|
37
|
+
externalFragments: LoadedFragment<{
|
|
38
|
+
level: number;
|
|
39
|
+
}>[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Transform the preset's provided documents into single-file generator sources, while resolving fragment and user-defined imports
|
|
43
|
+
*
|
|
44
|
+
* Resolves user provided imports and fragment imports using the `DocumentImportResolverOptions`.
|
|
45
|
+
* Does not define specific plugins, but rather returns a string[] of `importStatements` for the calling plugin to make use of
|
|
46
|
+
*/
|
|
47
|
+
export declare function resolveDocumentImports<T>(presetOptions: Types.PresetFnArgs<T>, schemaObject: GraphQLSchema, importResolverOptions: DocumentImportResolverOptions, dedupeFragments?: boolean): Array<ResolveDocumentImportResult>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { FragmentDefinitionNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
3
|
+
import { FragmentImport, ImportDeclaration, ImportSource, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
|
|
4
|
+
import { Source } from '@graphql-tools/utils';
|
|
5
|
+
export type FragmentRegistry = {
|
|
6
|
+
[fragmentName: string]: {
|
|
7
|
+
location: string;
|
|
8
|
+
importNames: string[];
|
|
9
|
+
onType: string;
|
|
10
|
+
node: FragmentDefinitionNode;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export type DocumentImportResolverOptions = {
|
|
14
|
+
baseDir: string;
|
|
15
|
+
/**
|
|
16
|
+
* Schema base types source
|
|
17
|
+
*/
|
|
18
|
+
schemaTypesSource: string | ImportSource;
|
|
19
|
+
/**
|
|
20
|
+
* Should `import type` be used
|
|
21
|
+
*/
|
|
22
|
+
typesImport: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Folder for generated files
|
|
25
|
+
*/
|
|
26
|
+
folder: string;
|
|
27
|
+
/**
|
|
28
|
+
* Extension for generated files
|
|
29
|
+
*/
|
|
30
|
+
extension: string;
|
|
31
|
+
};
|
|
32
|
+
interface ResolveDocumentImportResult {
|
|
33
|
+
filename: string;
|
|
34
|
+
documents: [Source];
|
|
35
|
+
importStatements: string[];
|
|
36
|
+
fragmentImports: ImportDeclaration<FragmentImport>[];
|
|
37
|
+
externalFragments: LoadedFragment<{
|
|
38
|
+
level: number;
|
|
39
|
+
}>[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Transform the preset's provided documents into single-file generator sources, while resolving fragment and user-defined imports
|
|
43
|
+
*
|
|
44
|
+
* Resolves user provided imports and fragment imports using the `DocumentImportResolverOptions`.
|
|
45
|
+
* Does not define specific plugins, but rather returns a string[] of `importStatements` for the calling plugin to make use of
|
|
46
|
+
*/
|
|
47
|
+
export declare function resolveDocumentImports<T>(presetOptions: Types.PresetFnArgs<T>, schemaObject: GraphQLSchema, importResolverOptions: DocumentImportResolverOptions, dedupeFragments?: boolean): Array<ResolveDocumentImportResult>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DocumentNode, FragmentDefinitionNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { FragmentRegistry } from './fragment-resolver.cjs';
|
|
3
|
+
/**
|
|
4
|
+
* Generate output file path for a specific operation/fragment
|
|
5
|
+
*/
|
|
6
|
+
export declare function generateOperationFilePath(sourceLocation: string, operationName: string, folder: string, extension: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Analyzes fragment usage in a GraphQL document.
|
|
9
|
+
* Returns information about which fragments are used and which specific types they're used with.
|
|
10
|
+
*/
|
|
11
|
+
export declare function analyzeFragmentUsage(documentNode: DocumentNode, fragmentRegistry: FragmentRegistry, schema: GraphQLSchema): {
|
|
12
|
+
fragmentsInUse: {
|
|
13
|
+
[fragmentName: string]: number;
|
|
14
|
+
};
|
|
15
|
+
usedFragmentTypes: {
|
|
16
|
+
[fragmentName: string]: string[];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare function extractExternalFragmentsInUse(documentNode: DocumentNode | FragmentDefinitionNode, fragmentNameToFile: FragmentRegistry, localFragment: Set<string>, result?: {
|
|
20
|
+
[fragmentName: string]: number;
|
|
21
|
+
}, level?: number): {
|
|
22
|
+
[fragmentName: string]: number;
|
|
23
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DocumentNode, FragmentDefinitionNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { FragmentRegistry } from './fragment-resolver.js';
|
|
3
|
+
/**
|
|
4
|
+
* Generate output file path for a specific operation/fragment
|
|
5
|
+
*/
|
|
6
|
+
export declare function generateOperationFilePath(sourceLocation: string, operationName: string, folder: string, extension: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Analyzes fragment usage in a GraphQL document.
|
|
9
|
+
* Returns information about which fragments are used and which specific types they're used with.
|
|
10
|
+
*/
|
|
11
|
+
export declare function analyzeFragmentUsage(documentNode: DocumentNode, fragmentRegistry: FragmentRegistry, schema: GraphQLSchema): {
|
|
12
|
+
fragmentsInUse: {
|
|
13
|
+
[fragmentName: string]: number;
|
|
14
|
+
};
|
|
15
|
+
usedFragmentTypes: {
|
|
16
|
+
[fragmentName: string]: string[];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare function extractExternalFragmentsInUse(documentNode: DocumentNode | FragmentDefinitionNode, fragmentNameToFile: FragmentRegistry, localFragment: Set<string>, result?: {
|
|
20
|
+
[fragmentName: string]: number;
|
|
21
|
+
}, level?: number): {
|
|
22
|
+
[fragmentName: string]: number;
|
|
23
|
+
};
|