/
index.ts
173 lines (154 loc) · 5.84 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Types, CodegenPlugin } from '@graphql-codegen/plugin-helpers';
import { BaseVisitor, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
import * as addPlugin from '@graphql-codegen/add';
import { join, resolve } from 'path';
import { Kind, FragmentDefinitionNode } from 'graphql';
import { appendExtensionToFilePath, extractExternalFragmentsInUse, resolveRelativeImport } from './utils';
export type NearOperationFileConfig = {
/**
* @name baseTypesPath
* @type string
* @description Required, should point to the base schema types file.
* The key of the output is used a the base path for this file.
*
* @example
* ```yml
* generates:
* src/:
* preset: near-operation-file
* presetConfig:
* baseTypesPath: types.ts
* plugins:
* - typescript-operations
* ```
*/
baseTypesPath: string;
/**
* @name extension
* @type 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 .generates.ts
*
* @example
* ```yml
* generates:
* src/:
* preset: near-operation-file
* presetConfig:
* baseTypesPath: types.ts
* extension: .generated.tsx
* plugins:
* - typescript-operations
* - typescript-react-apollo
* ```
*/
extension?: string;
/**
* @name cwd
* @type string
* @description Optional, override the `cwd` of the execution. We are using `cwd` to figure out the imports between files. Use this if your execuion path is not your project root directory.
* @default process.cwd()
*
* @example
* ```yml
* generates:
* src/:
* preset: near-operation-file
* presetConfig:
* baseTypesPath: types.ts
* cwd: /some/path
* plugins:
* - typescript-operations
* ```
*/
cwd?: string;
/**
* @name importTypesNamespace
* @type string
* @description Optional, override the name of the import namespace used to import from the `baseTypesPath` file.
* @default Types
*
* @example
* ```yml
* generates:
* src/:
* preset: near-operation-file
* presetConfig:
* baseTypesPath: types.ts
* importTypesNamespace: SchemaTypes
* plugins:
* - typescript-operations
* ```
*/
importTypesNamespace?: string;
};
export type FragmentNameToFile = { [fragmentName: string]: { filePath: string; importName: string; onType: string; node: FragmentDefinitionNode } };
export const preset: Types.OutputPreset<NearOperationFileConfig> = {
buildGeneratesSection: options => {
const baseVisitor = new BaseVisitor(options.config, {});
if (!options.presetConfig.baseTypesPath) {
throw new Error(`Preset "near-operation-file" requires you to specify "baseTypesPath" configuration and point it to your base types file (generated by "typescript" plugin)!`);
}
const baseDir = options.presetConfig.cwd || process.cwd();
const extension = options.presetConfig.extension || '.generated.ts';
const importTypesNamespace = options.presetConfig.importTypesNamespace || 'Types';
const pluginMap: { [name: string]: CodegenPlugin } = {
...options.pluginMap,
add: addPlugin,
};
const fragmentNameToFile: FragmentNameToFile = options.documents.reduce((prev, documentRecord) => {
const fragments: FragmentDefinitionNode[] = documentRecord.content.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[];
if (fragments.length > 0) {
for (const fragment of fragments) {
const filePath = appendExtensionToFilePath(documentRecord.filePath, extension);
const importName = baseVisitor.convertName(fragment, { suffix: 'Fragment' });
prev[fragment.name.value] = { filePath, importName, onType: fragment.typeCondition.name.value, node: fragment };
}
}
return prev;
}, {});
const absTypesPath = resolve(baseDir, join(options.baseOutputDir, options.presetConfig.baseTypesPath));
return options.documents
.map<Types.GenerateOptions | null>(documentFile => {
const generatedFilePath = appendExtensionToFilePath(documentFile.filePath, extension);
const absGeneratedFilePath = resolve(baseDir, generatedFilePath);
const relativeImportPath = resolveRelativeImport(absGeneratedFilePath, absTypesPath);
const fragmentsInUse = extractExternalFragmentsInUse(documentFile.content, fragmentNameToFile);
const plugins = [...options.plugins];
const config = {
...options.config,
namespacedImportName: importTypesNamespace,
externalFragments: [] as LoadedFragment[],
};
for (const fragmentName of fragmentsInUse) {
const fragmentDetails = fragmentNameToFile[fragmentName];
if (fragmentDetails) {
const absFragmentFilePath = resolve(baseDir, fragmentDetails.filePath);
const fragmentImportPath = resolveRelativeImport(absGeneratedFilePath, absFragmentFilePath);
plugins.unshift({
add: `import { ${fragmentDetails.importName} } from '${fragmentImportPath}';`,
});
config.externalFragments.push({
isExternal: true,
importFrom: fragmentImportPath,
name: fragmentName,
onType: fragmentDetails.onType,
node: fragmentDetails.node,
});
}
}
plugins.unshift({ add: `import * as ${importTypesNamespace} from '${relativeImportPath}';\n` });
return {
filename: generatedFilePath,
plugins,
pluginMap,
config,
schema: options.schema,
schemaAst: options.schemaAst,
documents: [documentFile],
};
})
.filter(f => f);
},
};
export default preset;