浏览代码

删除无用代码

zgl 1 周之前
父节点
当前提交
cc2e55c3d7
共有 4 个文件被更改,包括 1 次插入 和 234 次删除
  1. 1 5
      src/app/(public)/category/[slug]/[id]/page.tsx
  2. 0 130
      src/lib/graphql-fetch.ts
  3. 0 4
      src/types/graphqlFetch/type.ts
  4. 0 95
      src/utils/hooks/useCache.ts

+ 1 - 5
src/app/(public)/category/[slug]/[id]/page.tsx

@@ -17,11 +17,7 @@ import {
   GET_CATEGORY_ATTR_FILTERS,
   CATEGORY_PRODUCTS,
 } from "@/graphql";
-import {
-  // cachedGraphQLRequest,
-  // cachedCategoryRequest,
-  // getFilterAttributes,
-} from "@/utils/hooks/useCache";
+
 import { newSortByFields } from "@utils/constants";
 // import { CategoryDetail } from "@components/theme/search/CategoryDetail";
 import CategoryDesc from "./_components/CategoryDesc";

+ 0 - 130
src/lib/graphql-fetch.ts

@@ -1,130 +0,0 @@
-
-import { type DocumentNode } from "graphql";
-import {
-  type OperationVariables,
-  ApolloClient
-} from "@apollo/client";
-import type {GraphqlRequestResult} from "@/types/graphqlFetch/type";
-import {getClient} from "@/lib/ApolloClientServer";
-
-
-
-/* 定义自己的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 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 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;
-  }
-}
-

+ 0 - 4
src/types/graphqlFetch/type.ts

@@ -1,7 +1,3 @@
-export interface GraphqlRequestResult<TData = unknown> {
-  data: TData | null;
-  error: string | undefined;
-}
 
 export interface FetchGraphqlError {
     message: string;

+ 0 - 95
src/utils/hooks/useCache.ts

@@ -1,95 +0,0 @@
-import { type DocumentNode, type OperationVariables } from "@apollo/client";
-import { graphqlRequest, type CacheLifeOption } from "@/lib/graphql-fetch";
-import type {GraphqlRequestResult} from "@/types/graphqlFetch/type";
-
-
-export interface PageCacheConfig {
-  tags: string[];
-  life: CacheLifeOption;
-}
-
-/**
- * Cache configuration for different pages and queries
- * Centralized management of cache tags and revalidation times
- */
-export const PAGE_CACHE_CONFIG: Record<string, PageCacheConfig> = {
-  // Home page
-  home: {
-    tags: ["home-page"],
-    life: "hours",
-  },
-
-  // Product pages
-  product: {
-    tags: ["all-products"],
-    life: "hours",
-  },
-
-  // Category/Collection pages
-  category: {
-    tags: ["categories"],
-    life: "hours",
-  },
-
-  // Static content
-  static: {
-    tags: ["static-content"],
-    life: "days",
-  },
-
-  // Search results
-  search: {
-    tags: ["search-results"],
-    life: "hours",
-  },
-};
-
-/**
- * Helper to get cache config for a specific page
- */
-export function getPageCacheConfig(
-  page: keyof typeof PAGE_CACHE_CONFIG,
-): PageCacheConfig {
-  return PAGE_CACHE_CONFIG[page];
-}
-
-/**
- * Helper to create dynamic product cache config with specific product identifier
- */
-// export function getProductCacheConfig(productId: string): PageCacheConfig {
-//   return {
-//     tags: ["products", `product-${productId}`],
-//     life: "hours",
-//   };
-// }
-export function getProductCacheConfig(): {noCache: boolean} {
-  return {noCache: true};
-}
-/**
- * Helper to create dynamic category cache config with specific category identifier
- */
-export function getCategoryCacheConfig(categoryId: string): PageCacheConfig {
-  return {
-    tags: ["categories", `category-${categoryId}`],
-    life: "hours",
-  };
-}
-
-
-
-
-/**
- * Wrapper for category-specific queries with dynamic cache tags
- */
-export async function cachedCategoryRequest<
-  TData = unknown,
-  TVariables extends OperationVariables = OperationVariables,
->(
-  categoryId: string,
-  query: DocumentNode,
-  variables?: TVariables,
-): Promise<GraphqlRequestResult<TData>> {
-  const config = getCategoryCacheConfig(categoryId);
-  return graphqlRequest<TData, TVariables>(query, variables, config);
-}
-