如果我最初访问主页或刷新主页,则由于cache.readQuery无法正常工作,所以无法在解析器中获取配置文件。

以及它无限调用API。

如果我移到另一个页面并再次返回主页,则cache.readQuery可以正常工作并正确获取帖子的个人资料和voiceStatus。

有人遇到过这个问题吗? home.tsx是我的项目的主页。

以及useQuery(@ apollo / react-hooks)还会在每个页面上获取旧数据。

如果您有经验,请帮助我。

  • home.tsx
  •     ...
        const GET_POSTS = graphql`
          query posts($accountname: String!, $page: Int, $pathBuilder: any, $postsStatus: String) {
            posts(accountname: $accountname, page: $page, postsStatus: $postsStatus)
              @rest(type: "Post", pathBuilder: $pathBuilder) {
              post_id
              author
              voteStatus(accountname: $accountname) @client
              created_at
            }
          }
        `;
        interface Props {
          author: string;
        }
        const Home: NextPage<Props> = ({ author }) => {
          const { data, fetchMore, loading } = useQuery(GET_POSTS, {
            variables: {
              accountname: author,
              page: 1,
              postsStatus: 'home',
              pathBuilder: () => `posts/?Page=1&Limit=5&domainId=1`,
            },
          });
          const loadMorePosts = () => {
            fetchMore({
              variables: {
                page: page + 1,
                pathBuilder: () => `posts/?Page=${page + 1}&Limit=5&domainId=1`,
              },
              updateQuery: (previousResult, { fetchMoreResult }) => {
                if (!fetchMoreResult) {
                  return previousResult;
                }
                setPage(page + 1);
                return Object.assign({}, previousResult, {
                  posts: [...previousResult.posts, ...fetchMoreResult.posts],
                });
              },
            });
          };
          return (
            <div></div>
          );
        };
        interface Context extends NextPageContext {
          apolloClient: ApolloClient<NormalizedCacheObject>;
        }
        Home.getInitialProps = async (ctx: Context) => {
          const cookies = nextCookie(ctx);
          const author = cookies[encodeURIComponent(KARMA_AUTHOR)];
          ctx.apolloClient.writeData({
            data: {
              accountName: author,
            },
          });
          return {
            layoutConfig: { layout: labels.DEFAULT },
            meta: {
              title: 'Home',
            },
            author,
          };
        };
        export default withAuthSync(withApollo({ ssr: true })(Home));
    

    带有Apollo.tsx的
  •     import { ApolloClient } from 'apollo-client';
        import { withClientState } from 'apollo-link-state';
        import serverFetch from 'node-fetch';
        import graphql from 'graphql-tag';
        const GET_PROFILE = graphql`
          query Profile($accountname: String!, $domainID: number) {
            profile(accountname: $accountname, domainID: $domainID)
              @rest(type: "Profile", path: "profile/{args.accountname}?domainID={args.domainID}") {
              author
              followers_count
              following_count
            }
          }
        `;
        const cache = new InMemoryCache({
          cacheRedirects: {
            Query: {
              post: (_, { post_id }, { getCacheKey }) => getCacheKey({ __typename: 'Post', post_id }),
            },
          },
          dataIdFromObject: object => {
            switch (object.__typename) {
              case 'Post':
                return getUniquePostId(object.post_id);
              case 'Comment':
                return getUniqueCommentId(object.cmmt_id);
              case 'Profile':
                return object.author;
              default:
                defaultDataIdFromObject(object);
            }
          },
        });
        const resolvers = {
          Post: {
            voteStatus: async ({ post_id }, args, { cache }, info) => {
              const { profile } = cache.readQuery({
                query: GET_PROFILE,
                variables: {
                  accountname: args.accountname,
                  domainID: 1,
                },
              });
              console.log(profile); // can't make console log because profile is not coming from readQuery
              if (profile) {
                return 1;
              } else {
                return 0;
              }
            },
          },
        };
        const stateLink = withClientState({
          cache,
          resolvers,
        });
        const restLink = new RestLink({
          uri: `${SERVER_URL}/`,
          serverFetch,
        });
        const createApolloClient = (initialState: NormalizedCacheObject, ctx: NextPageContext) => {
          return new ApolloClient({
            ssrMode: true,
            link: ApolloLink.from([stateLink, restLink]),
            cache,
          });
        }
        ...
        export const withApollo = ({ ssr = false } = {}) => (PageComponent: NextPage) => {
          const client = createApolloClient(initialState, ctx);
          ...
          return {
            ...pageProps,
            apolloState: apolloClient.cache.extract(),
            apolloClient: ctx.apolloClient,
          };
        }
    

    最佳答案

    我没有使用此设置,只是假设您的应用程序可能正在清除缓存,所以当最初尝试在主页上获取该查询时,它会失败。

    同样,我可能是错的,但是根据这些文档:https://www.apollographql.com/docs/react/caching/cache-interaction/#readquery



    在您的readQuery中添加一个try / catch块似乎很有意义

    let profile;
    try {
      result = cache.readQuery({
        query: GET_PROFILE,
        variables: {
          accountname: args.accountname,
          domainID: 1,
        },
      });
      profile = result.profile;
    } catch(err) {
      console.error(err);
      // do something like printing an error in console, or nothing
    }
    

    10-04 13:38