Skip to content
Merged
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
1 change: 1 addition & 0 deletions config/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const reactSharedTestFileIgnoreList = [
ignoreDTSFiles,
ignoreTSFiles,
"src/react/hooks/__tests__/useBackgroundQuery/testUtils.tsx",
"src/react/hooks/__tests__/useLoadableQuery/testUtils.tsx",
"src/react/hooks/__tests__/useSuspenseQuery/testUtils.tsx",
"src/react/query-preloader/__tests__/createQueryPreloader/testUtils.tsx",
];
Expand Down
201 changes: 200 additions & 1 deletion src/core/__tests__/client.mutate/customScalars.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { delay, of } from "rxjs";

import type { OperationVariables, TypedDocumentNode } from "@apollo/client";
import { ApolloClient, ApolloLink, gql, NetworkStatus } from "@apollo/client";
import {
ApolloClient,
ApolloLink,
CombinedGraphQLErrors,
gql,
NetworkStatus,
} from "@apollo/client";
import { InMemoryCache } from "@apollo/client/cache";
import { MockLink } from "@apollo/client/testing";
import { dateScalar, ObservableStream } from "@apollo/client/testing/internal";
Expand Down Expand Up @@ -658,3 +664,196 @@ test("parses custom scalar fields in queries triggered by refetchQueries", async
},
});
});

test("serializes scalar fields in the error with a `none` error policy", async () => {
const mutation = gql`
mutation CreateEvent {
createEvent {
id
startDate
endDate
}
}
`;
const client = new ApolloClient({
cache: new InMemoryCache({
scalars: { Date: dateScalar },
typePolicies: {
Event: {
fields: {
startDate: { scalar: "Date" },
endDate: { scalar: "Date" },
},
},
},
}),
link: new ApolloLink(() =>
of({
data: {
createEvent: {
__typename: "Event",
id: "1",
startDate: "2026-01-01",
endDate: null,
},
},
errors: [
{
message: "Could not resolve endDate",
path: ["createEvent", "endDate"],
},
],
}).pipe(delay(20))
),
});

await expect(
client.mutate({ mutation, errorPolicy: "none" })
).rejects.toEqual(
new CombinedGraphQLErrors({
data: {
createEvent: {
__typename: "Event",
id: "1",
startDate: "2026-01-01",
endDate: null,
},
},
errors: [
{
message: "Could not resolve endDate",
path: ["createEvent", "endDate"],
},
],
})
);
});

test("parses scalar fields in the result and serializes them in the error with an `all` error policy", async () => {
const mutation = gql`
mutation CreateEvent {
createEvent {
id
startDate
endDate
}
}
`;
const client = new ApolloClient({
cache: new InMemoryCache({
scalars: { Date: dateScalar },
typePolicies: {
Event: {
fields: {
startDate: { scalar: "Date" },
endDate: { scalar: "Date" },
},
},
},
}),
link: new ApolloLink(() =>
of({
data: {
createEvent: {
__typename: "Event",
id: "1",
startDate: "2026-01-01",
endDate: null,
},
},
errors: [
{
message: "Could not resolve endDate",
path: ["createEvent", "endDate"],
},
],
}).pipe(delay(20))
),
});

await expect(
client.mutate({ mutation, errorPolicy: "all" })
).resolves.toStrictEqualTyped({
data: {
createEvent: {
__typename: "Event",
id: "1",
startDate: new Date(2026, 0, 1),
endDate: null,
},
},
error: new CombinedGraphQLErrors({
data: {
createEvent: {
__typename: "Event",
id: "1",
// TODO: Determine if this is correct
startDate: new Date(2026, 0, 1),
endDate: null,
},
},
errors: [
{
message: "Could not resolve endDate",
path: ["createEvent", "endDate"],
},
],
}),
});
});

test("parses custom scalar fields with an `ignore` error policy", async () => {
const mutation = gql`
mutation CreateEvent {
createEvent {
id
startDate
endDate
}
}
`;
const client = new ApolloClient({
cache: new InMemoryCache({
scalars: { Date: dateScalar },
typePolicies: {
Event: {
fields: {
startDate: { scalar: "Date" },
endDate: { scalar: "Date" },
},
},
},
}),
link: new ApolloLink(() =>
of({
data: {
createEvent: {
__typename: "Event",
id: "1",
startDate: "2026-01-01",
endDate: null,
},
},
errors: [
{
message: "Could not resolve endDate",
path: ["createEvent", "endDate"],
},
],
}).pipe(delay(20))
),
});

await expect(
client.mutate({ mutation, errorPolicy: "ignore" })
).resolves.toStrictEqualTyped({
data: {
createEvent: {
__typename: "Event",
id: "1",
startDate: new Date(2026, 0, 1),
endDate: null,
},
},
});
});
Loading
Loading