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