Skip to content

Commit

Permalink
fix typescript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 14, 2024
1 parent 35e8ae4 commit 065d181
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 78 deletions.
6 changes: 2 additions & 4 deletions packages/gateway/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ let cli = new Command()
)
.option(
'--hive-persisted-documents-token <token>',
'[EXPERIMENTAL] Hive persisted documents CDN endpoint. requires the "--hive-persisted-documents-endpoint <endpoint>" option',
'[EXPERIMENTAL] Hive persisted documents CDN endpoint token. requires the "--hive-persisted-documents-endpoint <endpoint>" option',
)
.addOption(
new Option(
Expand Down Expand Up @@ -299,9 +299,7 @@ export async function run(userCtx: Partial<CLIContext>) {
// @ts-ignore bob will complain when bundling for cjs
import.meta.url,
data: {
packedDepsPath:
// WILL BE AVAILABLE IN SEA ENVIRONMENTS (see install-sea-packed-deps.cjs and rollup.binary.config.js)
globalThis.__PACKED_DEPS_PATH__ || '',
packedDepsPath: globalThis.__PACKED_DEPS_PATH__ || '',
} satisfies InitializeData,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/gateway/src/commands/handleFork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Logger } from '@graphql-mesh/types';
import { registerTerminateHandler } from '@graphql-mesh/utils';

export function handleFork(log: Logger, config: { fork?: number }): boolean {
if (cluster.isPrimary && config.fork > 1) {
if (cluster.isPrimary && config.fork && config.fork > 1) {
const workers = new Set<Worker>();
let expectedToExit = false;
log.debug(`Forking ${config.fork} workers`);
Expand Down
52 changes: 29 additions & 23 deletions packages/gateway/src/commands/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cluster, { type Worker } from 'node:cluster';
import cluster from 'node:cluster';
import {
createGatewayRuntime,
type GatewayConfigProxy,
} from '@graphql-mesh/serve-runtime';
import { isUrl, PubSub, registerTerminateHandler } from '@graphql-mesh/utils';
import { isUrl, PubSub } from '@graphql-mesh/utils';
import {
defaultOptions,
type AddCommand,
Expand Down Expand Up @@ -63,7 +63,9 @@ export const addCommand: AddCommand = (ctx, cli) =>
}

let schema: GatewayConfigProxy['schema'] | undefined;
const hiveCdnEndpointOpt = opts.schema || hiveCdnEndpoint;
const hiveCdnEndpointOpt =
// TODO: take schema from optsWithGlobals once https://github.com/commander-js/extra-typings/pull/76 is merged
this.opts().schema || hiveCdnEndpoint;
if (hiveCdnEndpointOpt) {
if (hiveCdnKey) {
if (!isUrl(hiveCdnEndpointOpt)) {
Expand All @@ -79,7 +81,8 @@ export const addCommand: AddCommand = (ctx, cli) =>
key: hiveCdnKey,
};
} else {
schema = opts.schema;
// TODO: take schema from optsWithGlobals once https://github.com/commander-js/extra-typings/pull/76 is merged
schema = this.opts().schema;
}
} else if ('schema' in loadedConfig) {
schema = loadedConfig.schema;
Expand Down Expand Up @@ -115,40 +118,43 @@ export const addCommand: AddCommand = (ctx, cli) =>
}
: {}),
...(polling ? { pollingInterval: polling } : {}),
...(hivePersistedDocumentsEndpoint
? {
persistedDocuments: {
type: 'hive',
endpoint:
hivePersistedDocumentsEndpoint ||
(loadedConfig.persistedDocuments &&
'endpoint' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.endpoint),
token:
hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.token),
},
}
: {}),
proxy,
schema,
logging: loadedConfig.logging ?? ctx.log,
productName: ctx.productName,
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
productLogo: ctx.productLogo,
productLink: ctx.productLink,
...(ctx.productLogo ? { productLogo: ctx.productLogo } : {}),
pubsub,
cache,
plugins(ctx) {
const userPlugins = loadedConfig.plugins?.(ctx) ?? [];
return [...builtinPlugins, ...userPlugins];
},
};
if (hivePersistedDocumentsEndpoint) {
const token =
hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments.token);
if (!token) {
ctx.log.error(
`Hive persisted documents needs a CDN token. Please provide it through the "--hive-persisted-documents-token <token>" option or the config.`,
);
process.exit(1);
}
config.persistedDocuments = {
...loadedConfig.persistedDocuments,
type: 'hive',
endpoint: hivePersistedDocumentsEndpoint,
token,
};
}
if (maskedErrors != null) {
// overwrite masked errors from loaded config only when provided
// @ts-expect-error maskedErrors is a boolean but incorrectly inferred
config.maskedErrors = maskedErrors;
}
if (
Expand All @@ -163,7 +169,7 @@ export const addCommand: AddCommand = (ctx, cli) =>
return runProxy(ctx, config);
});

