Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark createLRUCache as an internal utility #3588

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-jokes-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-yoga': patch
---

Mark `createLRUCache` utility as deprecated, and export it as `_createLRUCache` marking it as an
internal utility
2 changes: 1 addition & 1 deletion packages/graphql-yoga/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export * from './subscription.js';
export * from './types.js';
export { maskError } from './utils/mask-error.js';
export { type OnParamsEventPayload } from './plugins/types.js';
export { createLRUCache } from './utils/create-lru-cache.js';
export { _createLRUCache, createLRUCache } from './utils/create-lru-cache.js';
export { mergeSchemas } from '@graphql-tools/schema';
export {
// Handy type utils
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DocumentNode, GraphQLError, GraphQLSchema, validate, ValidationRule } from 'graphql';
import type { AfterValidateHook } from '@envelop/core';
import { createLRUCache } from '../utils/create-lru-cache.js';
import { _createLRUCache } from '../utils/create-lru-cache.js';
import type { Plugin } from './types.js';

interface Cache<T> {
Expand All @@ -15,13 +15,13 @@ export interface ParserAndValidationCacheOptions {
}

export function useParserAndValidationCache({
documentCache = createLRUCache(),
errorCache = createLRUCache(),
documentCache = _createLRUCache(),
errorCache = _createLRUCache(),
validationCache = true,
}: // eslint-disable-next-line @typescript-eslint/no-empty-object-type
ParserAndValidationCacheOptions): Plugin<{}> {
const validationCacheByRules =
createLRUCache<WeakMap<GraphQLSchema, WeakMap<DocumentNode, GraphQLError[]>>>();
_createLRUCache<WeakMap<GraphQLSchema, WeakMap<DocumentNode, GraphQLError[]>>>();
return {
onParse({ params, setParsedDocument }) {
const strDocument = params.source.toString();
Expand Down
39 changes: 34 additions & 5 deletions packages/graphql-yoga/src/utils/create-lru-cache.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */
import { LRUCache as LRU } from 'lru-cache';

/**
* @deprecated In the next major, `createLRUCache` will be renamed to `_createLRUCache`,
* and the current `createLRUCache` will be removed.
*/
export const createLRUCache = _createLRUCache;

/**
* @deprecated In the next major, `LRUCacheOptions` will be renamed to `_LRUCacheOptions`,
* and the current `LRUCacheOptions` will be removed.
*/
export type LRUCacheOptions = _LRUCacheOptions;

/**
* @deprecated In the next major, `LRUCache` will be renamed to `_LRUCache`,
* and the current `LRUCache` will be removed.
*/
export type LRUCache<T extends {}> = _LRUCache<T>;

const DEFAULT_MAX = 1024;
const DEFAULT_TTL = 3_600_000;

export type LRUCache<T extends {}> = LRU<string, T>;
export interface LRUCacheOptions {
/**
* @internal This is an internal utility, and you should use it with your own risk.
* This utility can have breaking changes in the future.
*/
export type _LRUCache<T extends {}> = LRU<string, T>;
/**
* @internal This is an internal utility, and you should use it with your own risk.
* This utility can have breaking changes in the future.
*/
export interface _LRUCacheOptions {
max?: number;
ttl?: number;
}

export function createLRUCache<T extends {}>({
/**
* @internal This is an internal utility, and you should use it with your own risk.
* This utility can have breaking changes in the future.
*/
export function _createLRUCache<T extends {}>({
max = DEFAULT_MAX,
ttl = DEFAULT_TTL,
}: LRUCacheOptions = {}) {
}: _LRUCacheOptions = {}) {
return new LRU<string, T>({ max, ttl });
}
Loading