Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
: null,
)
.filter(isNotNullOrUndefined);
keyDirectiveInfoOnTypeExtensions.push(...keyDirectivesInfo);
},
});
// this allows us to build a partial schema
let schema = new GraphQLSchema({
query: undefined,
directives: [...specifiedDirectives, ...federationDirectives],
});
try {
schema = buildSchemaFromSDL(typeDefs, schema);
} catch (e) {
errors.push(e);
return errors;
}
const typeInfo = new TypeInfo(schema);
for (const { typeName, keyArgument } of keyDirectiveInfoOnTypeExtensions) {
const keyDirectiveSelectionSet = parse(
`fragment __generated on ${typeName} { ${keyArgument} }`,
);
visit(
keyDirectiveSelectionSet,
visitWithTypeInfo(typeInfo, {
Field() {
const fieldDef = typeInfo.getFieldDef();
if ('typeDefs' in modulesOrSDL) {
const { typeDefs, resolvers } = modulesOrSDL;
const augmentedTypeDefs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
shapedModulesOrSDL = augmentedTypeDefs.map((typeDefs, i) => {
const module: GraphQLSchemaModule = { typeDefs };
// add the resolvers to the first "module" in the array
if (i === 0 && resolvers) module.resolvers = resolvers;
return module;
});
} else {
shapedModulesOrSDL = modulesOrSDL;
}
const modules = modulesFromSDL(shapedModulesOrSDL);
let schema = buildSchemaFromSDL(
modules,
new GraphQLSchema({
query: undefined,
directives: [...specifiedDirectives, ...federationDirectives],
}),
);
// At this point in time, we have a schema to be printed into SDL which is
// representative of what the user defined for their schema. This is before
// we process any of the federation directives and add custom federation types
// so its the right place to create our service definition sdl.
//
// We have to use a modified printSchema from graphql-js which includes
// support for preserving the *uses* of federation directives while removing
// their *definitions* from the sdl.
const sdl = printSchema(schema);
// load each path and get sdl string from each, if a list, concatenate them all
const documents = path
? [this.loadFileAndGetDocument(path)]
: paths
? paths.map(this.loadFileAndGetDocument, this)
: undefined;
if (!documents)
throw new Error(
`Schema could not be loaded for [${
path ? path : paths ? paths.join(", ") : "undefined"
}]`
);
this.schema = buildSchemaFromSDL(documents);
if (!this.schema) throw new Error(`Schema could not be loaded for ${path}`);
return this.schema;
}