@graphql-hive/core
Version:
116 lines • 4.58 kB
TypeScript
import type { CompositionFailure } from '@theguild/federation-composition';
import { CircuitBreakerConfiguration } from './circuit-breaker.js';
import type { LegacyLogger } from './types.js';
export type FetchImplementation = typeof globalThis.fetch;
type Service = {
name: string;
url: string;
sdl: string;
};
export type DevFetcherTargetReference = {
byId: string | number;
bySelector?: never;
} | {
byId?: never;
bySelector: {
organizationSlug: string;
projectSlug: string;
targetSlug: string;
};
};
export type HiveDevService = {
name: string;
url: string;
} & ({
/** Read the schema from an SDL file rather than introspecting `url`. */
source: 'file';
/** Path to the service's SDL file. */
schema: string;
} | {
/**
* How to obtain the schema from `url`.
* - `federation` (default): query the federation `_service { sdl }` field.
* - `graphql`: perform standard GraphQL introspection (the `IntrospectionQuery`) and print
* the resulting schema.
*/
source?: 'federation' | 'graphql';
});
type CachedSupergraph = {
services: Service[];
supergraphSdl: string;
};
export interface HiveDevFetcherOptions {
services: HiveDevService[];
remote?: boolean;
registry?: string;
token?: string;
target?: DevFetcherTargetReference | null;
unstable__forceLatest?: boolean;
/** Reported to the registry API when composing remotely. */
version?: string;
logger?: LegacyLogger;
/** Custom fetch implementation used for introspecting services and calling the registry. */
fetch?: FetchImplementation;
/** Base directory used to resolve relative service schema file paths. Defaults to `process.cwd()`. */
cwd?: string;
/** Guards composition so it isn't attempted more frequently than the circuit breaker allows. */
circuitBreaker?: CircuitBreakerConfiguration;
/** Used to avoid recomposing the supergraph when resolved service SDLs are unchanged. */
cache?: {
get(key: string): Promise<CachedSupergraph | undefined> | CachedSupergraph | undefined;
set(key: string, value: CachedSupergraph): Promise<void> | void;
};
}
export declare class LocalSupergraphCompositionError extends Error {
compositionResult: CompositionFailure;
constructor(compositionResult: CompositionFailure);
}
/** The registry API returned a GraphQL/API-level error while composing remotely. */
export declare class SupergraphRegistryApiError extends Error {
}
/** Remote composition finished but produced composition errors. */
export declare class RemoteSupergraphCompositionError extends Error {
errors: Array<{
message: string;
}>;
constructor(errors: Array<{
message: string;
}>);
}
/** Remote composition reported success but did not return a usable supergraph SDL. */
export declare class InvalidSupergraphResultError extends Error {
supergraphSdl: string | null | undefined;
constructor(supergraphSdl: string | null | undefined);
}
export declare function composeSupergraphLocally(services: Service[]): Promise<string>;
export declare function composeSupergraphRemotely(input: {
services: Service[];
registry: string;
token: string;
unstable__forceLatest: boolean;
target: DevFetcherTargetReference | null;
version: string;
logger?: LegacyLogger;
fetch?: FetchImplementation;
}): Promise<string>;
export type HiveDevFetcher = {
/** Resolve the configured services and return the (possibly cached) composed supergraph SDL. */
fetch(): Promise<string>;
/** Dispose the fetcher and cleanup existing timers (e.g. used for circuit breaker) */
dispose(): void;
};
/**
* Create a fetcher that can get subgraph definitions from a local file, graphql introspection,
* or federated introspection (default), and then compose these services with the latest schema
* stored in Hive with these subgraphs replaced (based on service name).
*
* This is an alternative to using `@graphql-hive/cli`'s dev command.
*
* The composed supergraph is cached and is only recomposed if the provided service SDLs change. But
* introspection and file reading is ran on every call, so if using Hive Gateway's polling interval,
* set the interval accordingly. Composition is also CircuitBreaked, so that the expensive composition
* request is guaranteed not to run too frequently.
*/
export declare function createDevFetcher(options: HiveDevFetcherOptions): HiveDevFetcher;
export {};
//# sourceMappingURL=dev-fetcher.d.ts.map