【发布时间】:2021-07-20 14:32:06
【问题描述】:
我有一个 nextjs 项目,它使用 apollo graphql 从后端获取数据。我正在尝试使用服务器端渲染来渲染我的页面。但我目前正在使用 graphql apollo react hooks 从后端获取我的数据,react hooks 阻止我在 getServerSideProps 中调用我的后端。
我该如何解决这个问题?
import * as React from "react";
import { useExampleQuery } from "graphql/types";
export const getServerSideProps = async ({ params }) => {
// Here we should probably call graphql function that is now called inside Page
return { props: { hash: params.hash } };
};
const Page = ({ hash }) => {
/*
I am currently calling my graphql query here using apollo react hooks,
that are generated inside graphql/types using @graphql-codegen (typescript)
*/
const { data, loading } = useExampleQuery({
variables: {
hash: hash,
},
});
return loading ? <p>{loading}</p> : <p>{data.example.text}</p>;
};
export default Page;
【问题讨论】:
标签: typescript graphql react-hooks next.js server-side-rendering