Skip to content

Commit

Permalink
chore: prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Nov 25, 2023
1 parent 312e79f commit dcc42ec
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/handlers/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export const GraphQLHandler = async (req: Request, env: any, ctx: any) => {
landingPage: false,
});
return yoga(req, env, ctx);
};
};
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {GraphQLHandler} from "./handlers/graphql";
export {generateGraphQLCacheKey} from "./invalidation/invalidation";
export { GraphQLHandler } from "./handlers/graphql";
export { generateGraphQLCacheKey } from "./invalidation/invalidation";
4 changes: 1 addition & 3 deletions services/itty-hydra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -52,4 +50,4 @@ class_name = "RateLimiter"
[[migrations]]
tag = "v2"
new_classes = [ "RateLimiter" ]
```
```
16 changes: 6 additions & 10 deletions services/itty-hydra/handlers/hydraHandler.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
export const Hydra = async (request: Request, env: any, ctx: Context) => {
return new Response("Hydra handler", {
status: 200,
});
};
29 changes: 14 additions & 15 deletions services/itty-hydra/hydra.config.ts
Original file line number Diff line number Diff line change
@@ -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",
};
},
publicQueries: [
{
name: "health",
},
{
name: "hydraDevQuery",
},
],
jwksUri: "https://id.authdog.com/oidc/.well-known/jwks.json",
};
97 changes: 49 additions & 48 deletions services/itty-hydra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit dcc42ec

Please sign in to comment.