@apollo/client#getApolloContext TypeScript Examples

The following examples show how to use @apollo/client#getApolloContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useFragment.ts    From apollo-cache-policies with Apache License 2.0 6 votes vote down vote up
// A hook for subscribing to a fragment in the Apollo cache from a React component.
export default function useFragment<FragmentType>(fragment: DocumentNode, options: UseFragmentOptions) {
  const context = useContext(getApolloContext());
  const client = context.client;
  const cache = client?.cache as unknown as InvalidationPolicyCache;
  const fieldName = useFragmentTypePolicyFieldName();

  const queryForFragment = useOnce(() => buildWatchFragmentQuery({
    fragment,
    fieldName,
    id: options.id,
    policies: cache.policies,
  }));

  return useGetQueryDataByFieldName<FragmentType | null>(queryForFragment, fieldName);
}
Example #2
Source File: useFragmentWhere.ts    From apollo-cache-policies with Apache License 2.0 6 votes vote down vote up
// A hook for subscribing to a fragment for entities in the Apollo cache matching a given filter from a React component.
export default function useFragmentWhere<FragmentType>(fragment: DocumentNode, filter?: FragmentWhereFilter<FragmentType>) {
  const context = useContext(getApolloContext());
  const client = context.client;
  const cache = client?.cache as unknown as InvalidationPolicyCache;
  const fieldName = useFragmentTypePolicyFieldName();

  const query = useOnce(() => buildWatchFragmentWhereQuery({
    filter,
    fragment,
    fieldName,
    cache,
    policies: cache.policies,
  }));

  return useGetQueryDataByFieldName<FragmentType[]>(query, fieldName);
}