Skip to content

Commit

Permalink
use JSON as persistence format for local schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Dec 17, 2023
1 parent 0c4105c commit 6150e6d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-bulldogs-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@authdog/hydra-core": patch
---

use json as schemaRaw persistence format
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*

# vercel
.vercel

.hydra
2 changes: 1 addition & 1 deletion packages/cli/src/__assets__/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const hydraSchemaRawPath = ".hydra/schemaRaw.js";
export const hydraSchemaRawPath = ".hydra/schemaRaw.json";
export const hydraConfigPathDefault = "./hydra.config.ts";
29 changes: 19 additions & 10 deletions packages/cli/src/utils/introspectSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/schema.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 0 additions & 7 deletions services/itty-hydra/handlers/hydraHandler.ts

This file was deleted.

12 changes: 11 additions & 1 deletion services/itty-hydra/hydra.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 4 additions & 2 deletions services/itty-hydra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion services/itty-hydra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down

0 comments on commit 6150e6d

Please sign in to comment.