question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypePolicy on a request level

See original GitHub issue

With the deprecation of updateQuery on a fetchMore, there doesn’t seem to be a way to specify its replacement, a TypePolicy, in the same place. I understand that you can specify it on specific entities as seen in the docs and below:

const cache = new InMemoryCache({
  typePolicies: {
    Person: {
      fields: {
...

But in my use-case, I have a helper method used by various entities with the list query passed in:

import {useQuery} from "@apollo/client";
import {useEffect, useState} from "react";
import {nameFromQuery} from "lib/graphql/helpers";

export default function useFetchAll(query) {
  const [error, setError] = useState(null);
  const [loading, setLoading] = useState(true);
  const {data, fetchMore} = useQuery(query, {
    onError: setError,
    variables: {first: 100}
  });

  useEffect(() => {
    if(!data) { return; }

    const name = nameFromQuery(query);
    const {endCursor, hasNextPage} = data[name].pageInfo;

    if(hasNextPage) {
      fetchMore({
        updateQuery: (oldData, {fetchMoreResult: newData}) => ({
          ...newData,
          [name]: {
            ...newData[name],
            edges: [
              ...oldData[name].edges,
              ...newData[name].edges
            ]
          }
        }),
        variables: {after: endCursor}
      });
    } else {
      setLoading(false);
    }
  }, [data]);

  return {data, error, loading};
}

I’m not sure how I can convert this method to using type policies instead. There are a couple of ways to avoid this issue:

  • Type Policies for everything that uses this - but it would make the method unreliable
  • Skipping the cache - but it’d then it’d making a lot of extra calls if used multiple times

It’d make more sense to me if useQuery or fetchMore accepted a typePolicy, similar to how it accepts a fetchPolicy. That may be a naive statement, but there just seems like there should be an equivalent way of handling the issue if updateQuery is going away.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
heyrictcommented, Jan 3, 2021

I have a similar issue on migrating from v2 to v3 on cache updating. I used the same query while with different view types in my chat application.

Here is the sample query

query Chatmessages(...) {
  chatmessages(filter: $filter, limit: $limit, offset: $offset, order: $order) {
    ...Chatmessage
  }
}

The main part displays all messages in an infinite-scrolling method, where the following policy works great.

typePolicies: {
  Query: {
    fields: {
      chatmessages: offsetLimitPagination({keyArgs: ['filter', 'order']}),
    },
  },
},

While the other part displays logs of messages in a pages, where only $limit number of records are shown at a time. I have to use the following policy to make this part work.

typePolicies: {
  Query: {
    fields: {
      chatmessages: { keyArgs: ['filter', 'order', 'limit', 'offset'] },
    },
  },
},

As updateQuery is deprecated, is there a workaround for making both work now in v3?

1reaction
pontusabcommented, Dec 31, 2020

@michel Yeah I solved my issue by:

typePolicies: {
  Project: {
          fields: {
            postsConnection: relayStylePagination(),
          },
        },
 },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the behavior of cached fields - Apollo GraphQL
You provide field policies to the constructor of InMemoryCache . Each field policy is defined inside whichever TypePolicy object corresponds to the field's ......
Read more >
Type Policies in Apollo Client 3: An Introduction - YouTube
What are type policies in Apollo Client 3 and how can we leverage them in our applications? This video explores some of the...
Read more >
Apollo Cache in a Nutshell - E.Y. - Medium
Inside the TypePolicies, you can supply field level policies for each ... Otherwise, the query with the same request data but different ...
Read more >
Advanced Topics on Caching – Angular
Please note that if you call refetchQueries with an array of strings, ... const cache = new InMemoryCache({ typePolicies: { Query: { fields: ......
Read more >
Customizing the behavior of cached fields - Client (React)
Each field policy is defined inside whatever TypePolicy object corresponds ... so that Apollo Client doesn't include it in requests to your GraphQL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found