-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
concatenateTypeDefs.ts
41 lines (37 loc) · 1.33 KB
/
concatenateTypeDefs.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
import { print, DocumentNode } from 'graphql';
import { ITypedef } from '../Interfaces';
import SchemaError from './SchemaError';
function concatenateTypeDefs(
typeDefinitionsAry: ITypedef[],
calledFunctionRefs = [] as any,
): string {
let resolvedTypeDefinitions: string[] = [];
typeDefinitionsAry.forEach((typeDef: ITypedef) => {
if (typeof typeDef === 'function') {
if (calledFunctionRefs.indexOf(typeDef) === -1) {
calledFunctionRefs.push(typeDef);
resolvedTypeDefinitions = resolvedTypeDefinitions.concat(
concatenateTypeDefs(typeDef(), calledFunctionRefs),
);
}
} else if (typeof typeDef === 'string') {
resolvedTypeDefinitions.push(typeDef.trim());
} else if ((<DocumentNode>typeDef).kind !== undefined) {
resolvedTypeDefinitions.push(print(typeDef).trim());
} else {
const type = typeof typeDef;
throw new SchemaError(
`typeDef array must contain only strings and functions, got ${type}`,
);
}
});
return uniq(resolvedTypeDefinitions.map(x => x.trim())).join('\n');
}
function uniq(array: Array<any>): Array<any> {
return array.reduce((accumulator, currentValue) => {
return accumulator.indexOf(currentValue) === -1
? [...accumulator, currentValue]
: accumulator;
}, []);
}
export default concatenateTypeDefs;