From 6150e6d4af8096abddfbc4676b39bb07a6d8fc87 Mon Sep 17 00:00:00 2001 From: David Barrat Date: Sun, 17 Dec 2023 23:32:42 +0100 Subject: [PATCH] use JSON as persistence format for local schema --- .changeset/shiny-bulldogs-leave.md | 5 ++++ .gitignore | 2 ++ packages/cli/src/__assets__/constants.ts | 2 +- packages/cli/src/utils/introspectSchemas.ts | 29 +++++++++++++------- packages/core/src/schema.ts | 15 ++++++---- services/itty-hydra/handlers/hydraHandler.ts | 7 ----- services/itty-hydra/hydra.config.ts | 12 +++++++- services/itty-hydra/main.ts | 6 ++-- services/itty-hydra/package.json | 2 +- 9 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 .changeset/shiny-bulldogs-leave.md delete mode 100644 services/itty-hydra/handlers/hydraHandler.ts diff --git a/.changeset/shiny-bulldogs-leave.md b/.changeset/shiny-bulldogs-leave.md new file mode 100644 index 0000000..1f2ee37 --- /dev/null +++ b/.changeset/shiny-bulldogs-leave.md @@ -0,0 +1,5 @@ +--- +"@authdog/hydra-core": patch +--- + +use json as schemaRaw persistence format diff --git a/.gitignore b/.gitignore index d1595af..90c8aed 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ yarn-error.log* # vercel .vercel + +.hydra \ No newline at end of file diff --git a/packages/cli/src/__assets__/constants.ts b/packages/cli/src/__assets__/constants.ts index 14e1813..0da02e1 100644 --- a/packages/cli/src/__assets__/constants.ts +++ b/packages/cli/src/__assets__/constants.ts @@ -1,2 +1,2 @@ -export const hydraSchemaRawPath = ".hydra/schemaRaw.js"; +export const hydraSchemaRawPath = ".hydra/schemaRaw.json"; export const hydraConfigPathDefault = "./hydra.config.ts"; diff --git a/packages/cli/src/utils/introspectSchemas.ts b/packages/cli/src/utils/introspectSchemas.ts index 62aa4fb..c4a6d1e 100644 --- a/packages/cli/src/utils/introspectSchemas.ts +++ b/packages/cli/src/utils/introspectSchemas.ts @@ -68,16 +68,25 @@ export const buildSchemaIntrospection = async ( stopSpinner(interval); // const outputPath = "src/handlers/federation/schemaRawGenerated.ts"; - const exportStatements = schemaWithIntrospection.map( - (s) => - `export const ${s.name} = { - name: "${s.name}", - url: "${s.url}", - introspected: ${JSON.stringify(s.introspected, null, 2)} - };`, - ); - - const fileContent = `${exportStatements.join("\n")}\n`; + // const exportStatements = schemaWithIntrospection.map( + // (s) => + // `export const ${s.name} = { + // name: "${s.name}", + // url: "${s.url}", + // introspected: ${JSON.stringify(s.introspected, null, 2)} + // };`, + // ); + + const exportObjects = schemaWithIntrospection.map(s => ({ + name: s.name, + url: s.url, + introspected: s.introspected + })); + + // const fileContent = `${exportStatements.join("\n")}\n`; + + const fileContent = JSON.stringify(exportObjects, null, 2); + const fs = require("fs"); diff --git a/packages/core/src/schema.ts b/packages/core/src/schema.ts index af18793..c0daba2 100644 --- a/packages/core/src/schema.ts +++ b/packages/core/src/schema.ts @@ -1,22 +1,27 @@ import { makeExecutableSchema } from "@graphql-tools/schema"; import { default as fetch } from "node-fetch"; -let schemaRaw; +import * as fs from "fs"; + +let schemas = []; + try { - schemaRaw = require(".hydra/schemaRaw"); + // schemaRaw = require(".hydra/schemaRaw"); + const filePath = "./.hydra/schemaRaw.json"; + const fileContent = fs.readFileSync(filePath, { encoding: "utf8" }); + schemas = JSON.parse(fileContent); } catch (error) { // Handle error when import fails console.error("Error loading schemaRaw:", error); - schemaRaw = {}; // Set schemaRaw as an empty object when import fails } // If the schemaRaw is empty, handle it here -if (!schemaRaw || Object.keys(schemaRaw).length === 0) { +if (!schemas || schemas.length === 0) { console.warn("SchemaRaw is empty or invalid."); // Perform necessary actions or log messages for an empty schemaRaw } -const schemas = Object.values(schemaRaw); + import { addTypenameKeywordToSchema, diff --git a/services/itty-hydra/handlers/hydraHandler.ts b/services/itty-hydra/handlers/hydraHandler.ts deleted file mode 100644 index 6922e23..0000000 --- a/services/itty-hydra/handlers/hydraHandler.ts +++ /dev/null @@ -1,7 +0,0 @@ -interface Context {} - -export const Hydra = async (request: Request, env: any, ctx: Context) => { - return new Response("Hydra handler", { - status: 200, - }); -}; diff --git a/services/itty-hydra/hydra.config.ts b/services/itty-hydra/hydra.config.ts index 154ff73..715324f 100644 --- a/services/itty-hydra/hydra.config.ts +++ b/services/itty-hydra/hydra.config.ts @@ -1,4 +1,14 @@ -export const HydraConfig = { +export const HydraConfigAcme = { + schemas: [ + { + id: "mgt", + uri: "https://mgt.auth.dog/graphql", + }, + { + id: "authz", + uri: "https://authz.auth.dog/graphql", + }, + ], rateLimiting: { default: { budget: 100, diff --git a/services/itty-hydra/main.ts b/services/itty-hydra/main.ts index 432a94f..53bc60e 100644 --- a/services/itty-hydra/main.ts +++ b/services/itty-hydra/main.ts @@ -4,7 +4,7 @@ import { createCors } from "itty-cors"; import { NotFound } from "./handlers/notFound"; import { Health } from "./handlers/health"; import { GraphQLHandler, HydraHandler } from "@authdog/hydra-core"; -import { HydraConfig } from "./hydra.config"; +import { HydraConfigAcme } from "./hydra.config"; const { preflight, corsify } = createCors(); @@ -25,10 +25,12 @@ const handleRequest = (req, env, ctx) => { const enrichedContext = { ...ctx, kv: HYDRA_ACME, - hydraConfig: HydraConfig, + hydraConfig: HydraConfigAcme, // rateLimiter: null, }; + + return router .handle(req, env, enrichedContext) .catch( diff --git a/services/itty-hydra/package.json b/services/itty-hydra/package.json index 33239d4..5d14493 100644 --- a/services/itty-hydra/package.json +++ b/services/itty-hydra/package.json @@ -6,7 +6,7 @@ "private": true, "scripts": { "build": "node build.mjs", - "dev": "npx wrangler dev -c wrangler.base.toml --live-reload --port 3544", + "dev": "npx wrangler dev -c wrangler.base.toml --live-reload --port 3546", "deploy": "npx wrangler -c wrangler.base.toml deploy --minify" }, "keywords": [],