@powerhousedao/reactor-api 1.29.17-dev.3 → 1.29.17-dev.5

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.
@@ -0,0 +1,4 @@
1
+ export declare const config: {
2
+ basePath: string;
3
+ };
4
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM;;CAElB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export const config = {
2
+ basePath: process.env.BASE_PATH || "/",
3
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/base/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAG5C,qBAAa,QAAS,YAAW,SAAS;IACxC,IAAI,SAAa;IACjB,IAAI,SAAM;IACV,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAI5B;IACF,QAAQ,EAAE,YAAY,CAIpB;IACF,OAAO,EAAE,oBAAoB,CAAC;IAC9B,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,EAAE,CAAC;gBACT,IAAI,EAAE,YAAY;IAKxB,OAAO;CAGd"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/base/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAG5C,qBAAa,QAAS,YAAW,SAAS;IACxC,IAAI,SAAa;IACjB,IAAI,SAAM;IACV,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAI5B;IACF,QAAQ,EAAE,YAAY,CAIpB;IACF,OAAO,EAAE,oBAAoB,CAAC;IAC9B,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,EAAE,CAAC;gBACT,IAAI,EAAE,YAAY;IAMxB,OAAO;CAGd"}
@@ -19,6 +19,7 @@ export class Subgraph {
19
19
  this.reactor = args.reactor;
20
20
  this.graphqlManager = args.graphqlManager;
21
21
  this.operationalStore = args.operationalStore;
22
+ this.path = args.path ?? "";
22
23
  }
