graphql-fetch.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { type DocumentNode } from "graphql";
  2. import {
  3. type OperationVariables,
  4. ApolloClient
  5. } from "@apollo/client";
  6. import {getClient} from "@/lib/ApolloClientServer";
  7. // Comprehensive error handling example. https://www.apollographql.com/docs/react/data/error-handling
  8. export interface GraphqlRequestResult<TData = unknown> {
  9. data: TData | null;
  10. error: string | undefined;
  11. }
  12. export type CacheLifePreset =
  13. | "seconds"
  14. | "minutes"
  15. | "hours"
  16. | "days"
  17. | "weeks"
  18. | "max";
  19. export type CacheLifeOption = number | CacheLifePreset;
  20. export function getRevalidateTime(
  21. life?: CacheLifeOption
  22. ): number | false {
  23. if (!life) return false;
  24. if (typeof life === "number") return life;
  25. switch (life) {
  26. case "seconds":
  27. return 10;
  28. case "minutes":
  29. return 60;
  30. case "hours":
  31. return 3600;
  32. case "days":
  33. return 86400;
  34. case "weeks":
  35. return 604800;
  36. case "max":
  37. return false;
  38. default:
  39. return false;
  40. }
  41. }
  42. export function stableStringify(value: unknown): string {
  43. if (value === null || typeof value !== "object") {
  44. return JSON.stringify(value);
  45. }
  46. if (Array.isArray(value)) {
  47. return `[${value.map(stableStringify).join(",")}]`;
  48. }
  49. const obj = value as Record<string, unknown>;
  50. return `{${Object.keys(obj)
  51. .sort()
  52. .map(
  53. (key) => `"${key}":${stableStringify(obj[key])}`
  54. )
  55. .join(",")}}`;
  56. }
  57. export interface GraphQLRequestOptions {
  58. tags?: string[];
  59. life?: CacheLifeOption;
  60. noCache?: boolean;
  61. context?: Record<string, unknown>;
  62. fetchPolicy?:
  63. | "cache-first"
  64. | "network-only"
  65. | "no-cache"
  66. | "cache-only";
  67. }
  68. export async function graphqlRequest<
  69. TData = unknown,
  70. TVariables extends OperationVariables = OperationVariables
  71. >(
  72. query: DocumentNode,
  73. variables?: TVariables,
  74. options?: GraphQLRequestOptions
  75. ): Promise<GraphqlRequestResult<TData>> {
  76. const client = getClient();
  77. let resData;
  78. const revalidate = getRevalidateTime(options?.life);
  79. let queryOption: ApolloClient.QueryOptions<TData> = {
  80. query,
  81. variables,
  82. fetchPolicy: "network-only",
  83. context: {
  84. fetchOptions: {
  85. next: {
  86. revalidate,
  87. tags: options?.tags,
  88. },
  89. },
  90. }
  91. };
  92. if (options?.noCache) {
  93. /***
  94. * client.query的参数是一个对象:
  95. * {query, variables, context, fetchPolicy, errorPolicy}
  96. * context.fetchOptions 可以设置nextjs fetch的缓存策略 https://www.apollographql.com/docs/react/integrations/nextjs
  97. * context: {
  98. fetchOptions: {
  99. next: {
  100. revalidate: 60, // 对应 life: 'minutes'
  101. tags: ['posts'], // 对应 tags 选项
  102. },
  103. },
  104. },
  105. */
  106. queryOption = {
  107. query,
  108. variables,
  109. context: options?.context,
  110. fetchPolicy: "no-cache", // 跳过 apollo client 的缓存,直接调fetch
  111. };
  112. }
  113. if (options?.context) {
  114. throw new Error(
  115. "graphqlRequest: Caching with `context` is unsafe. Use noCache instead."
  116. );
  117. }
  118. try {
  119. // Promise-based APIs (e.g. client.query, client.mutate) - Errors either reject the promise or are returned in the result as the error field.
  120. // 如果错误被reject 则会进入catch
  121. const result: ApolloClient.QueryResult<TData> = await client.query(queryOption);
  122. console.log('graphqlRequest network-only result ---- ', result);
  123. resData = result.data || null;
  124. return {data: resData, error: result.error? result.error.message : '' };
  125. } catch (error) {
  126. throw error;
  127. }
  128. }
  129. export async function graphqlRequestNoCache<
  130. TData = unknown,
  131. TVariables extends OperationVariables = OperationVariables
  132. >(
  133. query: DocumentNode,
  134. variables?: TVariables,
  135. options?: Omit<
  136. GraphQLRequestOptions,
  137. "noCache" | "tags" | "life"
  138. >
  139. ): Promise<GraphqlRequestResult<TData>> {
  140. return graphqlRequest<TData, TVariables>(
  141. query,
  142. variables,
  143. {
  144. ...options,
  145. noCache: true,
  146. }
  147. );
  148. }