Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main(): Promise {
try {
const cwd = process.cwd();
const lib = await libDir(cwd);
// check if the project has a custom schema
const customSchemaLocation = path.join(lib, "graphql", "schema.json");
const defaultSchemaLocation = path.resolve(__dirname, "..", "lib", "graph", "schema.json");
const schema = (await fs.pathExists(customSchemaLocation)) ? customSchemaLocation : defaultSchemaLocation;
const transform = new RenameTypes(name => {
switch (name) {
case "Fingerprint":
case "PushImpact":
return `Deprecated${name}`;
default:
return undefined;
}
});
const gqlGenOutput = path.join(lib, "typings", "types.ts");
const graphQlGlob = `${lib}/graphql/!(ingester)/*.graphql`;
const config: Types.GenerateOptions = {
schema: parse(printSchema(transform.transformSchema(await loadSchema(schema)))),
filename: gqlGenOutput,
plugins: [{
function transformRemoteSchema(schema: string, namespace: string | null): string {
let transformedSchema = buildSchema(schema);
transformedSchema = transformSchema(transformedSchema, [
new RenameTypes(
(name) => namespace ? `${namespace}_${name}` : name,
),
new RenameRootFields(
((operation, name) => namespace ? `${namespace}_${name}` : name),
),
]);
const rootTypeNames: string[] = [];
if (transformedSchema.getMutationType()) {
rootTypeNames.push(transformedSchema.getMutationType()!.name);
}
if (transformedSchema.getQueryType()) {
rootTypeNames.push(transformedSchema.getQueryType()!.name);
}
// Once subscriptions are supported, do the same here...
let transformedSchemaDoc = parse(printSchema(transformedSchema));
namespaceSchema (schema, fieldName, typeName) {
return transformSchema(schema, [
new StripNonQueryTransform(),
new RenameTypes(name => `${typeName}_${name}`),
new NamespaceUnderFieldTransform(typeName, fieldName)
])
}
}
function renameSystemInfo(schema: GraphQLSchema, systemName: String) {
return transformSchema(schema, [
new RenameTypes((name: string) => (name === "ProcessInfo" ? `${systemName}Status` : undefined)),
new RenameRootFields(
(_operation: string, name: string) =>
name === "ping" ? `${systemName.substring(0, 1).toLowerCase()}${systemName.substring(1)}Status` : name
)
]);
}
private patchSchema(schema: GraphQLSchema, systemName: string) {
return transformSchema(schema, [
new RenameTypes((name: string) => (name === 'StatusInfo' ? `${systemName}StatusInfo` : undefined)),
new RenameRootFields(
(_operation: string, name: string) =>
name === 'status' ? `${systemName.substring(0, 1).toLowerCase()}${systemName.substring(1)}Status` : name,
),
]);
}
namespaceSchema(schema, fieldName, typeName) {
return transformSchema(schema, [
new StripNonQueryTransform(),
new RenameTypes(name => `${typeName}_${name}`),
new NamespaceUnderFieldTransform(typeName, fieldName),
])
}
}