Skip to content

Commit

Permalink
Remove usage of classes for error codes and related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Lebresne committed Jan 3, 2022
1 parent f40ea18 commit e9487de
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 234 deletions.
2 changes: 1 addition & 1 deletion composition-js/src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function compose(subgraphs: Subgraphs): CompositionResult {
const federatedQueryGraph = buildFederatedQueryGraph(supergraphSchema, false);
const validationResult = validateGraphComposition(supergraphQueryGraph, federatedQueryGraph);
if (validationResult.errors) {
return { errors: validationResult.errors.map(e => ERRORS.SATISFIABILITY_ERROR.err(e.message)) };
return { errors: validationResult.errors.map(e => ERRORS.SATISFIABILITY_ERROR.err({ message: e.message })) };
}

// printSchema calls validateOptions, which can throw
Expand Down
46 changes: 26 additions & 20 deletions composition-js/src/merging/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function copyTypeReference(source: Type, dest: Schema): Type {
default:
const type = dest.type(source.name);
assert(type, () => `Cannot find type ${source} in destination schema (with types: ${dest.types().join(', ')})`);
return type!;
return type;
}
}

Expand Down Expand Up @@ -341,7 +341,7 @@ class Merger {
}

if (!this.merged.schemaDefinition.rootType('query')) {
this.errors.push(ERRORS.NO_QUERIES.err("No queries found in any subgraph: a supergraph must have a query root type."));
this.errors.push(ERRORS.NO_QUERIES.err({ message: "No queries found in any subgraph: a supergraph must have a query root type." }));
}

// If we already encountered errors, `this.merged` is probably incomplete. Let's not risk adding errors that
Expand Down Expand Up @@ -445,8 +445,11 @@ class Merger {
mismatchAccessor,
(elt, names) => `${elt} in ${names}`,
(elt, names) => `${elt} in ${names}`,
(distribution, astNodes) => {
this.errors.push(code.err(message + join(distribution, ' and ', ' but '), astNodes));
(distribution, nodes) => {
this.errors.push(code.err({
message: message + join(distribution, ' and ', ' but '),
nodes
}));
},
elt => !elt
);
Expand All @@ -469,8 +472,11 @@ class Merger {
mismatchAccessor,
supergraphElementPrinter,
otherElementsPrinter,
(distribution, astNodes) => {
this.errors.push(code.err(message + distribution[0] + join(distribution.slice(1), ' and '), astNodes));
(distribution, nodes) => {
this.errors.push(code.err({
message: message + distribution[0] + join(distribution.slice(1), ' and '),
nodes
}));
},
ignorePredicate,
includeMissingSources
Expand Down Expand Up @@ -684,10 +690,10 @@ class Merger {
}
if (extensionSubgraphs.length > 0 && defSubgraphs.length === 0) {
for (const [i, subgraph] of extensionSubgraphs.entries()) {
this.errors.push(ERRORS.EXTENSION_WITH_NO_BASE.err(
`[${subgraph}] Type "${dest}" is an extension type, but there is no type definition for "${dest}" in any subgraph.`,
extensionASTs[i]
));
this.errors.push(ERRORS.EXTENSION_WITH_NO_BASE.err({
message: `[${subgraph}] Type "${dest}" is an extension type, but there is no type definition for "${dest}" in any subgraph.`,
nodes: extensionASTs[i],
}));
}
}
}
Expand Down Expand Up @@ -836,11 +842,11 @@ class Merger {
private mergeField(sources: (FieldDefinition<any> | undefined)[], dest: FieldDefinition<any>) {
if (sources.every((s, i) => s === undefined || this.isExternal(i, s))) {
const definingSubgraphs = sources.map((source, i) => source ? this.names[i] : undefined).filter(s => s !== undefined) as string[];
const asts = sources.map(source => source?.sourceAST).filter(s => s !== undefined) as ASTNode[];
this.errors.push(ERRORS.EXTERNAL_MISSING_ON_BASE.err(
`Field "${dest.coordinate}" is marked @external on all the subgraphs in which it is listed (${printSubgraphNames(definingSubgraphs)}).`,
asts
));
const nodes = sources.map(source => source?.sourceAST).filter(s => s !== undefined) as ASTNode[];
this.errors.push(ERRORS.EXTERNAL_MISSING_ON_BASE.err({
message: `Field "${dest.coordinate}" is marked @external on all the subgraphs in which it is listed (${printSubgraphNames(definingSubgraphs)}).`,
nodes
}));
return;
}

Expand Down Expand Up @@ -1633,14 +1639,14 @@ class Merger {
const typeInSubgraph = s.type(type.name);
return typeInSubgraph !== undefined && (typeInSubgraph as ObjectType | InterfaceType).implementsInterface(itf.name);
});
this.errors.push(ERRORS.INTERFACE_FIELD_NO_IMPLEM.err(
`Interface field "${itfField.coordinate}" is declared in ${printSubgraphNames(subgraphsWithTheField)} but type "${type}", `
+ `which implements "${itf}" only in ${printSubgraphNames(subgraphsWithTypeImplementingItf)} does not have field "${itfField.name}".`,
sourceASTs(
this.errors.push(ERRORS.INTERFACE_FIELD_NO_IMPLEM.err({
message: `Interface field "${itfField.coordinate}" is declared in ${printSubgraphNames(subgraphsWithTheField)} but type "${type}", `
+ `which implements "${itf}" only in ${printSubgraphNames(subgraphsWithTypeImplementingItf)} does not have field "${itfField.name}".`,
nodes: sourceASTs(
...subgraphsWithTheField.map(s => this.subgraphByName(s).typeOfKind<InterfaceType>(itf.name, 'InterfaceType')?.field(itfField.name)),
...subgraphsWithTypeImplementingItf.map(s => this.subgraphByName(s).type(type.name))
)
));
}));
continue;
}

Expand Down
Loading

0 comments on commit e9487de

Please sign in to comment.