1 | import { GraphQLSchema } from 'graphql';
|
2 | import { TransformFn, YamlConfig } from '@graphql-mesh/types';
|
3 | import {
|
4 | Transform,
|
5 | RenameTypes,
|
6 | RenameRootFields,
|
7 | transformSchema
|
8 | } from 'graphql-tools-fork';
|
9 | export const prefixTransform: TransformFn<YamlConfig.Prefix> = async ({
|
10 | apiName,
|
11 | schema,
|
12 | config
|
13 | }): Promise<GraphQLSchema> => {
|
14 | let prefix: string | null = null;
|
15 |
|
16 | if (config.prefix) {
|
17 | prefix = config.prefix;
|
18 | } else if (apiName) {
|
19 | prefix = `${apiName}_`;
|
20 | }
|
21 |
|
22 | if (!prefix) {
|
23 | throw new Error(`Transform 'prefix' has missing config: prefix`);
|
24 | }
|
25 |
|
26 | const ignoreList = config.ignore || [];
|
27 | const transforms: Transform[] = [];
|
28 |
|
29 | transforms.push(
|
30 | new RenameTypes(typeName =>
|
31 | ignoreList.includes(typeName) ? typeName : `${prefix}${typeName}`
|
32 | )
|
33 | );
|
34 |
|
35 | if (config.includeRootOperations) {
|
36 | transforms.push(
|
37 | new RenameRootFields((typeName, fieldName) =>
|
38 | ignoreList.includes(typeName) ||
|
39 | ignoreList.includes(`${typeName}.${fieldName}`)
|
40 | ? fieldName
|
41 | : `${prefix}${fieldName}`
|
42 | )
|
43 | );
|
44 | }
|
45 |
|
46 | return transformSchema(schema, transforms);
|
47 | };
|
48 |
|
49 | export default prefixTransform;
|