|
@@ -1,6 +1,6 @@
|
|
|
import { cookies } from "next/headers";
|
|
import { cookies } from "next/headers";
|
|
|
import { auth } from "@/utils/auth/auth-helper";
|
|
import { auth } from "@/utils/auth/auth-helper";
|
|
|
-import { print, type DocumentNode } from "graphql";
|
|
|
|
|
|
|
+import { print, parse, getOperationAST, type DocumentNode } from "graphql";
|
|
|
import {
|
|
import {
|
|
|
GRAPHQL_URL,
|
|
GRAPHQL_URL,
|
|
|
REST_API_URL,
|
|
REST_API_URL,
|
|
@@ -76,16 +76,27 @@ export async function getAuthorizationToken(): Promise<{token: string | null; is
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function getGraphqlOperationName(query: string | DocumentNode) {
|
|
|
|
|
+ const documentAST = typeof query === "string" ? parse(query) : query;
|
|
|
|
|
+ // 不传入 operationName 时,如果文档中只有一个操作,会自动返回该操作
|
|
|
|
|
+ const operation = getOperationAST(documentAST);
|
|
|
|
|
+ const operationName = operation && operation.name
|
|
|
|
|
+ ? operation.name.value
|
|
|
|
|
+ : undefined;
|
|
|
|
|
+
|
|
|
|
|
+ return operationName;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// rest api fetch
|
|
// rest api fetch
|
|
|
export async function restApiFetch<T>({
|
|
export async function restApiFetch<T>({
|
|
|
api,
|
|
api,
|
|
|
method,
|
|
method,
|
|
|
- cache = "force-cache",
|
|
|
|
|
|
|
+ cache = "no-store",
|
|
|
headers,
|
|
headers,
|
|
|
tags,
|
|
tags,
|
|
|
variables,
|
|
variables,
|
|
|
isRoute = true,
|
|
isRoute = true,
|
|
|
- revalidate = 60,
|
|
|
|
|
|
|
+ revalidate = 0,
|
|
|
takeAuthorization = true, // 是否携带token,默认携带
|
|
takeAuthorization = true, // 是否携带token,默认携带
|
|
|
}: {
|
|
}: {
|
|
|
api: string;
|
|
api: string;
|
|
@@ -119,7 +130,10 @@ export async function restApiFetch<T>({
|
|
|
Object.assign(baseHeaders, headers);
|
|
Object.assign(baseHeaders, headers);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- console.log('restApiFetch --- baseHeaders:', baseHeaders)
|
|
|
|
|
|
|
+ console.log('restApiFetch --- baseHeaders:', {
|
|
|
|
|
+ ...baseHeaders,
|
|
|
|
|
+ apiUrl
|
|
|
|
|
+ })
|
|
|
const param: RequestInit = {
|
|
const param: RequestInit = {
|
|
|
method: method,
|
|
method: method,
|
|
|
headers: baseHeaders,
|
|
headers: baseHeaders,
|
|
@@ -144,7 +158,6 @@ export async function restApiFetch<T>({
|
|
|
const result = await fetch(url, param);
|
|
const result = await fetch(url, param);
|
|
|
|
|
|
|
|
const body = await result.json();
|
|
const body = await result.json();
|
|
|
- console.log('restApiFetch --- body:', body)
|
|
|
|
|
if(!isRoute) {
|
|
if(!isRoute) {
|
|
|
if(result.status === 401) {
|
|
if(result.status === 401) {
|
|
|
const err = new Error('Authorization Bearer token is required. Please Login.');//new Error(body.message || 'Authorization Bearer token is required');
|
|
const err = new Error('Authorization Bearer token is required. Please Login.');//new Error(body.message || 'Authorization Bearer token is required');
|
|
@@ -191,6 +204,8 @@ export async function serverGraphqlFetch<
|
|
|
try {
|
|
try {
|
|
|
const queryString = typeof query === "string" ? query : print(query);
|
|
const queryString = typeof query === "string" ? query : print(query);
|
|
|
|
|
|
|
|
|
|
+ const operationName = getGraphqlOperationName(query);
|
|
|
|
|
+
|
|
|
if (headers) {
|
|
if (headers) {
|
|
|
if (headers instanceof Headers) {
|
|
if (headers instanceof Headers) {
|
|
|
headers.forEach((value, key) => (baseHeaders[key] = value));
|
|
headers.forEach((value, key) => (baseHeaders[key] = value));
|
|
@@ -198,7 +213,10 @@ export async function serverGraphqlFetch<
|
|
|
Object.assign(baseHeaders, headers);
|
|
Object.assign(baseHeaders, headers);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- console.log('serverGraphqlFetch --- baseHeaders:', baseHeaders);
|
|
|
|
|
|
|
+ console.log('serverGraphqlFetch --- baseHeaders:', {
|
|
|
|
|
+ ...baseHeaders,
|
|
|
|
|
+ operationName
|
|
|
|
|
+ });
|
|
|
const result = await fetch(GRAPHQL_URL, {
|
|
const result = await fetch(GRAPHQL_URL, {
|
|
|
method: "POST",
|
|
method: "POST",
|
|
|
headers: baseHeaders,
|
|
headers: baseHeaders,
|
|
@@ -266,8 +284,9 @@ export async function bagistoFetch<T>({
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- const queryString =
|
|
|
|
|
- typeof query === "string" ? query : print(query);
|
|
|
|
|
|
|
+ const queryString = typeof query === "string" ? query : print(query);
|
|
|
|
|
+
|
|
|
|
|
+ const operationName = getGraphqlOperationName(query);
|
|
|
|
|
|
|
|
if (headers) {
|
|
if (headers) {
|
|
|
if (headers instanceof Headers) {
|
|
if (headers instanceof Headers) {
|
|
@@ -276,12 +295,12 @@ export async function bagistoFetch<T>({
|
|
|
Object.assign(baseHeaders, headers);
|
|
Object.assign(baseHeaders, headers);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // let cc = await cookies();
|
|
|
|
|
- // console.log('bagistoFetch --- url:', GRAPHQL_URL,isCookies);
|
|
|
|
|
- // console.log('bagistoFetch --- queryString:', queryString);
|
|
|
|
|
- console.log('bagistoFetch --- baseHeaders:', baseHeaders);
|
|
|
|
|
- // console.log('bagistoFetch --- cookies:', cc.getAll());
|
|
|
|
|
- // console.log('bagistoFetch --- variables:', variables);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ console.log('bagistoFetch --- baseHeaders:', {
|
|
|
|
|
+ ...baseHeaders,
|
|
|
|
|
+ operationName
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
const result = await fetch(GRAPHQL_URL, {
|
|
const result = await fetch(GRAPHQL_URL, {
|
|
|
method: "POST",
|
|
method: "POST",
|
|
|
headers: baseHeaders,
|
|
headers: baseHeaders,
|
|
@@ -297,7 +316,6 @@ export async function bagistoFetch<T>({
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const body = await result.json();
|
|
const body = await result.json();
|
|
|
- console.log('bagistoFetch --- body:',body);
|
|
|
|
|
return { status: result.status, body };
|
|
return { status: result.status, body };
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
throw e;
|
|
throw e;
|