【发布时间】:2021-07-10 22:52:03
【问题描述】:
我在apollo-client repo 中发布了这个,但我想我也会问 stackoverflow
你好!我是 apollo(以及整个 graphql)的新手,并且对 SSR / SSG 有一些疑问。我在概念上知道什么是 SSR/SSG 以及它是如何工作的,但对 apollo-client 了解不多。
我已经尝试在网上搜索和搜索正确执行此操作的方法,并且看到两个版本几乎没有解释原因,所以我在这篇文章中的目标是有一个地方指出并去“这就是你这样做的原因一个比一个”。
这样做有什么好处/坏处
这是 TypeScript 和伪代码的混合体,请不要批评语法 kthx
// apolloClient.ts
const client = new ApolloClient({
link: new HttpLink({ uri: '/graphql' }),
cache: new InMemoryCache(),
});
// component.tsx
import client from '../apolloClient';
const Component: FunctionalComponent = ({ data }) => {
...
}
export const getServerSideProps: GetServerSideProps = async () => {
const { data } = client.query(...);
return {
props: { data }
}
}
// app.tsx
import client from './client';
const MyApp: NextComponentType<AppContext, AppInitialProps, AppProps> = ({ Component, pageProps }) => (
<ApolloProvider client={client}>
<Component {...pageProps} />
</ApolloProvider>
);
VS
// apolloClient.ts
const createClient = () => new ApolloClient({
link: new HttpLink({ uri: '/graphql' }),
cache: new InMemoryCache(),
ssrMode: typeof window === 'undefined',
});
let client: ApolloClient<NormalizedCacheObject>;
const initalizeApollo = (initalState?: NormalizedCacheObject) => {
const apolloClient = client ?? createClient();
if (initalState) {
apolloClient.cache.restore({
...apolloClient.cache.extract(),
...initalState,
});
}
if (typeof window === 'undefined') return apolloClient;
client ??= apolloClient;
return client;
}
const useApollo = (initalState?: NormalizedCacheObject) => useMemo(() => initalizeApollo(initalState), [initalState]);
// component.tsx
import { useQuery } from 'apollo-client';
import useApollo from '../apolloClient';
const Component = () => {
const { data } = useQuery(...);
}
export const getServerSideProps: GetServerSideProps = async () => {
const client = useApollo();
await client.query(...);
return {
props: { initalApolloState: client.cache.extract() }
}
}
// app.tsx
const MyApp: NextComponentType<AppContext, AppInitialProps, AppProps> = ({ Component, pageProps }) => {
const client = useApollo(pageProps.initalApolloState);
return (
<ApolloProvider client={client}>
<Component {...pageProps} />
</ApolloProvider>
)
};
这是我自己的“我不明白”部分的讨论
对我来说,似乎是第二种方式(使用 SSR?)您必须运行两次查询并编写大量额外代码才能获得相同的效果?这两种方法的性能/安全性/任何好处是什么。
谢谢!
【问题讨论】:
标签: typescript next.js server-side-rendering apollo-client