Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!schema.getQueryType()) {
schema = new GraphQLSchema({
...schema.toConfig(),
query: new GraphQLObjectType({
name: 'Query',
fields: {},
}),
});
}
const entityTypes = Object.values(schema.getTypeMap()).filter(
type => isObjectType(type) && typeIncludesDirective(type, 'key'),
);
const hasEntities = entityTypes.length > 0;
schema = transformSchema(schema, type => {
// Add `_entities` and `_service` fields to query root type
if (isObjectType(type) && type === schema.getQueryType()) {
const config = type.toConfig();
return new GraphQLObjectType({
...config,
fields: {
...(hasEntities && { _entities: entitiesField }),
_service: {
...serviceField,
resolve: () => ({ sdl }),
},
...config.fields,
},
});
}
Object.entries(federationConfig)
.filter(([, { keyFields }]) => keyFields && keyFields.length)
.map(([objectName]) => {
const type = schemaWithQueryType.getType(objectName);
if (!isObjectType(type)) {
throw new Error(
`Type "${objectName}" is not an object type and can't have a key directive`,
);
}
return [objectName, type];
}),
);
const hasEntities = !!Object.keys(entityTypes).length;
const schemaWithFederationQueryType = transformSchema(
schemaWithQueryType,
type => {
// Add `_entities` and `_service` fields to query root type
if (isObjectType(type) && type === schemaWithQueryType.getQueryType()) {
const config = type.toConfig();
return new GraphQLObjectType({
...config,
fields: {
...config.fields,
...(hasEntities && { _entities: entitiesField }),
_service: {
...serviceField,
resolve: () => ({ sdl: schemaWithFederationDirectives }),
},
},
});
};
schema = new GraphQLSchema({
...schema.toConfig(),
...mapValues(operationTypeMap, typeName =>
typeName
? (schema.getType(typeName) as GraphQLObjectType)
: undefined,
),
});
// If multiple type definitions and extensions for the same type implement the
// same interface, it will get added to the constructed object multiple times,
// resulting in a schema validation error. We therefore need to remove
// duplicate interfaces from object types manually.
schema = transformSchema(schema, type => {
if (isObjectType(type)) {
const config = type.toConfig();
return new GraphQLObjectType({
...config,
interfaces: Array.from(new Set(config.interfaces)),
});
}
return undefined;
});
addFederationMetadataToSchemaNodes({
schema,
typeToServiceMap,
externalFields,
keyDirectivesMap,
valueTypes,
...config,
fields: {
...config.fields,
...(hasEntities && { _entities: entitiesField }),
_service: {
...serviceField,
resolve: () => ({ sdl: schemaWithFederationDirectives }),
},
},
});
}
return undefined;
},
);
const schemaWithUnionType = transformSchema(
schemaWithFederationQueryType,
type => {
if (isUnionType(type) && type.name === EntityType.name) {
return new GraphQLUnionType({
...EntityType.toConfig(),
types: Object.values(entityTypes),
});
}
return undefined;
},
);
// Not using transformSchema since it will remove resolveReference
Object.entries(federationConfig).forEach(
([objectName, currentFederationConfig]) => {
if (currentFederationConfig.resolveReference) {
...config,
fields: {
...(hasEntities && { _entities: entitiesField }),
_service: {
...serviceField,
resolve: () => ({ sdl }),
},
...config.fields,
},
});
}
return undefined;
});
schema = transformSchema(schema, type => {
if (hasEntities && isUnionType(type) && type.name === EntityType.name) {
return new GraphQLUnionType({
...EntityType.toConfig(),
types: entityTypes.filter(isObjectType),
});
}
return undefined;
});
for (const module of modules) {
if (!module.resolvers) continue;
addResolversToSchema(schema, module.resolvers);
}
return schema;
}