Skip to content

Commit

Permalink
adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jul 9, 2024
1 parent 4a3bdc0 commit d4abb02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 47 deletions.
7 changes: 3 additions & 4 deletions .changeset/slimy-berries-yawn.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ to begin with, and the `GraphQLError` class has additional properties that can
never be correctly rehydrated from a GraphQL result.
The correct type to use here is `GraphQLFormattedError`.

Similarly, please ensure to use types like
* `FormattedExecutionResult` instead of `ExecutionResult`
the non-"Formatted" versions are for use on the server only,
not for use in client code.
Similarly, please ensure to use the type `FormattedExecutionResult`
instead of `ExecutionResult` - the non-"Formatted" versions of these types
are for use on the server only, but don't get transported over the network.
51 changes: 8 additions & 43 deletions src/errors/__tests__/ApolloError.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ApolloError } from "..";
import { ExecutableDefinitionNode, GraphQLError, parse, Source } from "graphql";

describe("ApolloError", () => {
it("should construct itself correctly", () => {
const graphQLErrors = [
new GraphQLError("Something went wrong with GraphQL"),
new GraphQLError("Something else went wrong with GraphQL"),
{ message: "Something went wrong with GraphQL" },
{ message: "Something else went wrong with GraphQL" },
];
const protocolErrors = [
{
Expand Down Expand Up @@ -41,7 +40,7 @@ describe("ApolloError", () => {
});

it("should add a graphql error to the message", () => {
const graphQLErrors = [new GraphQLError("this is an error message")];
const graphQLErrors = [{ message: "this is an error message" }];
const apolloError = new ApolloError({
graphQLErrors,
});
Expand All @@ -51,8 +50,8 @@ describe("ApolloError", () => {

it("should add multiple graphql errors to the message", () => {
const graphQLErrors = [
new GraphQLError("this is new"),
new GraphQLError("this is old"),
{ message: "this is new" },
{ message: "this is old" },
];
const apolloError = new ApolloError({
graphQLErrors,
Expand All @@ -64,7 +63,7 @@ describe("ApolloError", () => {
});

it("should add both network and graphql errors to the message", () => {
const graphQLErrors = [new GraphQLError("graphql error message")];
const graphQLErrors = [{ message: "graphql error message" }];
const networkError = new Error("network error message");
const apolloError = new ApolloError({
graphQLErrors,
Expand All @@ -77,7 +76,7 @@ describe("ApolloError", () => {
});

it("should add both protocol and graphql errors to the message", () => {
const graphQLErrors = [new GraphQLError("graphql error message")];
const graphQLErrors = [{ message: "graphql error message" }];
const protocolErrors = [
{
message: "cannot read message from websocket",
Expand All @@ -99,46 +98,12 @@ describe("ApolloError", () => {
});

it("should contain a stack trace", () => {
const graphQLErrors = [new GraphQLError("graphql error message")];
const graphQLErrors = [{ message: "graphql error message" }];
const networkError = new Error("network error message");
const apolloError = new ApolloError({
graphQLErrors,
networkError,
});
expect(apolloError.stack).toBeDefined();
});

it("will revive `GraphQLError` instances from `graphQLErrors`", () => {
const source = new Source(`
{
field
}
`);
const ast = parse(source);
const operationNode = ast.definitions[0] as ExecutableDefinitionNode;
const fieldNode = operationNode.selectionSet.selections[0];
const original = new GraphQLError("msg" /* message */, {
nodes: [fieldNode],
source,
positions: [1, 2, 3],
path: ["a", "b", "c"],
originalError: new Error("test"),
extensions: { foo: "bar" },
});

const apolloError = new ApolloError({
graphQLErrors: [JSON.parse(JSON.stringify(original))],
});
const graphQLError = apolloError.graphQLErrors[0];

console.log({
graphQLError,
original,
serialized: JSON.stringify(original),
});

expect(graphQLError).toBeInstanceOf(GraphQLError);
// test equality of enumberable fields. non-enumerable fields will differ
expect({ ...graphQLError }).toStrictEqual({ ...original });
});
});

0 comments on commit d4abb02

Please sign in to comment.