export type ProxyConfig = GatewayConfigProxy<unknown> & GatewayCLIConfig;
export type ProxyConfig = GatewayConfigProxy & GatewayCLIConfig;

export async function runProxy({ log }: CLIContext, config: ProxyConfig) {
if (handleFork(log, config)) {
Expand Down
43 changes: 23 additions & 20 deletions packages/gateway/src/commands/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cluster, { type Worker } from 'node:cluster';
import cluster from 'node:cluster';
import { lstat } from 'node:fs/promises';
import { isAbsolute, resolve } from 'node:path';
import {
Expand Down Expand Up @@ -79,39 +79,42 @@ export const addCommand: AddCommand = (ctx, cli) =>
}
: {}),
...(polling ? { pollingInterval: polling } : {}),
...(hivePersistedDocumentsEndpoint
? {
persistedDocuments: {
type: 'hive',
endpoint:
hivePersistedDocumentsEndpoint ||
(loadedConfig.persistedDocuments &&
'endpoint' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.endpoint),
token:
hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.token),
},
}
: {}),
subgraph,
logging: loadedConfig.logging ?? ctx.log,
productName: ctx.productName,
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
productLogo: ctx.productLogo,
productLink: ctx.productLink,
...(ctx.productLogo ? { productLogo: ctx.productLogo } : {}),
pubsub,
cache,
plugins(ctx) {
const userPlugins = loadedConfig.plugins?.(ctx) ?? [];
return [...builtinPlugins, ...userPlugins];
},
};
if (hivePersistedDocumentsEndpoint) {
const token =
hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments.token);
if (!token) {
ctx.log.error(
`Hive persisted documents needs a CDN token. Please provide it through the "--hive-persisted-documents-token <token>" option or the config.`,
);
process.exit(1);
}
config.persistedDocuments = {
...loadedConfig.persistedDocuments,
type: 'hive',
endpoint: hivePersistedDocumentsEndpoint,
token,
};
}
if (maskedErrors != null) {
// overwrite masked errors from loaded config only when provided
// @ts-expect-error maskedErrors is a boolean but incorrectly inferred
config.maskedErrors = maskedErrors;
}
if (
Expand All @@ -126,7 +129,7 @@ export const addCommand: AddCommand = (ctx, cli) =>
return runSubgraph(ctx, config);
});

export type SubgraphConfig = GatewayConfigSubgraph<unknown> & GatewayCLIConfig;
export type SubgraphConfig = GatewayConfigSubgraph & GatewayCLIConfig;

export async function runSubgraph({ log }: CLIContext, config: SubgraphConfig) {
let absSchemaPath: string | null = null;
Expand Down
61 changes: 36 additions & 25 deletions packages/gateway/src/commands/supergraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cluster, { type Worker } from 'node:cluster';
import cluster from 'node:cluster';
import { lstat } from 'node:fs/promises';
import { dirname, isAbsolute, resolve } from 'node:path';
import { Option } from '@commander-js/extra-typings';
Expand Down Expand Up @@ -51,11 +51,14 @@ export const addCommand: AddCommand = (ctx, cli) =>
polling,
apolloGraphRef,
apolloKey,
apolloUplink,
hivePersistedDocumentsEndpoint,
hivePersistedDocumentsToken,
...opts
} = this.optsWithGlobals<CLIGlobals>();

// TODO: move to optsWithGlobals once https://github.com/commander-js/extra-typings/pull/76 is merged
const { apolloUplink } = this.opts();

