Skip to content

Commit

Permalink
feat: add configurable schema linting (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus authored Mar 11, 2024
1 parent 93419b1 commit c662485
Show file tree
Hide file tree
Showing 38 changed files with 10,833 additions and 398 deletions.
53 changes: 46 additions & 7 deletions cli/src/commands/subgraph/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default (opts: BaseCommandOptions) => {

const changesTable = new Table({
head: [pc.bold(pc.white('CHANGE')), pc.bold(pc.white('TYPE')), pc.bold(pc.white('DESCRIPTION'))],
colWidths: [15, 30, 80],
wordWrap: true,
});

Expand All @@ -100,22 +99,34 @@ export default (opts: BaseCommandOptions) => {
wordWrap: true,
});

const lintIssuesTable = new Table({
head: [pc.bold(pc.white('LINT_RULE')), pc.bold(pc.white('ERROR_MESSAGE')), pc.bold(pc.white('LINE NUMBER'))],
colAligns: ['left', 'left', 'center'],
wordWrap: true,
});

let success = false;
let finalStatement = '';

let studioCheckDestination = '';
if (resp.checkId && resp.checkedFederatedGraphs.length > 0) {
studioCheckDestination = `Open in studio: ${config.webURL}/${resp.checkedFederatedGraphs[0].organizationSlug}/${resp.checkedFederatedGraphs[0].namespace}/graph/${resp.checkedFederatedGraphs[0].name}/checks/${resp.checkId}`;
studioCheckDestination = `${pc.bold('Open in studio')}: ${config.webURL}/${
resp.checkedFederatedGraphs[0].organizationSlug
}/${resp.checkedFederatedGraphs[0].namespace}/graph/${resp.checkedFederatedGraphs[0].name}/checks/${
resp.checkId
}`;
}

switch (resp.response?.code) {
case EnumStatusCode.OK: {
if (
resp.nonBreakingChanges.length === 0 &&
resp.breakingChanges.length === 0 &&
resp.compositionErrors.length === 0
resp.compositionErrors.length === 0 &&
resp.lintErrors.length === 0 &&
resp.lintWarnings.length === 0
) {
console.log(`\nDetected no changes.\n${studioCheckDestination}\n`);
console.log(`\nDetected no changes.\nDetected no lint issues.\n\n${studioCheckDestination}\n`);

success = true;

Expand Down Expand Up @@ -167,13 +178,21 @@ export default (opts: BaseCommandOptions) => {

if (resp.breakingChanges.length > 0) {
for (const breakingChange of resp.breakingChanges) {
changesTable.push([pc.red('BREAKING'), breakingChange.changeType, breakingChange.message]);
changesTable.push([
`${logSymbols.error} ${pc.red('BREAKING')}`,
breakingChange.changeType,
breakingChange.message,
]);
}
}

if (resp.nonBreakingChanges.length > 0) {
for (const nonBreakingChange of resp.nonBreakingChanges) {
changesTable.push(['NON-BREAKING', nonBreakingChange.changeType, nonBreakingChange.message]);
changesTable.push([
`${logSymbols.success} NON-BREAKING`,
nonBreakingChange.changeType,
nonBreakingChange.message,
]);
}
}

Expand All @@ -192,12 +211,32 @@ export default (opts: BaseCommandOptions) => {
console.log(compositionErrorsTable.toString());
}

if (resp.lintErrors.length > 0 || resp.lintWarnings.length > 0) {
success = resp.lintErrors.length === 0;
console.log('\nDetected lint issues:');
for (const error of resp.lintErrors) {
lintIssuesTable.push([
`${logSymbols.error} ${pc.red(error.lintRuleType)}`,
error.message,
error.issueLocation?.line,
]);
}
for (const warning of resp.lintWarnings) {
lintIssuesTable.push([
`${logSymbols.warning} ${pc.yellow(warning.lintRuleType)}`,
warning.message,
warning.issueLocation?.line,
]);
}
console.log(lintIssuesTable.toString());
}

if (success) {
console.log(
'\n' +
logSymbols.success +
pc.green(` Schema check passed. ${finalStatement}`) +
'\n' +
'\n\n' +
studioCheckDestination +
'\n',
);
Expand Down
180 changes: 101 additions & 79 deletions composition-go/index.global.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions composition/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export * from './schema-building/type-merging';
export * from './schema-building/utils';
export * from './subgraph/subgraph';
export * from './utils/utils';
export * from './utils/constants';
export * from './warnings/warnings';
24 changes: 24 additions & 0 deletions composition/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,27 @@ export const SCOPE_SCALAR_DEFINITION: MutableScalarNode = {
export const MAXIMUM_TYPE_NESTING = 30;

export const INHERITABLE_DIRECTIVE_NAMES = [EXTERNAL, SHAREABLE];

export const baseDirectives = `
directive @deprecated(reason: String = "No longer supported") on ARGUMENT_DEFINITION | ENUM_VALUE | FIELD_DEFINITION | INPUT_FIELD_DEFINITION
directive @extends on INTERFACE | OBJECT
directive @external on FIELD_DEFINITION | OBJECT
directive @eventsPublish(topic: String!, sourceID: String) on FIELD_DEFINITION
directive @eventsRequest(topic: String!, sourceID: String) on FIELD_DEFINITION
directive @eventsSubscribe(topic: String!, sourceID: String) on FIELD_DEFINITION
directive @key(fields: openfed__FieldSet!, resolvable: Boolean = true) repeatable on INTERFACE | OBJECT
directive @provides(fields: openfed__FieldSet!) on FIELD_DEFINITION
directive @requires(fields: openfed__FieldSet!) on FIELD_DEFINITION
directive @specifiedBy(url: String!) on SCALAR
directive @tag(name: String!) repeatable on ARGUMENT_DEFINITION | ENUM | ENUM_VALUE | FIELD_DEFINITION | INPUT_FIELD_DEFINITION | INPUT_OBJECT | INTERFACE | OBJECT | SCALAR | UNION
directive @authenticated on ENUM | FIELD_DEFINITION | INTERFACE | OBJECT | SCALAR
directive @composeDirective(name: String!) repeatable on SCHEMA
directive @inaccessible on ARGUMENT_DEFINITION | ENUM | ENUM_VALUE | FIELD_DEFINITION | INPUT_FIELD_DEFINITION | INPUT_OBJECT | INTERFACE | OBJECT | SCALAR | UNION
directive @interfaceObject on OBJECT
directive @link(url: String!, as: String, for: String, import: [String]) repeatable on SCHEMA
directive @override(from: String!) on FIELD_DEFINITION
directive @requiresScopes(scopes: [[openfed__Scope!]!]!) on ENUM | FIELD_DEFINITION | INTERFACE | OBJECT | SCALAR
directive @shareable on FIELD_DEFINITION | OBJECT
scalar openfed__FieldSet
scalar openfed__Scope
`;
Loading

0 comments on commit c662485

Please sign in to comment.