-
-
Notifications
You must be signed in to change notification settings - Fork 801
/
merge-schemas.ts
60 lines (53 loc) · 1.71 KB
/
merge-schemas.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
import { GraphQLSchema } from 'graphql';
import {
asArray,
extractExtensionsFromSchema,
getResolversFromSchema,
IResolvers,
SchemaExtensions,
TypeSource,
} from '@graphql-tools/utils';
import { makeExecutableSchema } from './makeExecutableSchema.js';
import { IExecutableSchemaDefinition } from './types.js';
/**
* Configuration object for schema merging
*/
export type MergeSchemasConfig<T = any> = Partial<IExecutableSchemaDefinition<T>> & {
/**
* The schemas to be merged
*/
schemas?: GraphQLSchema[];
};
/**
* Synchronously merges multiple schemas, typeDefinitions and/or resolvers into a single schema.
* @param config Configuration object
*/
export function mergeSchemas(config: MergeSchemasConfig) {
const extractedTypeDefs: TypeSource[] = [];
const extractedResolvers: IResolvers<any, any>[] = [];
const extractedSchemaExtensions: SchemaExtensions[] = [];
if (config.schemas != null) {
for (const schema of config.schemas) {
extractedTypeDefs.push(schema);
extractedResolvers.push(getResolversFromSchema(schema));
extractedSchemaExtensions.push(extractExtensionsFromSchema(schema));
}
}
if (config.typeDefs != null) {
extractedTypeDefs.push(config.typeDefs);
}
if (config.resolvers != null) {
const additionalResolvers = asArray(config.resolvers);
extractedResolvers.push(...additionalResolvers);
}
if (config.schemaExtensions != null) {
const additionalSchemaExtensions = asArray(config.schemaExtensions);
extractedSchemaExtensions.push(...additionalSchemaExtensions);
}
return makeExecutableSchema({
...config,
typeDefs: extractedTypeDefs,
resolvers: extractedResolvers,
schemaExtensions: extractedSchemaExtensions,
});
}