Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
? options.rule
.map(
(name): Rule => {
const rule = resolveRule(name);
if (!rule) {
renderer.error(`\Rule '${name}' does not exist!\n`);
return process.exit(1);
}
return rule;
},
)
.filter(f => f)
: [];
const changes = diffSchema(oldSchema, newSchema, rules);
if (!changes.length) {
renderer.success('No changes detected');
} else {
renderer.emit(
`\nDetected the following changes (${changes.length}) between schemas:\n`,
);
changes.forEach(change => {
renderer.emit(...renderChange(change));
});
if (hasBreaking(changes)) {
const breakingCount = changes.filter(
c => c.criticality.level === CriticalityLevel.Breaking,
).length;
.action(async (baseSchemaPtr: string) => {
try {
const config = await loadProjectConfig({
extensions: [DiffExtension]
})
if (!baseSchemaPtr) {
const diffConfig = await config.extension('diff');
baseSchemaPtr = diffConfig.baseSchema || 'git:origin/master:schema.graphql';
}
const [baseSchema, currentSchema] = await Promise.all([
config.loadSchema(baseSchemaPtr),
config.getSchema(),
]);
const changes = diff(baseSchema, currentSchema);
if (!changes.length) {
console.log(logSymbols.success, 'No changes detected');
} else {
console.warn(logSymbols.warning, `Detected the following changes (${changes.length}) between schemas:\n`);
changes.forEach(change => {
console.log(...renderChange(change));
});
if (hasBreaking(changes)) {
const breakingCount = changes.filter(c => c.criticality.level === CriticalityLevel.Breaking).length;
throw `Detected ${breakingCount} breaking change${breakingCount > 1 ? 's' : ''}\n`;
} else {
console.log(logSymbols.success, 'No breaking changes detected\n');
}
useEffect(() => {
try {
setChanges(diff(oldSchema, buildSchema(code)));
} catch (e) {
console.error(e);
}
}, [code]);
export const getBreakingChanges = async (
localSchemaSDL: string,
upstreamSchemaSDL: string
): Promise => {
const allChanges = diff(
buildSchema(localSchemaSDL),
buildSchema(upstreamSchemaSDL)
)
const breakings = allChanges.filter(c => c.criticality.level === "BREAKING")
const messages = breakings.map(c => c.message)
return messages
}
export async function diff({
path,
schemas,
}: {
path: string;
schemas: {
old: GraphQLSchema;
new: GraphQLSchema;
};
}): Promise {
const changes = diffSchemas(schemas.old, schemas.new);
if (!changes || !changes.length) {
return {
conclusion: CheckConclusion.Success,
};
}
const annotations = await Promise.all(
changes.map(change => annotate({path, change, schemas})),
);
let conclusion: CheckConclusion = CheckConclusion.Success;
if (
changes.some(
change => change.criticality.level === CriticalityLevel.Breaking,
)
let oldSchema: GraphQLSchema;
if (migrations.length) {
const last = migrations[migrations.length - 1];
oldSchema = buildSchema(last.model);
}
const newMigration: SchemaMigration = {
id: new Date().getTime(),
model: printSchema(this.schema)
};
let changes: ModelChange[] = [];
if (oldSchema) {
const inspectorChanges = diff(oldSchema, this.schema);
changes = mapModelChanges(inspectorChanges);
} else {
changes = this.getContext().map((model: InputModelTypeContext) => {
return {
type: ModelChangeType.TYPE_ADDED,
path: {
type: model.name
}
}
});
}
if (!changes.length) {
return undefined;
}
public getChanges(): Change[] {
let changes: Change[] = [];
if (!this.previousSchemaText || !this.previousSchemaText.length) {
return changes;
}
const oldSchema = buildSchema(this.previousSchemaText);
const newSchema = buildSchema(this.currentSchemaText);
if (oldSchema && newSchema) {
changes = diff(oldSchema, newSchema);
}
return this.getValidChangeTypes(changes);
}