Skip to content

Commit

Permalink
Merge branch 'gql' into course-section-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ARtheboss authored Apr 17, 2024
2 parents 3a4f748 + 1145edd commit 4d4c99d
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 64 deletions.
52 changes: 25 additions & 27 deletions .github/workflows/gen.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name:
Codegen Test
name: Codegen Test

on:
push:
Expand All @@ -9,28 +8,27 @@ jobs:
Codegen:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: install node v12
uses: actions/setup-node@v1
with:
node-version: 12

- name: npm install
run: npm install

- name: gen
run: npm run generate

- name: test diff
uses: tj-actions/verify-changed-files@v14
id: test-diff
with:
files: |
**/generated-types/*.ts
- if: steps.test-diff.outputs.files_changed == 'true'
run: |
echo "Run 'npm run generate' to fix!"
exit 1
- uses: actions/checkout@v2

- name: install node v12
uses: actions/setup-node@v1
with:
node-version: 12

- name: npm install and gen
working-directory: ./backend
run: |
npm install
npm run generate
- name: test diff
uses: tj-actions/verify-changed-files@v14
id: test-diff
with:
files: |
./backend/**/generated-types/*.ts
- if: steps.test-diff.outputs.files_changed == 'true'
run: |
echo "Run 'npm run generate' to fix!"
exit 1
24 changes: 12 additions & 12 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name:
Linting

name: Linting

on:
push:
pull_request:
Expand All @@ -9,12 +8,13 @@ jobs:
Lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: install node v12
uses: actions/setup-node@v1
with:
node-version: 12
- name: npm install
run: npm install
- name: lint
run: npm run lint
- uses: actions/checkout@v1
- name: install node v12
uses: actions/setup-node@v1
with:
node-version: 12
- name: npm install and lint
working-directory: ./backend
run: |
npm install
npm run lint
105 changes: 105 additions & 0 deletions backend/package-lock.json

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

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"dependencies": {
"@apollo/server": "^4.1.0",
"@apollo/server-plugin-response-cache": "^4.1.3",
"@graphql-tools/schema": "^10.0.0",
"@graphql-tools/utils": "^10.0.7",
"axios": "^1.5.1",
Expand All @@ -66,6 +67,7 @@
"mongoose": "^7.6.3",
"passport": "^0.6.0",
"passport-google-oauth20": "^2.0.0",
"redis": "^4.6.13",
"reflect-metadata": "^0.1.13"
},
"overrides": {
Expand Down
37 changes: 35 additions & 2 deletions backend/src/bootstrap/loaders/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
import { ApolloServer } from "@apollo/server";
import { buildSchema } from "../graphql/buildSchema";
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
import { ApolloServerPluginCacheControl } from '@apollo/server/plugin/cacheControl';
import { KeyValueCache, KeyValueCacheSetOptions } from '@apollo/utils.keyvaluecache';
import responseCachePlugin from '@apollo/server-plugin-response-cache';
import { RedisClientType, createClient } from 'redis';

export default async () => {
class RedisCache implements KeyValueCache {
client: RedisClientType;
prefix: string = 'apollo-cache:';

constructor(client: RedisClientType) {
this.client = client;
}

async get(key: string) {
return await this.client.get(this.prefix + key) ?? undefined;
}

async set(key: string, value: string, _?: KeyValueCacheSetOptions | undefined) {
// ttl options are intentionally ignored because we will invalidate cache in update script
await this.client.set(this.prefix + key, value)
}

async delete(key: string) {
return await this.client.del(this.prefix + key) === 1;
}

}

export default async (redis: RedisClientType) => {
const schema = buildSchema();

const server = new ApolloServer({
schema,
plugins: [ApolloServerPluginLandingPageLocalDefault({ includeCookies: true })],
plugins: [
ApolloServerPluginLandingPageLocalDefault({ includeCookies: true }),
ApolloServerPluginCacheControl({ calculateHttpHeaders: false }),
responseCachePlugin(),
],
introspection: true, // TODO(production): disable introspection upon final deployment
cache: new RedisCache(redis),
});
await server.start();

Expand Down
7 changes: 6 additions & 1 deletion backend/src/bootstrap/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { config } from "../../config";
import apolloLoader from "./apollo";
import expressLoader from "./express";
import mongooseLoader from "./mongoose";
import redisLoader from "./redis";

export default async (root: Application): Promise<void> => {
const app = Router() as Application;
Expand All @@ -13,9 +14,13 @@ export default async (root: Application): Promise<void> => {
console.log("Booting up mongo...");
await mongooseLoader();

// connect to redis
console.log("Booting up redis...");
const redis = await redisLoader();

// load apollo server config. must be loaded before express
console.log("Loading apollo...");
const server = await apolloLoader();
const server = await apolloLoader(redis);

// load everything related to express. depends on apollo
console.log("Loading express...");
Expand Down
6 changes: 6 additions & 0 deletions backend/src/bootstrap/loaders/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RedisClientType, createClient } from "redis"
import { config } from "../../config";

export default async (): Promise<RedisClientType> => {
return await createClient({ url: config.redisUri }).connect() as RedisClientType;
}
2 changes: 2 additions & 0 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Config {
SESSION_SECRET: string;
GOOGLE_CLIENT_ID: string;
GOOGLE_CLIENT_SECRET: string;
redisUri: string;
}

// All your secrets, keys go here
Expand All @@ -51,4 +52,5 @@ export const config: Config = {
SESSION_SECRET: env("SESSION_SECRET"),
GOOGLE_CLIENT_ID: env("GOOGLE_CLIENT_ID"),
GOOGLE_CLIENT_SECRET: env("GOOGLE_CLIENT_SECRET"),
redisUri: env("REDIS_URI"),
};
Loading

0 comments on commit 4d4c99d

Please sign in to comment.