| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import { cookies } from 'next/headers'
- import { type DocumentNode } from "graphql";
- import {
- type OperationVariables,
- ApolloClient
- } from "@apollo/client";
- import {getClient} from "@/lib/ApolloClientServer";
- import {IS_GUEST,GUEST_CART_TOKEN} from "@/utils/constants";
- import { decodeJWT } from "@/utils/jwt-cookie";
- /* 定义自己的context类型
- import "@apollo/client";
- import { HttpLink } from "@apollo/client";
- declare module "@apollo/client" {
- interface DefaultContext extends HttpLink.ContextOptions {}
- }
- */
- // Comprehensive error handling example. https://www.apollographql.com/docs/react/data/error-handling
- export interface GraphqlRequestResult<TData = unknown> {
- data: TData | null;
- error: string | undefined;
- }
- export type CacheLifePreset =
- | "seconds"
- | "minutes"
- | "hours"
- | "days"
- | "weeks"
- | "max";
- export type CacheLifeOption = number | CacheLifePreset;
- export function getRevalidateTime(
- life?: CacheLifeOption
- ): number | false {
- if (!life) return false;
- if (typeof life === "number") return life;
- switch (life) {
- case "seconds":
- return 10;
- case "minutes":
- return 60;
- case "hours":
- return 3600;
- case "days":
- return 86400;
- case "weeks":
- return 604800;
- case "max":
- return false;
- default:
- return false;
- }
- }
- export function stableStringify(value: unknown): string {
- if (value === null || typeof value !== "object") {
- return JSON.stringify(value);
- }
- if (Array.isArray(value)) {
- return `[${value.map(stableStringify).join(",")}]`;
- }
- const obj = value as Record<string, unknown>;
- return `{${Object.keys(obj)
- .sort()
- .map(
- (key) => `"${key}":${stableStringify(obj[key])}`
- )
- .join(",")}}`;
- }
- export interface GraphQLRequestOptions {
- tags?: string[];
- life?: CacheLifeOption;
- noCache?: boolean;
- context?: Record<string, unknown>;
- fetchPolicy?:
- | "cache-first"
- | "network-only"
- | "no-cache"
- | "cache-only";
- }
- export async function graphqlRequest<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables
- >(
- query: DocumentNode,
- variables?: TVariables,
- options?: GraphQLRequestOptions
- ): Promise<GraphqlRequestResult<TData>> {
- const client = getClient();
- let resData;
- const revalidate = getRevalidateTime(options?.life);
- let queryOption: ApolloClient.QueryOptions<TData> = {
- query,
- variables,
- fetchPolicy: "network-only",
- context: {
- fetchOptions: {
- next: {
- revalidate,
- tags: options?.tags,
- },
- },
- }
- };
- if (options?.noCache) {
- /***
- * client.query的参数是一个对象:
- * {query, variables, context, fetchPolicy, errorPolicy}
- * context.fetchOptions 可以设置nextjs fetch的缓存策略 https://www.apollographql.com/docs/react/integrations/nextjs
- * context: {
- fetchOptions: {
- next: {
- revalidate: 60, // 对应 life: 'minutes'
- tags: ['posts'], // 对应 tags 选项
- },
- },
- },
- */
- queryOption = {
- query,
- variables,
- context: options?.context,
- fetchPolicy: "no-cache", // 跳过 apollo client 的缓存,直接调fetch
- };
- }
- if (options?.context) {
- throw new Error(
- "graphqlRequest: Caching with `context` is unsafe. Use noCache instead."
- );
- }
- try {
- // Promise-based APIs (e.g. client.query, client.mutate) - Errors either reject the promise or are returned in the result as the error field.
- // 如果错误被reject 则会进入catch
- const result: ApolloClient.QueryResult<TData> = await client.query(queryOption);
- resData = result.data || null;
- return {data: resData, error: result.error? result.error.message : '' };
- } catch (error) {
- throw error;
- }
- }
- export async function graphqlRequestNoCache<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables
- >(
- query: DocumentNode,
- variables?: TVariables,
- options?: Omit<
- GraphQLRequestOptions,
- "noCache" | "tags" | "life"
- >
- ): Promise<GraphqlRequestResult<TData>> {
- return graphqlRequest<TData, TVariables>(
- query,
- variables,
- {
- ...options,
- noCache: true,
- }
- );
- }
- export async function authorizationGraphqlRequest<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables
- >(
- query: DocumentNode,
- variables?: TVariables,
- ): Promise<GraphqlRequestResult<TData>> {
- const cookieStore = await cookies();
- const isGuest = cookieStore.get(IS_GUEST);
- const authToken = cookieStore.get(GUEST_CART_TOKEN);
- let token = '';
- if(!authToken) {
- return {data: null, error: 'Authorization token not found!'};
- }
- if(isGuest?.value === 'false') { // 登录用户
- token = authToken.value;
- } else {
- // 游客
- const jwtRes = decodeJWT<{
- sessionToken: string;
- cartId: number;
- isGuest: boolean;
- }>(authToken.value, true);
- token = jwtRes?.sessionToken || '';
- }
- const client = getClient();
- try {
- const queryOption: ApolloClient.QueryOptions<TData> = {
- query,
- variables,
- fetchPolicy: "no-cache",
- context: {
- headers: {
- "Authorization": "Bearer " + token,
- },
- fetchOptions: {
- cache: 'no-store',
- },
- }
- };
- const result: ApolloClient.QueryResult<TData> = await client.query(queryOption);
- console.log('authorizationGraphqlRequest result ---- ', result);
- const resData = result.data || null;
- return {data: resData, error: result.error? result.error.message : '' };
- } catch (error) {
- throw error;
- }
-
- }
- export async function fullCacheGraphqlRequest<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables
- >(
- query: DocumentNode,
- variables?: TVariables,
- ): Promise<GraphqlRequestResult<TData>> {
-
- const client = getClient();
- try {
- const queryOption: ApolloClient.QueryOptions<TData> = {
- query,
- variables,
- fetchPolicy: "cache-first",
- context: {
- fetchOptions: {
- cache: 'force-cache',
- },
- }
- };
- const result: ApolloClient.QueryResult<TData> = await client.query(queryOption);
- console.log('authorizationGraphqlRequest result ---- ', result);
- const resData = result.data || null;
- return {data: resData, error: result.error? result.error.message : '' };
- } catch (error) {
- throw error;
- }
-
- }
|