1 | import { RenameTypes, RenameRootFields } from '@graphql-tools/wrap';
|
2 | import { applySchemaTransforms, applyRequestTransforms, applyResultTransforms } from '@graphql-tools/utils';
|
3 |
|
4 | class PrefixTransform {
|
5 | constructor(options) {
|
6 | this.transforms = [];
|
7 | const { apiName, config } = options;
|
8 | let prefix = null;
|
9 | if (config.value) {
|
10 | prefix = config.value;
|
11 | }
|
12 | else if (apiName) {
|
13 | prefix = `${apiName}_`;
|
14 | }
|
15 | if (!prefix) {
|
16 | throw new Error(`Transform 'prefix' has missing config: prefix`);
|
17 | }
|
18 | const ignoreList = config.ignore || [];
|
19 | this.transforms.push(new RenameTypes(typeName => (ignoreList.includes(typeName) ? typeName : `${prefix}${typeName}`)));
|
20 | if (config.includeRootOperations) {
|
21 | this.transforms.push(new RenameRootFields((typeName, fieldName) => ignoreList.includes(typeName) || ignoreList.includes(`${typeName}.${fieldName}`)
|
22 | ? fieldName
|
23 | : `${prefix}${fieldName}`));
|
24 | }
|
25 | }
|
26 | transformSchema(schema) {
|
27 | return applySchemaTransforms(schema, this.transforms);
|
28 | }
|
29 | transformRequest(request) {
|
30 | return applyRequestTransforms(request, this.transforms);
|
31 | }
|
32 | transformResult(result) {
|
33 | return applyResultTransforms(result, this.transforms);
|
34 | }
|
35 | }
|
36 |
|
37 | export default PrefixTransform;
|
38 |
|