23
24
  async onSetup() {
24
25
  // noop
@@ -15,7 +15,7 @@ export declare class GraphQLManager {
15
15
  private contextFields;
16
16
  constructor(path: string, app: express.Express, reactor: IDocumentDriveServer, operationalStore: Db, analyticsStore: IAnalyticsStore, subgraphs?: Map<string, Subgraph[]>);
17
17
  init(): Promise<void>;
18
- registerSubgraph(subgraph: SubgraphClass, supergraph?: string, path?: string): Promise<void>;
18
+ registerSubgraph(subgraph: SubgraphClass, supergraph?: string): Promise<void>;
19
19
  updateRouter(): Promise<void>;
20
20
  getAdditionalContextFields: () => Record<string, any>;
21
21
  setAdditionalContextFields(fields: Record<string, any>): void;
@@ -1 +1 @@
1
- {"version":3,"file":"graphql-manager.d.ts","sourceRoot":"","sources":["../../../src/graphql/graphql-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AAOpC,OAAO,EA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AK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAOnC,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG/D,qBAAa,cAAc;;IAKvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAT5B,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,aAAa,CAA2B;gBAG7B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,OAAO,CAAC,OAAO,EACpB,OAAO,EAAE,oBAAoB,EAC7B,gBAAgB,EAAE,EAAE,EACpB,cAAc,EAAE,eAAe,EAC/B,SAAS,GAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAa;IAK3D,IAAI;IA2BJ,gBAAgB,CAAC,QAAQ,EAAE,aAAa,EAAE,UAAU,SAAK;IA6BzD,YAAY;IASlB,0BAA0B,4BAExB;IAEF,0BAA0B,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAItD,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE;CAiJxD"}
@@ -7,6 +7,7 @@ import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin
7
7
  import bodyParser from "body-parser";
8
8
  import cors from "cors";
9
9
  import { Router } from "express";
10
+ import path from "node:path";
10
11
  import { AnalyticsSubgraph } from "./analytics/index.js";
11
12
  import { AuthSubgraph } from "./auth/index.js";
12
13
  import { DriveSubgraph } from "./drive/index.js";
@@ -27,15 +28,16 @@ export class GraphQLManager {
27
28
  this.operationalStore = operationalStore;
28
29
  this.analyticsStore = analyticsStore;
29
30
  this.subgraphs = subgraphs;
30
- // Setup Default subgraphs
31
- this.registerSubgraph(AuthSubgraph, "graphql");
32
- this.registerSubgraph(SystemSubgraph, "graphql");
33
- this.registerSubgraph(AnalyticsSubgraph, "graphql");
34
- // special case for drive
35
- this.registerSubgraph(DriveSubgraph);
31
+ this.subgraphs.set("graphql", []);
36
32
  }
37
33
  async init() {
38
34
  console.log(`> Initializing Subgraph Manager...`);
35
+ // Setup Default subgraphs
36
+ await this.registerSubgraph(AuthSubgraph, "graphql");
37
+ await this.registerSubgraph(SystemSubgraph, "graphql");
38
+ await this.registerSubgraph(AnalyticsSubgraph, "graphql");
39
+ // special case for drive
40
+ await this.registerSubgraph(DriveSubgraph);
39
41
  const models = this.reactor.getDocumentModelModules();
40
42
  const driveModel = models.find((it) => it.documentModel.name === "DocumentDrive");
41
43
  if (!driveModel) {
@@ -44,15 +46,16 @@ export class GraphQLManager {
44
46
  this.reactor.on("documentModelModules", () => {
45
47
  this.updateRouter().catch((error) => console.error(error));
46
48
  });
47
- this.app.use(this.path, (req, res, next) => this.reactorRouter(req, res, next));
49
+ this.app.use("/", (req, res, next) => this.reactorRouter(req, res, next));
50
+ this.updateRouter();
48
51
  }
49
- async registerSubgraph(subgraph, supergraph = "", path = "") {
52
+ async registerSubgraph(subgraph, supergraph = "") {
50
53
  const subgraphInstance = new subgraph({
51
54
  operationalStore: this.operationalStore,
52
55
  analyticsStore: this.analyticsStore,
53
56
  reactor: this.reactor,
54
57
  graphqlManager: this,
55
- path,
58
+ path: this.path,
56
59
  });
57
60
  await subgraphInstance.onSetup();
58
61
  if (!this.subgraphs.get(supergraph)) {
@@ -62,10 +65,15 @@ export class GraphQLManager {
62
65
  this.subgraphs.set(supergraph, []);
63
66
  }
64
67
  this.subgraphs.get(supergraph)?.push(subgraphInstance);
65
- console.log(`> Registered ${supergraph ? "/" + supergraph : ""}${this.path.endsWith("/") ? this.path : this.path + "/"}${subgraphInstance.name} subgraph.`);
66
- await this.updateRouter();
68
+ // also add to global graphql supergraph
69
+ if (supergraph !== "" && supergraph !== "graphql") {
70
+ this.subgraphs.get("graphql")?.push(subgraphInstance);
71
+ }
72
+ console.log(`> Registered ${this.path.endsWith("/") ? this.path : this.path + "/"}${supergraph ? supergraph + "/" : ""}${subgraphInstance.name} subgraph.`);
73
+ // this.updateRouter();
67
74
  }
68
75
  async updateRouter() {
76
+ console.log("> Updating router");
69
77
  const newRouter = Router();
70
78
  newRouter.use(cors());
71
79
  newRouter.use(bodyParser.json());
@@ -110,9 +118,12 @@ export class GraphQLManager {
110
118
  }
111
119
  });
112
120
  }
121
+ #getSubgraphPath(subgraph, supergraph) {
122
+ return path.join(subgraph.path, supergraph, subgraph.name);
123
+ }
113
124
  async #setupSubgraphs(router) {
114
125
  for (const [supergraph, subgraphs] of this.subgraphs.entries()) {
115
- const supergraphEndpoints = {};
126
+ const supergraphEndpoints = [];
116
127
  for (const subgraph of subgraphs) {
117
128
  const subgraphConfig = this.#getLocalSubgraphConfig(subgraph.name);
118
129
  if (!subgraphConfig)
@@ -121,30 +132,39 @@ export class GraphQLManager {
121
132
  const schema = createSchema(this.reactor, subgraphConfig.resolvers, subgraphConfig.typeDefs);
122
133
  // create and start apollo server
123
134
  const server = this.#createApolloServer(schema);
124
- const path = `${subgraph.path ? "/" + subgraph.path : ""}${supergraph !== "" ? "/" + supergraph : ""}/${subgraphConfig.name}`;
135
+ const path = this.#getSubgraphPath(subgraph, supergraph);
125
136
  await server.start();
126
137
  await this.#waitForServer(server);
127
138
  this.#setupApolloExpressMiddleware(server, router, path);
128
139
  if (supergraph !== "") {
129
- supergraphEndpoints[path] = server;
140
+ supergraphEndpoints.push(this.#getSubgraphPath(subgraph, supergraph));
130
141
  }
131
142
  }
132
- if (Object.keys(supergraphEndpoints).length > 0) {
143
+ if (supergraphEndpoints.length > 0) {
144
+ await this.#sleep(1000);
133
145
  const supergraphServer = await this.#createApolloGateway(supergraphEndpoints);
134
146
  if (supergraphServer) {
135
- const path = `/${supergraph}`;
136
- this.#setupApolloExpressMiddleware(supergraphServer, router, path);
147
+ const superGraphPath = path.join(this.path, supergraph ?? "graphql");
148
+ this.#setupApolloExpressMiddleware(supergraphServer, router, superGraphPath);
137
149
  }
138
150
  }
139
151
  }
140
152
  }
153
+ #sleep(ms) {
154
+ return new Promise((resolve) => {
155
+ setTimeout(resolve, ms);
156
+ });
157
+ }
141
158
  async #createApolloGateway(endpoints) {
142
159
  try {
160
+ const herokuOrLocal = process.env.HEROKU_APP_DEFAULT_DOMAIN_NAME
161
+ ? `https://${process.env.HEROKU_APP_DEFAULT_DOMAIN_NAME}`
162
+ : `http://localhost:${process.env.PORT ?? 4001}`;
143
163
  const gateway = new ApolloGateway({
144
164
  supergraphSdl: new IntrospectAndCompose({
145
- subgraphs: Object.keys(endpoints).map((path) => ({
146
- name: path.replaceAll("/", ""),
147
- url: `http://localhost:${process.env.PORT ?? 4001}${path}`,
165
+ subgraphs: endpoints.map((endpoint) => ({
166
+ name: endpoint.replaceAll("/", ""),
167
+ url: `${herokuOrLocal}${endpoint}`,
148
168
  })),
149
169
  }),
150
170
  });
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAOnD,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAM3D,OAAgB,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAMhD,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,IAAI,CAAC;AAC/B,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,YAAY,CAAC;AAGtC,KAAK,OAAO,GAAG;IACb,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,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"}
@@ -1,3 +1,4 @@
1
+ import { config } from "#config.js";
1
2
  import { GraphQLManager } from "#graphql/graphql-manager.js";
2
3
  import { renderGraphqlPlayground } from "#graphql/playground.js";
3
4
  import { getUniqueDocumentModels, PackagesManager, } from "#package-manager.js";
@@ -13,11 +14,12 @@ const DEFAULT_PORT = 4000;
13
14
  /**
14
15
  * Sets up the Express app with necessary routes
15
16
  */
16
- function setupGraphQlExplorer(app) {
17
- app.get("/explorer/:endpoint?", (req, res) => {
17
+ function setupGraphQlExplorer(router) {
18
+ router.get("/explorer/:endpoint?", (req, res) => {
18
19
  res.setHeader("Content-Type", "text/html");
19
- const basePath = process.env.BASE_PATH === "/" ? "" : process.env.BASE_PATH || "";
20
- const endpoint = `${basePath}${req.params.endpoint !== undefined ? `/${req.params.endpoint}` : "/graphql"}`;
20
+ const endpoint = req.params.endpoint !== undefined
21
+ ? `/${req.params.endpoint}`
22
+ : "/graphql";
21
23
  const { query } = req.query;
22
24
  if (query && typeof query !== "string") {
23
25
  throw new Error("Invalid query");
@@ -54,15 +56,15 @@ async function initializePackageManager(options) {
54
56
  * Sets up the subgraph manager and registers subgraphs
55
57
  */
56
58
  async function setupGraphQLManager(app, reactor, db, analyticsStore, result) {
57
- const graphqlManager = new GraphQLManager("/", app, reactor, db, analyticsStore);
58
- await graphqlManager.init();
59
+ const graphqlManager = new GraphQLManager(config.basePath, app, reactor, db, analyticsStore);
59
60
  if (result.subgraphs) {
60
61
  for (const [supergraph, subgraphs] of result.subgraphs.entries()) {
61
62
  for (const subgraph of subgraphs) {
62
- graphqlManager.registerSubgraph(subgraph, supergraph);
63
+ await graphqlManager.registerSubgraph(subgraph, supergraph);
63
64
  }
64
65
  }
65
66
  }
67
+ await graphqlManager.init();
66
68
  return graphqlManager;
67
69
  }
68
70
  /**
@@ -80,6 +82,7 @@ function setupEventListeners(pkgManager, reactor, graphqlManager, processorManag
80
82
  graphqlManager.registerSubgraph(subgraph, supergraph);
81
83
  }
82
84
  }
85
+ graphqlManager.updateRouter();
83
86
  });
84
87
  pkgManager.onProcessorsChange(async (processors) => {
85
88
  for (const [packageName, fn] of processors) {
@@ -124,8 +127,9 @@ async function startServer(app, port, httpsOptions) {
124
127
  export async function startAPI(reactor, options) {
125
128
  const port = options.port ?? DEFAULT_PORT;
126
129
  const app = options.express ?? express();
127
- // Set up Express app with routes
128
- setupGraphQlExplorer(app);
130
+ const defaultRouter = express.Router();
131
+ setupGraphQlExplorer(defaultRouter);
132
+ app.use(config.basePath, defaultRouter);
129
133
  // Initialize database and analytics store
130
134
  const { db, analyticsStore } = initializeDatabaseAndAnalytics(options.dbPath);
131
135
  const module = { db, analyticsStore };