Skip to main content

watchFragment

Watch a GraphQL fragment in the cache and get updates when the fragment changes.

API

Apollo.watchFragment<TData, TVariables>(
options: WatchFragmentOptions<TData, TVariables>
): Observable<WatchFragmentResult<TData>>

Returns an Observable which emits the current fragment value in the cache. This observable is long-lasting and continues to deliver fragment value changes until unsubscribed from.

Watching a fragment

this.apollo.watchFragment({
fragment: AuthorFragmentDoc,
from: {
__typename: 'Author',
id: '1'
}
}).pipe(
map(result => result.data as AuthorFragment)
);
tip

In the example above, __typename can be omitted (e.g. from: { id: '1' })
Apollo Orbit will automatically extract the type name from the first fragment definition it encounters in the document, even if it contains multiple fragments.