Skip to content

Commit

Permalink
improvement(cli): allow to pass a factory function to cache field
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Jan 30, 2025
1 parent 0bb4f47 commit 5ee0f96
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .changeset/tame-crabs-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@graphql-hive/gateway': minor
---

Improve `cache` configuration signature.

The `cache` configuration key now allow you to pass a custom factory function to get the cache instance:

```ts

```

In addition, the `context` exposed to cache functions now include the complete context of the gateway.
5 changes: 5 additions & 0 deletions packages/gateway/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export interface GatewayCLIProxyConfig
proxy?: GatewayConfigProxy['proxy'];
}

export type KeyValueCacheFactoryFn = (
ctx: GatewayConfigContext,
) => KeyValueCache;

export interface GatewayCLIBuiltinPluginConfig {
/**
* Configure JWT Auth
Expand Down Expand Up @@ -129,6 +133,7 @@ export interface GatewayCLIBuiltinPluginConfig {
jit?: boolean;
cache?:
| KeyValueCache
| KeyValueCacheFactoryFn
| GatewayCLILocalforageCacheConfig
| GatewayCLIRedisCacheConfig
| GatewayCLICloudflareKVCacheConfig;
Expand Down
6 changes: 5 additions & 1 deletion packages/gateway/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ export async function getBuiltinPluginsFromConfig(

export async function getCacheInstanceFromConfig(
config: GatewayCLIBuiltinPluginConfig,
ctx: Pick<GatewayConfigContext, 'logger' | 'pubsub'>,
ctx: GatewayConfigContext,
): Promise<KeyValueCache> {
if (typeof config.cache === 'function') {
return config.cache(ctx);
}

if (config.cache && 'type' in config.cache) {
switch (config.cache.type) {
case 'redis': {
Expand Down

0 comments on commit 5ee0f96

Please sign in to comment.