UNPKG

1.45 kBJavaScriptView Raw
1import { asArray, extractExtensionsFromSchema, getDocumentNodeFromSchema, getResolversFromSchema, } from '@graphql-tools/utils';
2import { makeExecutableSchema } from './makeExecutableSchema.js';
3/**
4 * Synchronously merges multiple schemas, typeDefinitions and/or resolvers into a single schema.
5 * @param config Configuration object
6 */
7export function mergeSchemas(config) {
8 const extractedTypeDefs = [];
9 const extractedResolvers = [];
10 const extractedSchemaExtensions = [];
11 if (config.schemas != null) {
12 for (const schema of config.schemas) {
13 extractedTypeDefs.push(getDocumentNodeFromSchema(schema));
14 extractedResolvers.push(getResolversFromSchema(schema));
15 extractedSchemaExtensions.push(extractExtensionsFromSchema(schema));
16 }
17 }
18 if (config.typeDefs != null) {
19 extractedTypeDefs.push(config.typeDefs);
20 }
21 if (config.resolvers != null) {
22 const additionalResolvers = asArray(config.resolvers);
23 extractedResolvers.push(...additionalResolvers);
24 }
25 if (config.schemaExtensions != null) {
26 const additionalSchemaExtensions = asArray(config.schemaExtensions);
27 extractedSchemaExtensions.push(...additionalSchemaExtensions);
28 }
29 return makeExecutableSchema({
30 ...config,
31 typeDefs: extractedTypeDefs,
32 resolvers: extractedResolvers,
33 schemaExtensions: extractedSchemaExtensions,
34 });
35}