watchFragment
Watch a GraphQL fragment in the cache and get updates when the fragment changes.
API
Apollo.watchFragment<TData, TVariables>(
options: ApolloClient.WatchFragmentOptions<TData, TVariables>
): ApolloClient.ObservableFragment<TData>
Returns an ObservableFragment, which emits the current fragment value in the cache and continues to deliver changes until unsubscribed from. Each emission is an ApolloClient.WatchFragmentResult<TData>: narrow it on complete to reach fully typed data.
Alongside the usual Observable members it carries getCurrentResult(), which reads the cache synchronously without subscribing. That is what signal.fragment uses to have data available on its first read.
Watching a fragment
this.apollo.watchFragment({
fragment: AuthorFragmentDoc,
from: {
__typename: 'Author',
id: '1'
}
}).pipe(
map(result => result.complete ? result.data : undefined)
);
In the example above, __typename can be omitted (e.g. from: { id: '1' })
Orbit extracts the type name from the fragment selected by fragmentName. When the document contains multiple fragments, supply fragmentName to select the one to read. Without it, type-name inference falls back to the first fragment definition.
If you declare a stricter FromOptionValue type, inputs must satisfy it, including __typename when required. See Orbit's fragment-reference support.