Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use the GraphQLError class to produce the error #175

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions federation-2/router-bridge/js-src/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
DocumentNode,
ExecutionResult,
GraphQLError,
GraphQLSchema,
parse,
validate,
Expand All @@ -25,7 +26,9 @@ import {
import { ReferencedFieldsForType } from "apollo-reporting-protobuf";

const PARSE_FAILURE: string = "## GraphQLParseFailure\n";
const PARSE_FAILURE_EXT_CODE: string = "GRAPHQL_PARSE_FAILED";
const VALIDATION_FAILURE: string = "## GraphQLValidationFailure\n";
const VALIDATION_FAILURE_EXT_CODE: string = "GRAPHQL_VALIDATION_FAILED";
const UNKNOWN_OPERATION: string = "## GraphQLUnknownOperationName\n";

export type ReferencedFieldsByType = Record<string, ReferencedFieldsForType>;
Expand Down Expand Up @@ -77,7 +80,14 @@ export class BridgeQueryPlanner {
statsReportKey: PARSE_FAILURE,
referencedFieldsByType: {},
},
errors: [parseError],
errors: [
{
...parseError,
extensions: {
code: PARSE_FAILURE_EXT_CODE,
},
},
],
};
}

Expand All @@ -90,7 +100,25 @@ export class BridgeQueryPlanner {
statsReportKey: VALIDATION_FAILURE,
referencedFieldsByType: {},
},
errors: validationErrors,
errors: validationErrors.map((error): GraphQLError => {
if (
error.extensions == null ||
Object.keys(error.extensions).length === 0
) {
return new GraphQLError(error.message, {
extensions: {
code: VALIDATION_FAILURE_EXT_CODE,
},
path: error.path,
nodes: error.nodes,
originalError: error.originalError,
positions: error.positions,
source: error.source,
});
}

return error;
}),
};
}

Expand All @@ -116,7 +144,14 @@ export class BridgeQueryPlanner {
statsReportKey,
referencedFieldsByType: {},
},
errors: [e],
errors: [
{
...e,
extensions: {
code: VALIDATION_FAILURE_EXT_CODE,
},
},
],
};
}

Expand Down
30 changes: 26 additions & 4 deletions federation-2/router-bridge/js-src/plan_worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLErrorExt } from "@apollo/core-schema/dist/error";
import { QueryPlannerConfig } from "@apollo/query-planner";
import { ASTNode, Source, SourceLocation } from "graphql";
import { ASTNode, GraphQLError, Source, SourceLocation } from "graphql";
import {
BridgeQueryPlanner,
ExecutionResultWithUsageReporting,
Expand Down Expand Up @@ -191,10 +191,21 @@ async function run() {
}
} catch (e) {
logger.warn(`an error happened in the worker runtime ${e}\n`);

const unexpectedError = new GraphQLError(e.message, {
extensions: {
code: "QUERY_PLANNING_FAILED",
exception: {
stacktrace: e.toString().split(/\n/),
},
},
});
unexpectedError.name = e.name || "unknown";

await send({
id,
payload: {
errors: [e],
errors: [unexpectedError],
usageReporting: {
statsReportKey: "",
referencedFieldsByType: {},
Expand All @@ -203,10 +214,21 @@ async function run() {
});
}
} catch (e) {
logger.warn(`plan_worker: an unknown error occured ${e}\n`);
logger.warn(`plan_worker: an unknown error occurred ${e}\n`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also fix a typo.


const unexpectedError = new GraphQLError(e.message, {
extensions: {
code: "QUERY_PLANNING_FAILED",
exception: {
stacktrace: e.toString().split(/\n/),
},
},
});
unexpectedError.name = e.name || "unknown";

await send({
payload: {
errors: [e],
errors: [unexpectedError],
usageReporting: {
statsReportKey: "",
referencedFieldsByType: {},
Expand Down
2 changes: 1 addition & 1 deletion federation-2/router-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
"node": "16.13.2",
"npm": "8.3.1"
}
}
}
Loading