Skip to content

Commit

Permalink
cache: scaffold kv integration via context
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Nov 25, 2023
1 parent c2e62ca commit 312e79f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"crypto-js": "^4.2.0",
"graphql-tag": "^2.12.6"
}
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {GraphQLHandler} from "./handlers/graphql";
export {GraphQLHandler} from "./handlers/graphql";
export {generateGraphQLCacheKey} from "./invalidation/invalidation";
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions services/itty-hydra/hydra.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const HydraConfig = {
rateLimiting: {
default: {
budget: 100,
},
},
publicQueries: [
{
name: "health",
},
{
name: "hydraDevQuery",
}
],
jwksUri: "https://id.authdog.com/oidc/.well-known/jwks.json",
};

79 changes: 76 additions & 3 deletions services/itty-hydra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,90 @@ import { withDurables } from "itty-durable";
import { NotFound } from "./handlers/notFound";
import { Health } from "./handlers/health";
import {GraphQLHandler} from "@authdog/hydra-core";
import { HydraConfig } from "./hydra.config";
import { extractedAllQueryIdentifiersInRawQuery, generateGraphQLCacheKey } from "@authdog/hydra-core/src/invalidation/invalidation";

const { preflight, corsify } = createCors();

const router = Router();
router
.all("*", withDurables())
// .all("*", withDurables())
.options("*", preflight)
.get("/", Health)
.get("/health", Health)
// serves playground
.get("/graphql", GraphQLHandler)
.post("/graphql", GraphQLHandler)
.post("/graphql", async (req, env, ctx) => {
const {kv} = ctx;

let extractedQueries = [];
let cacheKey = null;

// const clonedRequest = req.clone();

// // const requestHeaders = clonedRequest?.headers;
const requestBody = await req.clone()?.json();

let isIntrospection = true;
if (requestBody?.operationName !== "IntrospectionQuery") {
isIntrospection = false;


// const isMutation = requestBody?.query?.startsWith("mutation");

extractedQueries = extractedAllQueryIdentifiersInRawQuery(
requestBody?.query
);

const variables = JSON.stringify(requestBody?.variables);


const requiresAuthorization = extractedQueries.some((query) => {
return !HydraConfig.publicQueries.some((publicQuery) => {
return publicQuery.name === query;
});
});

if (!requiresAuthorization) {
cacheKey = await generateGraphQLCacheKey({
query: requestBody?.query,
variables,
});
} else if (requiresAuthorization) {
return new Response(
JSON.stringify({
errors: [
{
message: "Unauthorized",
},
],
}),
{
status: 401,
}
);
}

if (cacheKey) {
const cachedResponse = await kv.get(cacheKey);
if (cachedResponse) {
return new Response(cachedResponse, {
status: 200,
});
}
}
}

const response = await GraphQLHandler(req, env, ctx);

if (cacheKey) {
const responseBody = await response?.clone()?.json();
const responseBodyString = JSON.stringify(responseBody);
await kv.put(cacheKey, responseBodyString);
}

return response;
})
.get("*", NotFound);

const handleRequest = (req, env, ctx) => {
Expand All @@ -23,7 +96,7 @@ const handleRequest = (req, env, ctx) => {
const enrichedContext = {
...ctx,
kv: HYDRA_ACME,
rateLimiter: null,
// rateLimiter: null,
};

return router
Expand Down

0 comments on commit 312e79f

Please sign in to comment.