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
…568)

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
dotansimha and ardatan authored Jan 30, 2025
1 parent 0bb4f47 commit de83dd2
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/green-gifts-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-hive/gateway': patch
---

Use the same logging instance across different components whenever possible

For example if the log level is set in the configuration, change it immediately for the cache storages etc.
18 changes: 18 additions & 0 deletions .changeset/tame-crabs-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@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
import { defineConfig } from '@graphql-hive/gateway'

export const gatewayConfig = defineConfig({
// ...
cache: (ctx) => {
// Here you may create/retrieve your cache store instance, and return a KeyValueCache instance
}
})
```
16 changes: 14 additions & 2 deletions packages/gateway/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import type { JWTAuthPluginOptions } from '@graphql-mesh/plugin-jwt-auth';
import type { OpenTelemetryMeshPluginOptions } from '@graphql-mesh/plugin-opentelemetry';
import type { PrometheusPluginOptions } from '@graphql-mesh/plugin-prometheus';
import useMeshRateLimit from '@graphql-mesh/plugin-rate-limit';
import type { KeyValueCache, Logger, YamlConfig } from '@graphql-mesh/types';
import type {
KeyValueCache,
Logger,
MeshPubSub,
YamlConfig,
} from '@graphql-mesh/types';
import { DefaultLogger } from '@graphql-mesh/utils';
import parseDuration from 'parse-duration';
import { addCommands } from './commands/index';
Expand Down Expand Up @@ -93,6 +98,12 @@ export interface GatewayCLIProxyConfig
proxy?: GatewayConfigProxy['proxy'];
}

export type KeyValueCacheFactoryFn = (ctx: {
logger: Logger;
pubsub: MeshPubSub;
cwd: string;
}) => KeyValueCache;

export interface GatewayCLIBuiltinPluginConfig {
/**
* Configure JWT Auth
Expand Down Expand Up @@ -129,6 +140,7 @@ export interface GatewayCLIBuiltinPluginConfig {
jit?: boolean;
cache?:
| KeyValueCache
| KeyValueCacheFactoryFn
| GatewayCLILocalforageCacheConfig
| GatewayCLIRedisCacheConfig
| GatewayCLICloudflareKVCacheConfig;
Expand Down Expand Up @@ -326,7 +338,7 @@ let cli = new Command()

export async function run(userCtx: Partial<CLIContext>) {
const ctx: CLIContext = {
log: new DefaultLogger(),
log: userCtx.log || new DefaultLogger(),
productName: 'Hive Gateway',
productDescription: 'Federated GraphQL Gateway',
productPackageName: '@graphql-hive/gateway',
Expand Down
25 changes: 25 additions & 0 deletions packages/gateway/src/commands/handleLoggingOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Logger } from '@graphql-mesh/types';
import { CLIContext, DefaultLogger, LogLevel } from '..';

export function handleLoggingConfig(
loggingConfig: boolean | Logger | LogLevel | undefined,
ctx: CLIContext,
) {
if (typeof loggingConfig === 'object') {
ctx.log = loggingConfig;
} else if (typeof loggingConfig === 'boolean') {
if (!loggingConfig) {
if (ctx.log instanceof DefaultLogger) {
ctx.log.logLevel = LogLevel.silent;
} else {
ctx.log = new DefaultLogger(ctx.log.name, LogLevel.silent);
}
}
} else if (typeof loggingConfig === 'number') {
if (ctx.log instanceof DefaultLogger) {
ctx.log.logLevel = loggingConfig;
} else {
ctx.log = new DefaultLogger(ctx.log.name, loggingConfig);
}
}
}
12 changes: 10 additions & 2 deletions packages/gateway/src/commands/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../config';
import { startServerForRuntime } from '../servers/startServerForRuntime';
import { handleFork } from './handleFork';
import { handleLoggingConfig } from './handleLoggingOption';

export const addCommand: AddCommand = (ctx, cli) =>
cli
Expand Down Expand Up @@ -94,9 +95,14 @@ export const addCommand: AddCommand = (ctx, cli) =>
}

const pubsub = loadedConfig.pubsub || new PubSub();
const cwd = loadedConfig.cwd || process.cwd();
if (loadedConfig.logging != null) {
handleLoggingConfig(loadedConfig.logging, ctx);
}
const cache = await getCacheInstanceFromConfig(loadedConfig, {
pubsub,
logger: ctx.log,
cwd,
});
const builtinPlugins = await getBuiltinPluginsFromConfig(
{
Expand All @@ -106,6 +112,8 @@ export const addCommand: AddCommand = (ctx, cli) =>
{
logger: ctx.log,
cache,
pubsub,
cwd,
},
);

Expand All @@ -124,8 +132,8 @@ export const addCommand: AddCommand = (ctx, cli) =>
}
: {}),
proxy,
...(schema ? { schema } : {}),
logging: loadedConfig.logging ?? ctx.log,
schema,
logging: ctx.log,
productName: ctx.productName,
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
Expand Down
10 changes: 9 additions & 1 deletion packages/gateway/src/commands/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../config';
import { startServerForRuntime } from '../servers/startServerForRuntime';
import { handleFork } from './handleFork';
import { handleLoggingConfig } from './handleLoggingOption';

export const addCommand: AddCommand = (ctx, cli) =>
cli
Expand Down Expand Up @@ -55,9 +56,14 @@ export const addCommand: AddCommand = (ctx, cli) =>
}

const pubsub = loadedConfig.pubsub || new PubSub();
const cwd = loadedConfig.cwd || process.cwd();
if (loadedConfig.logging != null) {
handleLoggingConfig(loadedConfig.logging, ctx);
}
const cache = await getCacheInstanceFromConfig(loadedConfig, {
pubsub,
logger: ctx.log,
cwd,
});
const builtinPlugins = await getBuiltinPluginsFromConfig(
{
Expand All @@ -67,6 +73,8 @@ export const addCommand: AddCommand = (ctx, cli) =>
{
logger: ctx.log,
cache,
pubsub,
cwd,
},
);

Expand All @@ -90,7 +98,7 @@ export const addCommand: AddCommand = (ctx, cli) =>
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
productLink: ctx.productLink,
...(ctx.productLogo ? { productLogo: ctx.productLogo } : {}),
productLogo: ctx.productLogo,
pubsub,
cache,
plugins(ctx) {
Expand Down
12 changes: 10 additions & 2 deletions packages/gateway/src/commands/supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '../config';
import { startServerForRuntime } from '../servers/startServerForRuntime';
import { handleFork } from './handleFork';
import { handleLoggingConfig } from './handleLoggingOption';

export const addCommand: AddCommand = (ctx, cli) =>
cli
Expand Down Expand Up @@ -179,9 +180,14 @@ export const addCommand: AddCommand = (ctx, cli) =>
}

const pubsub = loadedConfig.pubsub || new PubSub();
const cwd = loadedConfig.cwd || process.cwd();
if (loadedConfig.logging != null) {
handleLoggingConfig(loadedConfig.logging, ctx);
}
const cache = await getCacheInstanceFromConfig(loadedConfig, {
pubsub,
logger: ctx.log,
cwd,
});
const builtinPlugins = await getBuiltinPluginsFromConfig(
{
Expand All @@ -191,6 +197,8 @@ export const addCommand: AddCommand = (ctx, cli) =>
{
logger: ctx.log,
cache,
pubsub,
cwd,
},
);

Expand All @@ -201,12 +209,12 @@ export const addCommand: AddCommand = (ctx, cli) =>
pollingInterval: opts.polling,
...registryConfig,
supergraph,
logging: loadedConfig.logging ?? ctx.log,
logging: ctx.log,
productName: ctx.productName,
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
productLink: ctx.productLink,
...(ctx.productLogo ? { productLogo: ctx.productLogo } : {}),
productLogo: ctx.productLogo,
pubsub,
cache,
plugins(ctx) {
Expand Down
16 changes: 12 additions & 4 deletions packages/gateway/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { isAbsolute, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import type {
GatewayConfig,
GatewayConfigContext,
GatewayPlugin,
} from '@graphql-hive/gateway-runtime';
import type { KeyValueCache, Logger } from '@graphql-mesh/types';
import type { KeyValueCache, Logger, MeshPubSub } from '@graphql-mesh/types';
import type { GatewayCLIBuiltinPluginConfig } from './cli';
import type { ServerConfig } from './servers/types';

Expand Down Expand Up @@ -99,7 +98,12 @@ export async function loadConfig<

export async function getBuiltinPluginsFromConfig(
config: GatewayCLIBuiltinPluginConfig,
ctx: { cache: KeyValueCache; logger: Logger },
ctx: {
cache: KeyValueCache;
logger: Logger;
pubsub: MeshPubSub;
cwd: string;
},
) {
const plugins: GatewayPlugin[] = [];
if (config.jwt) {
Expand Down Expand Up @@ -146,8 +150,12 @@ export async function getBuiltinPluginsFromConfig(

export async function getCacheInstanceFromConfig(
config: GatewayCLIBuiltinPluginConfig,
ctx: Pick<GatewayConfigContext, 'logger' | 'pubsub'>,
ctx: { logger: Logger; pubsub: MeshPubSub; cwd: string },
): 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 de83dd2

Please sign in to comment.