2022-04-19 22:50:19 +00:00
|
|
|
import { initUrqlClient, SSRExchange } from "next-urql";
|
2022-04-18 22:12:15 +00:00
|
|
|
import { cacheExchange, dedupExchange, fetchExchange, ssrExchange } from "urql";
|
2022-04-19 22:50:19 +00:00
|
|
|
import customScalarsExchange from "urql-custom-scalars-exchange";
|
2022-04-18 22:12:15 +00:00
|
|
|
|
2022-04-19 22:50:19 +00:00
|
|
|
import schema from "../graphql/introspection.json";
|
2022-04-18 22:12:15 +00:00
|
|
|
import { getBasePath } from "./utils";
|
|
|
|
|
|
|
|
export const graphqlEndpoint = `${getBasePath()}/api/graphql`;
|
|
|
|
|
2022-04-19 22:50:19 +00:00
|
|
|
const scalarsExchange = customScalarsExchange({
|
|
|
|
// Types don't match for some reason.
|
|
|
|
// Related:
|
|
|
|
// - https://github.com/apollographql/apollo-tooling/issues/1491
|
|
|
|
// - https://spectrum.chat/urql/help/schema-property-kind-is-missing-in-type~29c8f416-068c-485a-adf1-935686b99d05
|
|
|
|
schema: schema as any,
|
|
|
|
scalars: {
|
|
|
|
/* not compatible with next.js serialization limitations, unfortunately */
|
|
|
|
// Date(value: number) {
|
|
|
|
// return new Date(value * 1000);
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const getUrqlClientOptions = (ssr: SSRExchange) => ({
|
|
|
|
url: graphqlEndpoint,
|
|
|
|
exchanges: [
|
|
|
|
dedupExchange,
|
|
|
|
scalarsExchange,
|
|
|
|
cacheExchange,
|
|
|
|
ssr,
|
|
|
|
fetchExchange,
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2022-04-18 22:12:15 +00:00
|
|
|
// for getServerSideProps/getStaticProps only
|
|
|
|
export const ssrUrql = () => {
|
|
|
|
const ssrCache = ssrExchange({ isClient: false });
|
2022-04-19 22:50:19 +00:00
|
|
|
const client = initUrqlClient(getUrqlClientOptions(ssrCache), false);
|
2022-05-09 17:37:28 +00:00
|
|
|
if (!client) {
|
|
|
|
throw new Error("Expected non-null client instance from initUrqlClient");
|
|
|
|
}
|
2022-04-18 22:12:15 +00:00
|
|
|
return [ssrCache, client] as const;
|
|
|
|
};
|