diff --git a/packages/core/src/handlers/graphql.ts b/packages/core/src/handlers/graphql.ts index 077c0b2..48ef127 100644 --- a/packages/core/src/handlers/graphql.ts +++ b/packages/core/src/handlers/graphql.ts @@ -25,4 +25,4 @@ export const GraphQLHandler = async (req: Request, env: any, ctx: any) => { landingPage: false, }); return yoga(req, env, ctx); -}; \ No newline at end of file +}; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 44a1424..91acb4a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,2 +1,2 @@ -export {GraphQLHandler} from "./handlers/graphql"; -export {generateGraphQLCacheKey} from "./invalidation/invalidation"; \ No newline at end of file +export { GraphQLHandler } from "./handlers/graphql"; +export { generateGraphQLCacheKey } from "./invalidation/invalidation"; diff --git a/services/itty-hydra/README.md b/services/itty-hydra/README.md index 047f7f3..26f185c 100644 --- a/services/itty-hydra/README.md +++ b/services/itty-hydra/README.md @@ -6,12 +6,10 @@ This project is a sample application to demonstrate the use of Hydra with Itty. Wrangler is recommended for local development and deployment. - ## Cloudflare Configuration A KV namespace and a Durable object (optional) are required for this project. - ## KV Namespace KV is used to cache GraphQL queries payload with user segmentation meaning each user will have a different cache segment. @@ -52,4 +50,4 @@ class_name = "RateLimiter" [[migrations]] tag = "v2" new_classes = [ "RateLimiter" ] -``` \ No newline at end of file +``` diff --git a/services/itty-hydra/handlers/hydraHandler.ts b/services/itty-hydra/handlers/hydraHandler.ts index be2422b..6922e23 100644 --- a/services/itty-hydra/handlers/hydraHandler.ts +++ b/services/itty-hydra/handlers/hydraHandler.ts @@ -1,11 +1,7 @@ -interface Context { +interface Context {} -} - -export const Hydra = async (request: Request, env: any, ctx: Context - - ) => { - return new Response("Hydra handler", { - status: 200, - }); -} \ No newline at end of file +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 9db3ca3..154ff73 100644 --- a/services/itty-hydra/hydra.config.ts +++ b/services/itty-hydra/hydra.config.ts @@ -1,17 +1,16 @@ export const HydraConfig = { - rateLimiting: { - default: { - budget: 100, - }, + rateLimiting: { + default: { + budget: 100, }, - publicQueries: [ - { - name: "health", - }, - { - name: "hydraDevQuery", - } - ], - jwksUri: "https://id.authdog.com/oidc/.well-known/jwks.json", - }; - \ No newline at end of file + }, + publicQueries: [ + { + name: "health", + }, + { + name: "hydraDevQuery", + }, + ], + jwksUri: "https://id.authdog.com/oidc/.well-known/jwks.json", +}; diff --git a/services/itty-hydra/main.ts b/services/itty-hydra/main.ts index e4ea94c..726dbba 100644 --- a/services/itty-hydra/main.ts +++ b/services/itty-hydra/main.ts @@ -3,9 +3,12 @@ import { createCors } from "itty-cors"; import { withDurables } from "itty-durable"; import { NotFound } from "./handlers/notFound"; import { Health } from "./handlers/health"; -import {GraphQLHandler} from "@authdog/hydra-core"; +import { GraphQLHandler } from "@authdog/hydra-core"; import { HydraConfig } from "./hydra.config"; -import { extractedAllQueryIdentifiersInRawQuery, generateGraphQLCacheKey } from "@authdog/hydra-core/src/invalidation/invalidation"; +import { + extractedAllQueryIdentifiersInRawQuery, + generateGraphQLCacheKey, +} from "@authdog/hydra-core/src/invalidation/invalidation"; const { preflight, corsify } = createCors(); @@ -18,7 +21,7 @@ router // serves playground .get("/graphql", GraphQLHandler) .post("/graphql", async (req, env, ctx) => { - const {kv} = ctx; + const { kv } = ctx; let extractedQueries = []; let cacheKey = null; @@ -32,66 +35,64 @@ router if (requestBody?.operationName !== "IntrospectionQuery") { isIntrospection = false; + // const isMutation = requestBody?.query?.startsWith("mutation"); - // const isMutation = requestBody?.query?.startsWith("mutation"); - - extractedQueries = extractedAllQueryIdentifiersInRawQuery( - requestBody?.query - ); - - const variables = JSON.stringify(requestBody?.variables); - + extractedQueries = extractedAllQueryIdentifiersInRawQuery( + requestBody?.query, + ); - const requiresAuthorization = extractedQueries.some((query) => { - return !HydraConfig.publicQueries.some((publicQuery) => { - return publicQuery.name === query; - }); - }); + const variables = JSON.stringify(requestBody?.variables); - if (!requiresAuthorization) { - cacheKey = await generateGraphQLCacheKey({ - query: requestBody?.query, - variables, + const requiresAuthorization = extractedQueries.some((query) => { + return !HydraConfig.publicQueries.some((publicQuery) => { + return publicQuery.name === query; + }); }); - } 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, + if (!requiresAuthorization) { + cacheKey = await generateGraphQLCacheKey({ + query: requestBody?.query, + variables, }); + } else if (requiresAuthorization) { + return new Response( + JSON.stringify({ + errors: [ + { + message: "Unauthorized", + }, + ], + }), + { + status: 401, + }, + ); } - } - } - 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); + 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; + return response; }) .get("*", NotFound); const handleRequest = (req, env, ctx) => { - const {HYDRA_ACME} = env; + const { HYDRA_ACME } = env; const enrichedContext = { ...ctx,