@@ -25,7 +25,7 @@ export interface UseFragmentOptions<TData, TVars>
2525 Cache . ReadFragmentOptions < TData , TVars > ,
2626 "id" | "variables" | "returnPartialData"
2727 > {
28- from : StoreObject | Reference | FragmentType < NoInfer < TData > > | string ;
28+ from : StoreObject | Reference | FragmentType < NoInfer < TData > > | string | null ;
2929 // Override this field to make it optional (default: true).
3030 optimistic ?: boolean ;
3131 /**
@@ -73,7 +73,10 @@ function _useFragment<TData = any, TVars = OperationVariables>(
7373 // `stableOptions` and retrigger our subscription. If the cache identifier
7474 // stays the same between renders, we want to reuse the existing subscription.
7575 const id = React . useMemo (
76- ( ) => ( typeof from === "string" ? from : cache . identify ( from ) ) ,
76+ ( ) =>
77+ typeof from === "string" ? from
78+ : from === null ? null
79+ : cache . identify ( from ) ,
7780 [ cache , from ]
7881 ) ;
7982
@@ -83,6 +86,16 @@ function _useFragment<TData = any, TVars = OperationVariables>(
8386 // get the correct diff on the next render given new diffOptions
8487 const diff = React . useMemo ( ( ) => {
8588 const { fragment, fragmentName, from, optimistic = true } = stableOptions ;
89+
90+ if ( from === null ) {
91+ return {
92+ result : diffToResult ( {
93+ result : { } as TData ,
94+ complete : false ,
95+ } ) ,
96+ } ;
97+ }
98+
8699 const { cache } = client ;
87100 const diff = cache . diff < TData > ( {
88101 ...stableOptions ,
@@ -111,24 +124,28 @@ function _useFragment<TData = any, TVars = OperationVariables>(
111124 React . useCallback (
112125 ( forceUpdate ) => {
113126 let lastTimeout = 0 ;
114- const subscription = client . watchFragment ( stableOptions ) . subscribe ( {
115- next : ( result ) => {
116- // Since `next` is called async by zen-observable, we want to avoid
117- // unnecessarily rerendering this hook for the initial result
118- // emitted from watchFragment which should be equal to
119- // `diff.result`.
120- if ( equal ( result , diff . result ) ) return ;
121- diff . result = result ;
122- // If we get another update before we've re-rendered, bail out of
123- // the update and try again. This ensures that the relative timing
124- // between useQuery and useFragment stays roughly the same as
125- // fixed in https://github.com/apollographql/apollo-client/pull/11083
126- clearTimeout ( lastTimeout ) ;
127- lastTimeout = setTimeout ( forceUpdate ) as any ;
128- } ,
129- } ) ;
127+
128+ const subscription =
129+ stableOptions . from === null ?
130+ null
131+ : client . watchFragment ( stableOptions ) . subscribe ( {
132+ next : ( result ) => {
133+ // Since `next` is called async by zen-observable, we want to avoid
134+ // unnecessarily rerendering this hook for the initial result
135+ // emitted from watchFragment which should be equal to
136+ // `diff.result`.
137+ if ( equal ( result , diff . result ) ) return ;
138+ diff . result = result ;
139+ // If we get another update before we've re-rendered, bail out of
140+ // the update and try again. This ensures that the relative timing
141+ // between useQuery and useFragment stays roughly the same as
142+ // fixed in https://github.com/apollographql/apollo-client/pull/11083
143+ clearTimeout ( lastTimeout ) ;
144+ lastTimeout = setTimeout ( forceUpdate ) as any ;
145+ } ,
146+ } ) ;
130147 return ( ) => {
131- subscription . unsubscribe ( ) ;
148+ subscription ? .unsubscribe ( ) ;
132149 clearTimeout ( lastTimeout ) ;
133150 } ;
134151 } ,
0 commit comments