33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import ApolloServerPluginResponseCache from "@apollo/server-plugin-response-cache";
|
|
import type { GraphQLRequestContext } from "@apollo/server";
|
|
|
|
/**
|
|
* Response Caching Plugin für Apollo Server
|
|
* Cached GraphQL Responses basierend auf Query und Variablen
|
|
*/
|
|
export const createResponseCachePlugin = () => {
|
|
return ApolloServerPluginResponseCache({
|
|
// Session-ID für User-spezifisches Caching
|
|
sessionId: async (
|
|
requestContext: GraphQLRequestContext<any>
|
|
): Promise<string | null> => {
|
|
// Optional: User-ID aus Headers oder Context
|
|
const userId = requestContext.request.http?.headers.get("x-user-id");
|
|
return userId || null;
|
|
},
|
|
|
|
// Cache nur bei erfolgreichen Queries
|
|
shouldWriteToCache: async (
|
|
requestContext: GraphQLRequestContext<any>
|
|
): Promise<boolean> => {
|
|
const query = requestContext.request.query;
|
|
|
|
if (!query) return false;
|
|
|
|
// Cache nur bestimmte Queries
|
|
const cacheableQueries = ["products", "pageSeo", "navigation", "page"];
|
|
return cacheableQueries.some((q) => query.includes(q));
|
|
},
|
|
});
|
|
};
|