const loadedConfig = await loadConfig({
log: ctx.log,
configPath: opts.configPath,
Expand Down Expand Up @@ -158,6 +161,12 @@ export const addCommand: AddCommand = (ctx, cli) =>
};
} else if (apolloKey) {
ctx.log.info(`Configuring Apollo GraphOS registry reporting`);
if (!apolloGraphRef) {
ctx.log.error(
`Apollo GraphOS requires a graph ref in the format <graph-id>@<graph-variant>. Please provide a valid graph ref.`,
);
process.exit(1);
}
registryConfig = {
reporting: {
type: 'graphos',
Expand All @@ -182,39 +191,42 @@ export const addCommand: AddCommand = (ctx, cli) =>
...opts,
...registryConfig,
...(polling ? { pollingInterval: polling } : {}),
...(hivePersistedDocumentsEndpoint
? {
persistedDocuments: {
type: 'hive',
endpoint:
hivePersistedDocumentsEndpoint ||
(loadedConfig.persistedDocuments &&
'endpoint' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.endpoint),
token:
hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments?.token),
},
}
: {}),
supergraph,
logging: loadedConfig.logging ?? ctx.log,
productName: ctx.productName,
productDescription: ctx.productDescription,
productPackageName: ctx.productPackageName,
productLogo: ctx.productLogo,
productLink: ctx.productLink,
...(ctx.productLogo ? { productLogo: ctx.productLogo } : {}),
pubsub,
cache,
plugins(ctx) {
const userPlugins = loadedConfig.plugins?.(ctx) ?? [];
return [...builtinPlugins, ...userPlugins];
},
};
if (hivePersistedDocumentsEndpoint) {
const token =
hivePersistedDocumentsToken ||
(loadedConfig.persistedDocuments &&
'token' in loadedConfig.persistedDocuments &&
loadedConfig.persistedDocuments.token);
if (!token) {
ctx.log.error(
`Hive persisted documents needs a CDN token. Please provide it through the "--hive-persisted-documents-token <token>" option or the config.`,
);
process.exit(1);
}
config.persistedDocuments = {
...loadedConfig.persistedDocuments,
type: 'hive',
endpoint: hivePersistedDocumentsEndpoint,
token,
};
}
if (maskedErrors != null) {
// overwrite masked errors from loaded config only when provided
// @ts-expect-error maskedErrors is a boolean but incorrectly inferred
config.maskedErrors = maskedErrors;
}
if (
Expand All @@ -229,8 +241,7 @@ export const addCommand: AddCommand = (ctx, cli) =>
return runSupergraph(ctx, config);
});

export type SupergraphConfig = GatewayConfigSupergraph<unknown> &
GatewayCLIConfig;
export type SupergraphConfig = GatewayConfigSupergraph & GatewayCLIConfig;

export async function runSupergraph(
{ log }: CLIContext,
Expand Down Expand Up @@ -263,7 +274,7 @@ export async function runSupergraph(
try {
watcher = await import('@parcel/watcher');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
if (Object(e).code !== 'MODULE_NOT_FOUND') {
log.debug('Problem while importing @parcel/watcher', e);
}
log.warn(
Expand All @@ -288,9 +299,9 @@ export async function runSupergraph(
)
) {
log.info(`${absSchemaPath} changed. Invalidating supergraph...`);
if (config.fork > 1) {
if (config.fork && config.fork > 1) {
for (const workerId in cluster.workers) {
cluster.workers[workerId].send('invalidateUnifiedGraph');
cluster.workers[workerId]!.send('invalidateUnifiedGraph');
}
} else {
runtime.invalidateUnifiedGraph();
Expand Down
9 changes: 5 additions & 4 deletions packages/gateway/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { pathToFileURL } from 'node:url';
import type {
GatewayConfig,
GatewayConfigContext,
GatewayPlugin,
} from '@graphql-mesh/serve-runtime';
import type { KeyValueCache, Logger } from '@graphql-mesh/types';
import type { GatewayCLIBuiltinPluginConfig } from './cli';
import type { ServerConfig } from './server';
import type { GatewayCLIBuiltinPluginConfig } from './cli.js';
import type { ServerConfig } from './server.js';

export const defaultConfigExtensions = [
'.ts',
Expand Down Expand Up @@ -98,7 +99,7 @@ export async function loadConfig<
export async function getBuiltinPluginsFromConfig(
config: GatewayCLIBuiltinPluginConfig,
ctx: { cache: KeyValueCache },
) {
): Promise<GatewayPlugin[]> {
const plugins = [];
if (config.jwt) {
const { useJWT } = await import('@graphql-mesh/plugin-jwt-auth');
Expand All @@ -123,8 +124,8 @@ export async function getBuiltinPluginsFromConfig(
);
plugins.push(
useMeshRateLimit({
cache: ctx.cache,
...config.rateLimiting,
cache: ctx.cache,
}),
);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/gateway/src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export {};

declare global {
/**
* Available ONLY in SEA environment.
* See `scripts/install-sea-packed-deps.cjs` and `rollup.binary.config.js`
*/
var __PACKED_DEPS_PATH__: string | undefined;
/** Gets injected during build by `scripts/inject-version.ts`. */
var __VERSION__: string | undefined;
}
2 changes: 1 addition & 1 deletion packages/gateway/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export async function startServerForRuntime<
log,
host,
port,
sslCredentials,
maxHeaderSize,
...(sslCredentials ? { sslCredentials } : {}),
};

const server = await startNodeHttpServer(runtime, serverOpts);
Expand Down

0 comments on commit 065d181

Please sign in to comment.