Skip to content

Commit

Permalink
useFragment: remove returnPartialData option (#10836)
Browse files Browse the repository at this point in the history
* Apply suggestions from code review

Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
  • Loading branch information
phryneas and jerelmiller authored May 9, 2023
1 parent 663aa3e commit 6794893
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .changeset/short-monkeys-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
'@apollo/client': patch

Remove the deprecated `returnPartialData` option from `useFragment` hook.
68 changes: 17 additions & 51 deletions src/react/hooks/__tests__/useFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, waitFor, screen, renderHook } from "@testing-library/react";
import userEvent from '@testing-library/user-event';
import { act } from "react-dom/test-utils";

import { useFragment_experimental as useFragment } from "../useFragment";
import { UseFragmentOptions, useFragment_experimental as useFragment } from "../useFragment";
import { MockedProvider } from "../../../testing";
import { ApolloProvider } from "../../context";
import {
Expand All @@ -14,10 +14,13 @@ import {
ApolloClient,
Observable,
ApolloLink,
StoreObject,
DocumentNode,
} from "../../../core";
import { useQuery } from "../useQuery";
import { concatPagination } from "../../../utilities";
import assert from "assert";
import { expectTypeOf } from 'expect-type'

describe("useFragment", () => {
it("is importable and callable", () => {
Expand Down Expand Up @@ -990,7 +993,6 @@ describe("useFragment", () => {
fragment: ListAndItemFragments,
fragmentName: "ListFragment",
from: { __typename: "Query" },
returnPartialData: true,
}),
{ wrapper },
);
Expand Down Expand Up @@ -1327,56 +1329,8 @@ describe("useFragment", () => {
expect(result.current.data).toEqual({ __typename: "Item", id: 5 });
expect(result.current.complete).toBe(false);
});

it("throws an exception with `returnPartialData: false` if only partial data is available", () => {
// this is actually not intended behavior, but it is the current behavior
// let's document it in a test until we remove `returnPartialData` in 3.8

let error: Error;

renderHook(
() => {
// we can't just `expect(() => renderHook(...)).toThrow(...)` because it will render a second time, resulting in an uncaught exception
try {
useFragment({
fragment: ItemFragment,
from: { __typename: "Item", id: 5 },
returnPartialData: false,
});
} catch (e) {
error = e;
}
},
{ wrapper }
);

expect(error!.toString()).toMatch(`Error: Can't find field 'text' on Item:5 object`);
});

it("throws an exception with `returnPartialData: false` if no data is available", () => {
// this is actually not intended behavior, but it is the current behavior
// let's document it in a test until we remove `returnPartialData` in 3.8
let error: Error;

renderHook(
() => {
// we can't just `expect(() => renderHook(...)).toThrow(...)` because it will render a second time, resulting in an uncaught exception
try {
useFragment({
fragment: ItemFragment,
from: { __typename: "Item", id: 6 },
returnPartialData: false,
});
} catch (e) {
error = e;
}
},
{ wrapper }
);

expect(error!.toString()).toMatch(`Error: Dangling reference to missing Item:6 object`);
});
});

describe("return value `complete` property", () => {
let cache: InMemoryCache, wrapper: React.FunctionComponent;
const ItemFragment = gql`
Expand Down Expand Up @@ -1475,4 +1429,16 @@ describe.skip("Type Tests", () => {
}
});
})

test("UseFragmentOptions interface shape", <TData, TVars>()=>{
expectTypeOf<UseFragmentOptions<TData, TVars>>()
.toEqualTypeOf<{
from: string | StoreObject | Reference;
fragment: DocumentNode | TypedDocumentNode<TData, TVars>;
fragmentName?: string;
optimistic?: boolean;
variables?: TVars;
canonizeResults?: boolean;
}>();
})
})
25 changes: 2 additions & 23 deletions src/react/hooks/useFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,8 @@ extends Omit<
from: StoreObject | Reference | string;
// Override this field to make it optional (default: true).
optimistic?: boolean;

/**
* Whether to return incomplete data rather than null.
* Defaults to `true`.
* @deprecated This option will be removed in Apollo Client 3.8.
* Please check `result.missing` instead.
*/
returnPartialData?: boolean;
}

// Since the above definition of UseFragmentOptions can be hard to parse without
// help from TypeScript/VSCode, here are the intended fields and their types.
// Uncomment this code to check that it's consistent with the definition above.
//
// export interface UseFragmentOptions<TData, TVars> {
// from: string | StoreObject | Reference;
// fragment: DocumentNode | TypedDocumentNode<TData, TVars>;
// fragmentName?: string;
// optimistic?: boolean;
// variables?: TVars;
// returnPartialData?: boolean;
// canonizeResults?: boolean;
// }

export type UseFragmentResult<TData> =
| {
data: TData;
Expand Down Expand Up @@ -84,9 +62,10 @@ export function useFragment_experimental<

const diffOptions: Cache.DiffOptions<TData, TVars> = {
...rest,
returnPartialData: true,
id: typeof from === "string" ? from : cache.identify(from),
query: cache["getFragmentDoc"](fragment, fragmentName),
optimistic,
optimistic
};

const resultRef = useRef<UseFragmentResult<TData>>();
Expand Down

0 comments on commit 6794893

Please sign in to comment.