Skip to content

Commit

Permalink
Merge branch 'main' into fix-type
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas authored Aug 26, 2024
2 parents fec8704 + a3c26e3 commit d9ab0d1
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Kind } from "graphql";
import { Observable } from "../utilities";
import { ApolloLink } from "../link/core";
import { HttpLink } from "../link/http";
import { InMemoryCache } from "../cache";
import { createFragmentRegistry, InMemoryCache } from "../cache";
import { itAsync } from "../testing";
import { ObservableStream, spyOnConsole } from "../testing/internal";
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
Expand Down Expand Up @@ -2515,6 +2515,120 @@ describe("ApolloClient", () => {
});
}
});

it("works with nested fragments", async () => {
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});

const ItemNestedFragment = gql`
fragment ItemNestedFragment on Item {
complete
}
`;

const ItemFragment = gql`
fragment ItemFragment on Item {
id
text
...ItemNestedFragment
}
${ItemNestedFragment}
`;

cache.writeFragment({
fragment: ItemFragment,
fragmentName: "ItemFragment",
data: {
__typename: "Item",
id: 5,
text: "Item #5",
complete: true,
},
});

const observable = client.watchFragment({
fragment: ItemFragment,
fragmentName: "ItemFragment",
from: { __typename: "Item", id: 5 },
});

const stream = new ObservableStream(observable);

{
const result = await stream.takeNext();

expect(result).toEqual({
data: {
__typename: "Item",
id: 5,
text: "Item #5",
complete: true,
},
complete: true,
});
}
});

it("can use the fragment registry for nested fragments", async () => {
const fragments = createFragmentRegistry();
const cache = new InMemoryCache({ fragments });

fragments.register(gql`
fragment ItemNestedFragment on Item {
complete
}
`);

const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});

const ItemFragment = gql`
fragment ItemFragment on Item {
id
text
...ItemNestedFragment
}
`;

cache.writeFragment({
fragment: ItemFragment,
fragmentName: "ItemFragment",
data: {
__typename: "Item",
id: 5,
text: "Item #5",
complete: true,
},
});

const observable = client.watchFragment({
fragment: ItemFragment,
fragmentName: "ItemFragment",
from: { __typename: "Item", id: 5 },
});

const stream = new ObservableStream(observable);

{
const result = await stream.takeNext();

expect(result).toEqual({
data: {
__typename: "Item",
id: 5,
text: "Item #5",
complete: true,
},
complete: true,
});
}
});
});

describe("defaultOptions", () => {
Expand Down

0 comments on commit d9ab0d1

Please sign in to comment.