Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Dec 18, 2023
1 parent aff63b6 commit 4472bbe
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 43 deletions.
2 changes: 0 additions & 2 deletions packages/cli/src/commands/generate-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ describe("generate schema command", () => {
// config: configPath,
// // namespaceId: "test",
// });

// Check if the schema file exists after generation
//const schemaFileExists = await checkFileExists("./.hydra/schemaRaw.ts");
//expect(schemaFileExists).toBeTruthy();

// Remove the generated schema file after validation
//await fs.unlink("./.hydra/schemaRaw.ts");
} else {
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/commands/generate-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IGenerateSchemaAction {

export const generateSchemaAction = async ({
config = hydraConfigPathDefault,
namespaceId
namespaceId,
}: IGenerateSchemaAction) => {
const rootPath = process.cwd(); // Get the root directory path
const configPath = path.resolve(rootPath, config); // Construct absolute path for config
Expand Down Expand Up @@ -46,7 +46,11 @@ export const generateSchemaAction = async ({
return logError(`Invalid config [${config}]`);
}
try {
await buildSchemaIntrospection(validatedConfig.schemas, outputPath, namespaceId);
await buildSchemaIntrospection(
validatedConfig.schemas,
outputPath,
namespaceId,
);
} catch (error: any) {
return logError(error.message);
}
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ program
"-c, --config <configPath>",
"Path to Hydra configuration path, default being <hydra.config.ts>",
)
.option(
"-nid, --namespace-id <namespaceId>",
"Cloudflare KV Namespace ID",
)
.option("-nid, --namespace-id <namespaceId>", "Cloudflare KV Namespace ID")
.action(actionRunner(generateSchemaAction));

// TODO
Expand Down
14 changes: 6 additions & 8 deletions packages/cli/src/utils/introspectSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { initSpinner, stopSpinner } from "./spinners";
// import { spawn } from "node:child_process";
import fs from "fs";


export async function introspectRemoteSchema(endpointUrl: string) {
try {
// Send an introspection query to the GraphQL endpoint
Expand Down Expand Up @@ -71,16 +70,17 @@ export const buildSchemaIntrospection = async (

stopSpinner(interval);

const fileContent = schemaWithIntrospection.map(s => ({
const fileContent = schemaWithIntrospection.map((s) => ({
name: s.name,
url: s.url,
introspected: s.introspected
introspected: s.introspected,
}));


// generate a schemaRaw file (.hydra/schemaRaw.js) module.exports = <fileContent>
fs.writeFileSync(outputPath, `module.exports = ${JSON.stringify(fileContent, null, 2)}`);

fs.writeFileSync(
outputPath,
`module.exports = ${JSON.stringify(fileContent, null, 2)}`,
);

// if (namespaceId && namespaceId !== "test") {
// // write with wrangler child process
Expand All @@ -97,6 +97,4 @@ export const buildSchemaIntrospection = async (
// console.log(`stdout: ${data}`);
// });
// }


};
2 changes: 1 addition & 1 deletion packages/core/src/handlers/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const GraphQLHandler = async (
env: any,
ctx: any,
): Promise<any> => {
const {rawSchema} = ctx;
const { rawSchema } = ctx;
const yoga = createYoga({
schema: await getSchema(rawSchema),
context: async (event) => {
Expand Down
21 changes: 7 additions & 14 deletions packages/core/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ const extractPrimitiveWithRegExp = (rawSchema: string, regex: RegExp) => {
return operations;
};


export const getSchema = (schemas: any) => {

// If the schemaRaw is empty, handle it here
if (!schemas || schemas.length === 0) {
console.warn("SchemaRaw is empty or invalid.");
// Perform necessary actions or log messages for an empty schemaRaw
}



const queries = [
...schemas
.map(({ introspected, url }) => {
Expand All @@ -67,7 +64,7 @@ export const getSchema = (schemas: any) => {
})
.flat(),
];

const mutations = [
...schemas
.map(({ introspected, url }) => {
Expand All @@ -93,20 +90,20 @@ export const getSchema = (schemas: any) => {
})
.flat(),
];

const resolvers = {
Query: {},
Mutation: {},
};

queries?.forEach(({ id, resolver }) => {
resolvers.Query[id] = resolver;
});

mutations?.forEach(({ id, resolver }) => {
resolvers.Mutation[id] = resolver;
});

let schema = makeExecutableSchema({
typeDefs: buildGraphQLSchemaSDL(
unifyGraphQLSchemasObjects([
Expand All @@ -117,7 +114,7 @@ export const getSchema = (schemas: any) => {
});

return schema;
}
};

const fetchGraphQLEndpoint = async (
endpoint: string,
Expand Down Expand Up @@ -162,8 +159,4 @@ const getAuthorizationFromContext = (ctx) => {
return authorization;
};





// export { schema };
1 change: 0 additions & 1 deletion services/itty-hydra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ tag = "v2"
new_classes = [ "RateLimiter" ]
```


## Setup schema

### Write
Expand Down
19 changes: 8 additions & 11 deletions services/itty-hydra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,28 @@ import { Health } from "./handlers/health";
import { GraphQLHandler, HydraHandler } from "@authdog/hydra-core";
import { HydraConfigAcme } from "./hydra.config";

import {default as rawSchema} from "./.hydra/schemaRaw"
import { default as rawSchema } from "./.hydra/schemaRaw";

const { preflight, corsify } = createCors();


const router = Router();
router
// .all("*", withDurables())
.options("*", preflight)
.get("/", Health)
.get("/health", Health)
.get("/schema", async (req, env, ctx) => {

const schema = await ctx.rawSchema
const schema = await ctx.rawSchema;

// const schema = await ctx.kv.get("schema")

return new Response(
JSON.stringify(schema),
{ status: 200, headers: {
return new Response(JSON.stringify(schema), {
status: 200,
headers: {
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
} },
);

},
});
})
// serves playground
.get("/graphql", GraphQLHandler)
Expand All @@ -46,7 +43,7 @@ const handleRequest = (req, env, ctx) => {
...ctx,
kv: HYDRA_ACME,
hydraConfig: HydraConfigAcme,
rawSchema
rawSchema,
// rateLimiter: null,
};

Expand Down

0 comments on commit 4472bbe

Please sign in to comment.