@powerhousedao/reactor-api 6.2.0-dev.9 → 6.2.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{adapter-gateway-apollo-IZuGzFaY.mjs → adapter-gateway-apollo-CxV_hMTv.mjs} +27 -5
- package/dist/adapter-gateway-apollo-CxV_hMTv.mjs.map +1 -0
- package/dist/index.d.mts +134 -269
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +619 -687
- package/dist/index.mjs.map +1 -1
- package/dist/src/packages/vite-loader.mjs +3 -3
- package/dist/src/packages/vite-loader.mjs.map +1 -1
- package/dist/{utils-CtC8sjRo.mjs → utils-iibOQ50e.mjs} +216 -50
- package/dist/utils-iibOQ50e.mjs.map +1 -0
- package/package.json +28 -16
- package/dist/adapter-gateway-apollo-IZuGzFaY.mjs.map +0 -1
- package/dist/utils-CtC8sjRo.mjs.map +0 -1
|
@@ -1,12 +1,34 @@
|
|
|
1
1
|
|
|
2
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="75bd4780-abf6-5deb-836d-0da65e8050b8")}catch(e){}}();
|
|
3
3
|
import { t as useServer } from "./websocket-DncAB9XK.mjs";
|
|
4
|
+
import { buildSubgraphSchema } from "@apollo/subgraph";
|
|
4
5
|
import { ApolloGateway, LocalCompose, RemoteGraphQLDataSource } from "@apollo/gateway";
|
|
5
6
|
import { ApolloServer, HeaderMap } from "@apollo/server";
|
|
6
7
|
import { ApolloServerPluginInlineTraceDisabled } from "@apollo/server/plugin/disabled";
|
|
7
8
|
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
|
|
8
9
|
import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default";
|
|
9
10
|
//#region src/graphql/gateway/adapter-gateway-apollo.ts
|
|
11
|
+
/**
|
|
12
|
+
* Drop subgraphs whose typeDefs cannot be built into a valid subgraph schema in
|
|
13
|
+
* isolation — e.g. a duplicate type definition within a single subgraph (Sentry
|
|
14
|
+
* #917) — so one broken subgraph is excluded instead of failing the build for
|
|
15
|
+
* everyone. Each excluded subgraph is logged; the rest are composed.
|
|
16
|
+
*
|
|
17
|
+
* Scope: this only catches per-subgraph build failures. Errors that surface only
|
|
18
|
+
* when subgraphs are composed together (cross-subgraph type or `@key` conflicts)
|
|
19
|
+
* are still thrown by `LocalCompose.initialize()` and are NOT handled here.
|
|
20
|
+
*/
|
|
21
|
+
function filterComposableSubgraphs(serviceList, logger) {
|
|
22
|
+
return serviceList.filter((service) => {
|
|
23
|
+
try {
|
|
24
|
+
buildSubgraphSchema({ typeDefs: service.typeDefs });
|
|
25
|
+
return true;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
logger?.error(`Excluding subgraph "${service.name}" from supergraph composition: ${error instanceof Error ? error.message : String(error)}`);
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
10
32
|
var AuthenticatedDataSource = class extends RemoteGraphQLDataSource {
|
|
11
33
|
willSendRequest(options) {
|
|
12
34
|
const { authorization } = options.context.headers;
|
|
@@ -65,11 +87,11 @@ var ApolloGatewayAdapter = class {
|
|
|
65
87
|
}
|
|
66
88
|
async #buildSupergraphSdl() {
|
|
67
89
|
if (!this.#gatewayOptions || !this.#getSubgraphs) throw new Error("Gateway is not ready");
|
|
68
|
-
return new LocalCompose({ localServiceList: this.#getSubgraphs().map((s) => ({
|
|
90
|
+
return new LocalCompose({ localServiceList: filterComposableSubgraphs(this.#getSubgraphs().map((s) => ({
|
|
69
91
|
name: s.name,
|
|
70
92
|
typeDefs: s.typeDefs,
|
|
71
93
|
url: s.url
|
|
72
|
-
})) }).initialize(this.#gatewayOptions);
|
|
94
|
+
})), this.#logger) }).initialize(this.#gatewayOptions);
|
|
73
95
|
}
|
|
74
96
|
attachWebSocket(wsServer, schema, contextFactory) {
|
|
75
97
|
return useServer({
|
|
@@ -130,5 +152,5 @@ function createApolloFetchHandler(server, contextFactory) {
|
|
|
130
152
|
//#endregion
|
|
131
153
|
export { ApolloGatewayAdapter };
|
|
132
154
|
|
|
133
|
-
//# sourceMappingURL=adapter-gateway-apollo-
|
|
134
|
-
//# debugId=
|
|
155
|
+
//# sourceMappingURL=adapter-gateway-apollo-CxV_hMTv.mjs.map
|
|
156
|
+
//# debugId=75bd4780-abf6-5deb-836d-0da65e8050b8
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-gateway-apollo-CxV_hMTv.mjs","sources":["../src/graphql/gateway/adapter-gateway-apollo.ts"],"sourcesContent":["import type {\n GetDataSourceFunction,\n GraphQLDataSourceProcessOptions,\n ServiceDefinition,\n SubgraphHealthCheckFunction,\n SupergraphSdlUpdateFunction,\n} from \"@apollo/gateway\";\nimport {\n ApolloGateway,\n LocalCompose,\n RemoteGraphQLDataSource,\n} from \"@apollo/gateway\";\nimport { ApolloServer, HeaderMap } from \"@apollo/server\";\nimport { ApolloServerPluginInlineTraceDisabled } from \"@apollo/server/plugin/disabled\";\nimport { ApolloServerPluginDrainHttpServer } from \"@apollo/server/plugin/drainHttpServer\";\nimport { ApolloServerPluginLandingPageLocalDefault } from \"@apollo/server/plugin/landingPage/default\";\nimport { buildSubgraphSchema } from \"@apollo/subgraph\";\nimport type { ILogger } from \"document-model\";\nimport type { GraphQLSchema } from \"graphql\";\nimport type http from \"node:http\";\nimport type { WebSocketServer } from \"ws\";\nimport type { Context } from \"../types.js\";\nimport { useServer } from \"../websocket.js\";\nimport type {\n FetchHandler,\n GatewayContextFactory,\n IGatewayAdapter,\n SubgraphDefinition,\n WsContextFactory,\n WsDisposer,\n} from \"./types.js\";\n\n/**\n * Drop subgraphs whose typeDefs cannot be built into a valid subgraph schema in\n * isolation — e.g. a duplicate type definition within a single subgraph (Sentry\n * #917) — so one broken subgraph is excluded instead of failing the build for\n * everyone. Each excluded subgraph is logged; the rest are composed.\n *\n * Scope: this only catches per-subgraph build failures. Errors that surface only\n * when subgraphs are composed together (cross-subgraph type or `@key` conflicts)\n * are still thrown by `LocalCompose.initialize()` and are NOT handled here.\n */\nexport function filterComposableSubgraphs(\n serviceList: ServiceDefinition[],\n logger?: Pick<ILogger, \"error\">,\n): ServiceDefinition[] {\n return serviceList.filter((service) => {\n try {\n buildSubgraphSchema({ typeDefs: service.typeDefs });\n return true;\n } catch (error) {\n logger?.error(\n `Excluding subgraph \"${service.name}\" from supergraph composition: ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n return false;\n }\n });\n}\n\n// Forwards the incoming authorization header to federated subgraph requests.\nclass AuthenticatedDataSource extends RemoteGraphQLDataSource {\n willSendRequest(options: GraphQLDataSourceProcessOptions) {\n const { authorization } = options.context.headers as {\n authorization: string;\n };\n if (authorization && options?.request.http) {\n options.request.http.headers.set(\"authorization\", authorization);\n }\n }\n}\n\nexport class ApolloGatewayAdapter implements IGatewayAdapter<Context> {\n readonly #logger: ILogger;\n readonly #servers: ApolloServer<Context>[] = [];\n\n #supergraphServer: ApolloServer<Context> | null = null;\n #gatewayOptions: {\n update: SupergraphSdlUpdateFunction;\n healthCheck: SubgraphHealthCheckFunction;\n getDataSource: GetDataSourceFunction;\n } | null = null;\n #getSubgraphs: (() => SubgraphDefinition[]) | null = null;\n\n constructor(logger: ILogger) {\n this.#logger = logger;\n }\n\n async start(_httpServer: http.Server): Promise<void> {\n // Per-subgraph Apollo servers start lazily in createHandler.\n // Nothing to do here.\n }\n\n async createHandler(\n schema: GraphQLSchema,\n contextFactory: GatewayContextFactory<Context>,\n ): Promise<FetchHandler> {\n const server = new ApolloServer<Context>({\n schema,\n logger: this.#logger,\n introspection: true,\n // Each ApolloServer otherwise registers its own SIGINT/SIGTERM handler\n // that re-emits the signal via process.kill after server.stop() resolves.\n // With many subgraph servers that re-emit lands while the reactor's\n // graceful-shutdown chain is mid-flight, looking like a \"second SIGINT\"\n // to the reactor and aborting cleanup. The reactor drives our shutdown\n // end-to-end (see ReactorBuilder.attachSignalHandlers), so opt out.\n stopOnTerminationSignals: false,\n plugins: [\n ApolloServerPluginInlineTraceDisabled(),\n ApolloServerPluginLandingPageLocalDefault(),\n ],\n });\n await server.start();\n this.#servers.push(server);\n return createApolloFetchHandler(server, contextFactory);\n }\n\n async createSupergraphHandler(\n getSubgraphs: () => SubgraphDefinition[],\n httpServer: http.Server,\n contextFactory: GatewayContextFactory<Context>,\n ): Promise<FetchHandler> {\n if (this.#supergraphServer) {\n throw new Error(\"Supergraph server is already running\");\n }\n\n this.#getSubgraphs = getSubgraphs;\n\n const gateway = new ApolloGateway({\n supergraphSdl: async (options) => {\n this.#gatewayOptions = options;\n return await this.#buildSupergraphSdl();\n },\n buildService: (serviceConfig) =>\n new AuthenticatedDataSource(serviceConfig),\n });\n\n this.#supergraphServer = new ApolloServer<Context>({\n gateway,\n logger: this.#logger,\n introspection: true,\n stopOnTerminationSignals: false,\n plugins: [\n ApolloServerPluginDrainHttpServer({ httpServer }),\n ApolloServerPluginInlineTraceDisabled(),\n ApolloServerPluginLandingPageLocalDefault(),\n ],\n });\n\n await this.#supergraphServer.start();\n return createApolloFetchHandler(this.#supergraphServer, contextFactory);\n }\n\n async updateSupergraph(): Promise<void> {\n if (!this.#gatewayOptions || !this.#getSubgraphs) {\n // Not yet initialized - no-op.\n return;\n }\n const { supergraphSdl } = await this.#buildSupergraphSdl();\n this.#gatewayOptions.update(supergraphSdl);\n }\n\n async #buildSupergraphSdl() {\n if (!this.#gatewayOptions || !this.#getSubgraphs) {\n throw new Error(\"Gateway is not ready\");\n }\n const serviceList: ServiceDefinition[] = this.#getSubgraphs().map((s) => ({\n name: s.name,\n typeDefs: s.typeDefs,\n url: s.url,\n }));\n const composableServiceList = filterComposableSubgraphs(\n serviceList,\n this.#logger,\n );\n const localCompose = new LocalCompose({\n localServiceList: composableServiceList,\n });\n return localCompose.initialize(this.#gatewayOptions);\n }\n\n attachWebSocket(\n wsServer: WebSocketServer,\n schema: GraphQLSchema,\n contextFactory: WsContextFactory<Context>,\n ): WsDisposer {\n return useServer(\n {\n schema,\n context: async (ctx: {\n connectionParams?: Record<string, unknown>;\n }) => {\n const connectionParams = (ctx.connectionParams ?? {}) as Record<\n string,\n unknown\n >;\n return contextFactory(connectionParams);\n },\n },\n wsServer,\n );\n }\n\n async stop(): Promise<void> {\n await Promise.all(this.#servers.map((s) => s.stop()));\n this.#servers.length = 0;\n\n if (this.#supergraphServer) {\n await this.#supergraphServer.stop();\n this.#supergraphServer = null;\n }\n this.#gatewayOptions = null;\n this.#getSubgraphs = null;\n }\n}\n\n/**\n * Wrap an existing ApolloServer as a Fetch API handler.\n * Does not manage server lifecycle; caller is responsible for start/stop.\n */\nexport function createApolloFetchHandler(\n server: ApolloServer<Context>,\n contextFactory: GatewayContextFactory<Context>,\n): FetchHandler {\n return async (request: Request): Promise<Response> => {\n const url = new URL(request.url);\n const headers = new HeaderMap();\n request.headers.forEach((value, key) => headers.set(key, value));\n\n let body: unknown = undefined;\n if (request.method !== \"GET\" && request.method !== \"HEAD\") {\n try {\n body = (await request.json()) as unknown;\n } catch {\n // body may be empty for some requests\n }\n }\n\n const result = await server.executeHTTPGraphQLRequest({\n httpGraphQLRequest: {\n method: request.method.toUpperCase(),\n headers,\n search: url.search,\n body,\n },\n context: () => contextFactory(request),\n });\n\n const responseHeaders = new Headers();\n for (const [key, value] of result.headers) {\n responseHeaders.set(key, value);\n }\n\n let responseBody: string;\n if (result.body.kind === \"complete\") {\n responseBody = result.body.string;\n } else {\n const chunks: string[] = [];\n for await (const chunk of result.body.asyncIterator) {\n chunks.push(chunk);\n }\n responseBody = chunks.join(\"\");\n }\n\n return new Response(responseBody, {\n status: result.status ?? 200,\n headers: responseHeaders,\n });\n };\n}\n"],"names":["#logger","#servers","#supergraphServer","#getSubgraphs","#gatewayOptions","#buildSupergraphSdl"],"mappings":";;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,0BACd,aACA,QACqB;AACrB,QAAO,YAAY,QAAQ,YAAY;AACrC,MAAI;AACF,uBAAoB,EAAE,UAAU,QAAQ,UAAU,CAAC;AACnD,UAAO;WACA,OAAO;AACd,WAAQ,MACN,uBAAuB,QAAQ,KAAK,iCAClC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GAEzD;AACD,UAAO;;GAET;;AAIJ,IAAM,0BAAN,cAAsC,wBAAwB;CAC5D,gBAAgB,SAA0C;EACxD,MAAM,EAAE,kBAAkB,QAAQ,QAAQ;AAG1C,MAAI,iBAAiB,SAAS,QAAQ,KACpC,SAAQ,QAAQ,KAAK,QAAQ,IAAI,iBAAiB,cAAc;;;AAKtE,IAAa,uBAAb,MAAsE;CACpE;CACA,WAA6C,EAAE;CAE/C,oBAAkD;CAClD,kBAIW;CACX,gBAAqD;CAErD,YAAY,QAAiB;AAC3B,QAAA,SAAe;;CAGjB,MAAM,MAAM,aAAyC;CAKrD,MAAM,cACJ,QACA,gBACuB;EACvB,MAAM,SAAS,IAAI,aAAsB;GACvC;GACA,QAAQ,MAAA;GACR,eAAe;GAOf,0BAA0B;GAC1B,SAAS,CACP,uCAAuC,EACvC,2CAA2C,CAC5C;GACF,CAAC;AACF,QAAM,OAAO,OAAO;AACpB,QAAA,QAAc,KAAK,OAAO;AAC1B,SAAO,yBAAyB,QAAQ,eAAe;;CAGzD,MAAM,wBACJ,cACA,YACA,gBACuB;AACvB,MAAI,MAAA,iBACF,OAAM,IAAI,MAAM,uCAAuC;AAGzD,QAAA,eAAqB;AAWrB,QAAA,mBAAyB,IAAI,aAAsB;GACjD,SAVc,IAAI,cAAc;IAChC,eAAe,OAAO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