feat: 文章详情 添加日期
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
import { useMemo } from "react";
|
||||
import {
|
||||
ApolloClient,
|
||||
ApolloLink,
|
||||
from,
|
||||
HttpLink,
|
||||
InMemoryCache,
|
||||
NormalizedCacheObject,
|
||||
} from "@apollo/client";
|
||||
import { concatPagination } from "@apollo/client/utilities";
|
||||
import { equals, mergeDeepWith } from "ramda";
|
||||
import { clone, equals, mergeDeepWith } from "ramda";
|
||||
import { onError } from "@apollo/client/link/error";
|
||||
import { buildClientSchema, IntrospectionQuery } from "graphql";
|
||||
import { DateTimeResolver } from "graphql-scalars";
|
||||
import introspectionResult from "./graphql.schema.json"; // schema 文件
|
||||
import { withScalars } from "apollo-link-scalars";
|
||||
import superjson from "superjson";
|
||||
|
||||
export const client = new ApolloClient({
|
||||
uri: "/api/graphql",
|
||||
cache: new InMemoryCache(),
|
||||
});
|
||||
const schema = buildClientSchema(
|
||||
introspectionResult as unknown as IntrospectionQuery
|
||||
);
|
||||
|
||||
const typesMap = {
|
||||
DateTime: DateTimeResolver,
|
||||
};
|
||||
|
||||
export const APOLLO_STATE_PROP_NAME = "__APOLLO_STATE__";
|
||||
|
||||
@@ -21,12 +30,10 @@ let apolloClient: ApolloClient<NormalizedCacheObject>;
|
||||
|
||||
function createApolloClient() {
|
||||
const httpLink = new HttpLink({
|
||||
uri:
|
||||
typeof window === "undefined"
|
||||
? process.env.BACKEND_URI
|
||||
: "/api/graphql", // Server URL (must be absolute)
|
||||
credentials: "same-origin", // Additional fetch() options like `credentials` or `headers`
|
||||
});
|
||||
uri:
|
||||
typeof window === "undefined" ? process.env.BACKEND_URI : "/api/graphql", // Server URL (must be absolute)
|
||||
credentials: "same-origin", // Additional fetch() options like `credentials` or `headers`
|
||||
});
|
||||
|
||||
const errorLink = onError(({ graphQLErrors, networkError }) => {
|
||||
if (graphQLErrors)
|
||||
@@ -40,7 +47,11 @@ function createApolloClient() {
|
||||
});
|
||||
return new ApolloClient({
|
||||
ssrMode: typeof window === "undefined",
|
||||
link: from([errorLink, httpLink]),
|
||||
link: from([
|
||||
errorLink,
|
||||
withScalars({ schema, typesMap }) as ApolloLink,
|
||||
httpLink,
|
||||
]),
|
||||
cache: new InMemoryCache({
|
||||
typePolicies: {
|
||||
Query: {
|
||||
@@ -66,10 +77,7 @@ export function initializeApollo(initialState = null) {
|
||||
const data = mergeDeepWith(
|
||||
(a, b) => {
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
return [
|
||||
...a,
|
||||
...b.filter((bi) => a.every((ai) => !equals(ai, bi))),
|
||||
];
|
||||
return [...a, ...b.filter((bi) => a.every((ai) => !equals(ai, bi)))];
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
@@ -91,7 +99,7 @@ export function initializeApollo(initialState = null) {
|
||||
|
||||
export function addApolloState(client, pageProps) {
|
||||
if (pageProps?.props) {
|
||||
pageProps.props[APOLLO_STATE_PROP_NAME] = client.cache.extract();
|
||||
pageProps.props[APOLLO_STATE_PROP_NAME] = clone(client.cache.extract());
|
||||
}
|
||||
|
||||
return pageProps;
|
||||
|
1572
commons/graphql/graphql.schema.json
Normal file
1572
commons/graphql/graphql.schema.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user