小编ham*_*huh的帖子

useQuery hook (apolloClient) 和 getStaticProps (NextJS) 结果之间的差异

我是 NextJS 和 GraphQL 的新手,我正在使用 MERN 堆栈(NextJS 和 GraphQL)构建一个应用程序。我只需要澄清一下我是使用从查询返回的数据还是从钩子(apollo/Client)getStaticProps(nextJS)返回的数据useQuery

例如:我有一个根据 ID 获取数据的组件,它的名称是 [id].tsx,我知道在 nextJS 中我们用来在getStaticProps生成组件之前获取数据,就像在我的组件中一样

export const getStaticProps: GetStaticProps = async ({ params }) => {

  let oneUserQuery = await client.query({
    query: ONE_USER_QUERY,
    variables: { id: params.id }
  });

  let user = oneUserQuery.data.userInfo;

  return {
    props: {
      user   //we fetched the userInfo and we have access to it in [id].tsx component props
    }
  };
};
Run Code Online (Sandbox Code Playgroud)

在组件本身中,我们可以使用 apollo/client 提供的 useQuery hook 来使用相同的 graphQL 查询来获取数据,如下所示

export default …
Run Code Online (Sandbox Code Playgroud)

express apollo graphql next.js apollo-client

5
推荐指数
1
解决办法
2209
查看次数

标签 统计

apollo ×1

apollo-client ×1

express ×1

graphql ×1

next.js ×1