我正在试用新的 nextjs 13(所有内容都在 app 目录而不是 pages 目录中)。我必须使用“使用客户端”指令,因为我也在项目中使用 react-redux。
我正在尝试设置和测试 apollo-client 以获取数据。在测试阿波罗时,我按照此处的设置说明进行操作: https://www.apollographql.com/blog/apollo-client/how-to-use-apollo-client-with-next-js-13/ 像这样:
client.js:
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client";
let client = ApolloClient;
export const getClient = () => {
//create a new client if there's no existing one
//or if we are running on the server
if (!client || typeof window === "undefined") {
client = new ApolloClient({
link: new HttpLink({
uri: "https://main--time-pav6zq.apollographos.net/graphql",
}),
cache: new InMemoryCache(),
});
} //end if
return client;
};
还有这个: 应用程序/page.js
"use client";
import { getClient, clientNoCache } from "./client";
import { gql, useQuery } from "@apollo/client";
const query = gql`
query Now {
now(id: "1")
}
`;
export default function Page() {
const client = getClient();
const { loading, data } = useQuery(query, { client });
...etc
但是,我得到这个错误:
TypeError: Cannot read properties of undefined (reading 'watchQuery') 23 | export default function Page() {
24 | const client = getClient();
> 25 | const { loading, data } = useQuery(query, { client });
我完全按照说明做了。怎么了?