Skip to content

Commit

Permalink
App Router (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkyslyy authored Jul 2, 2024
1 parent 8746bbc commit 66753eb
Show file tree
Hide file tree
Showing 106 changed files with 4,598 additions and 12,596 deletions.
12 changes: 7 additions & 5 deletions .env
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
PRISMA_CONNECTION_LIMIT=5
PRISMA_CONNECTION_LIMIT=15
CREATE_AURORA_DATABASE=true

# for local dev environments which use docker-compose, you can disable running migrations in AWS by setting this to false
# if you want to automatically run DB migrations on deployed environments
# (does not apply to local dev)
RUN_DB_MIGRATIONS=true

# set this for non-local-dev stages to your web URL
# WEB_DOMAIN=
# e.g. WEB_DOMAIN=d3s9sdc942lbai.cloudfront.net

# set this to enable a bastion EC2 host you can tunnel through to connect to the database
# create a keypair in your desired account and region and add the name here
# SSH_KEYPAIR_NAME=

# set this for non-local-dev stages to your web URL
# WEB_URL=
4 changes: 3 additions & 1 deletion .env.prod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
IS_PRODUCTION=true

# WEB_URL=https://mysite.com
# assumes you have a hosted zone set up in Route53
# HOSTED_ZONE=mysite.com
# WEB_DOMAIN=mysite.com
63 changes: 0 additions & 63 deletions .eslintrc.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
# generate types from infra
# needed for typechecks
- name: SST types
run: pnpm sst:types --stage test
run: pnpm run sst:types --stage test
- name: Type checks
run: pnpm build
- name: Lint
Expand All @@ -36,7 +36,7 @@ jobs:
test-backend:
name: Test backend
env:
DATABASE_URL: "postgresql://postgres:pass@localhost:5438/myapp?connection_limit=80"
DATABASE_URL: "postgresql://prisma:prisma@localhost:5432/tests?connection_limit=80"

services:
postgres:
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
- name: Install dependencies
run: pnpm install
- name: Type checks
run: pnpm -C web tsc
run: pnpm run -C web tsc
- uses: actions/cache@v4
name: Enable NextJS cache
# https://nextjs.org/docs/messages/no-cache#github-actions
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/generate-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Generate release for tag

on:
push:
tags:
- "*"

jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: ncipollo/release-action@v1
with:
generateReleaseNotes: true
33 changes: 33 additions & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build on push

on:
push:
branches:
- "**"

# don't run if a tag was pushed
tags-ignore:
- "v*"

# don't run if only package.json (version) was updated
paths-ignore:
- "package.json"

# This will cancel the workflow https://stackoverflow.com/questions/66335225/how-to-cancel-previous-runs-in-the-pr-when-you-push-new-commitsupdate-the-curre
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-test:
uses: ./.github/workflows/build-and-test.yml

# enable if PAT is provided
# update-version:
# # if on production branch, bump version
# if: github.ref == 'refs/heads/prod'
# needs: build-and-test
# uses: ./.github/workflows/update-version.yml
# secrets:
# # personal token is required to trigger other workflows
# RELEASE_PAT: ${{ secrets.RELEASE_PAT }}
38 changes: 38 additions & 0 deletions .github/workflows/update-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Bump version and tag

name: Update version and tag

on:
workflow_call:
secrets:
RELEASE_PAT:
# personal token is required to trigger other workflows
required: true
workflow_dispatch:

jobs:
update-version:
name: Update version and tag
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.RELEASE_PAT }}
- uses: pnpm/action-setup@v3
with:
version: 9
- name: Generate new version
run: |
git config --local user.email "[email protected]"
git config --local user.name "github-bot"
pnpm version patch
- name: Push new version
run: git push
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_PAT }}
- name: Push new tag
run: git push --tags
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_PAT }}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

!jest.config.js

# TSC build output
/dist

*.tsbuildinfo
4 changes: 2 additions & 2 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tabWidth: 2
semi: true
semi: false
printWidth: 120
trailingComma: es5
trailingComma: all
singleQuote: true
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ Assumes you have [Node 20](https://nodejs.org/en/download/current/) installed an

### Setup

Edit `sst.config.ts` and set your default AWS profile and region.

```shell
npm i -g pnpm # install pnpm globally
pnpm i # install dependencies + generate types from DB and GQL schemas
pnpm db:migrate # run DB migrations
pnpm dev # start local dev server
```

Expand All @@ -28,7 +31,6 @@ Set your default AWS profile and region in `sst.config.ts`.

Edit .env or .env.$stage to set infrastructure vars.


## Features

All features are optional, delete what you don't need.
Expand All @@ -37,19 +39,19 @@ Click links to learn more and view documentation.

- 🌩 [Serverless Stack](https://serverless-stack.com/) - powerful CDK developer experience tools
- 🌤 [AWS CDK](https://aws.amazon.com/cdk/) - cloud-native infrastructure as code
-[GraphQL API](https://docs.serverless-stack.com/constructs/GraphQLApi)
-[GraphQL API](https://docs.serverless-stack.com/constructs/GraphQLApi) (optional)
-[AWS AppSync](https://docs.aws.amazon.com/appsync/latest/devguide/what-is-appsync.html) - serverless GraphQL AWS service
-[Code generation](https://www.graphql-code-generator.com/) of [TypeScript](https://www.graphql-code-generator.com/docs/guides/front-end-typescript-only) + [react client](https://the-guild.dev/graphql/codegen/docs/guides/react-vue)
- 🌐 [REST API gateway](https://docs.serverless-stack.com/api)
- 🖥 [NextJS](https://nextjs.org/) frontend w/ Material-UI
- 🎨 [Material-UI](https://mui.com/material-ui/getting-started/overview/) - react components and styling solution
- 🖥 [NextJS](https://nextjs.org/) nextjs app router
- 🎨 [Tailwind](https://tailwindcss.com/) - for styling
- 🔓 [NextAuth.js](https://next-auth.js.org/) - authentication and session management
- 🔓 [AWS Cognito](https://aws.amazon.com/cognito/) - authentication backend
- 💾 [Prisma ORM](https://www.prisma.io/docs/)
- 📚 Prisma engine lambda layer
- 📜 Prisma DB migration CDK script
- 🐳 Database integration test setup with postgres in docker
- 🔋 [Aurora Serverless RDS](https://aws.amazon.com/rds/aurora/serverless/) PostgreSQL
- 🔋 [Aurora Serverless v2 RDS](https://aws.amazon.com/rds/aurora/serverless/) PostgreSQL
- ⚡️ [Live local lambda development](https://docs.serverless-stack.com/live-lambda-development) (`pnpm dev`)
- 🐞 [Lambda debugging](https://docs.sst.dev/live-lambda-development#debugging-with-visual-studio-code) - set breakpoints on your lambda functions and step through in your IDE
- 📦 [pnpm](https://pnpm.io/) - fast and correct package manager
Expand Down
10 changes: 5 additions & 5 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"migrate": "pnpm run migrate:dev",
"migrate:dev": "pnpm run format && prisma migrate dev",
"migrate:test": "pnpm init:db:test && dotenv -e .env.vitest -- prisma migrate dev --skip-seed --skip-generate",
"init:db:test": "[ -n \"$CI\" ] || (docker-compose exec -T sst-local-db dropdb -U postgres --if-exists tests && docker-compose exec -T sst-local-db createdb -U postgres tests)",
"db:seed": "prisma db seed",
"db:seed:clean": "CLEAN_SEED=true prisma db seed",
"init:db:test": "[ -n \"$CI\" ] || (docker-compose exec -T database dropdb -U postgres --if-exists tests && docker-compose exec -T database createdb -U postgres tests)",
"seed": "prisma db seed",
"seed:clean": "CLEAN_SEED=true prisma db seed",
"generate": "prisma generate"
},
"prisma": {
Expand All @@ -26,7 +26,7 @@
"@aws-sdk/client-kms": "~3.145.0",
"@aws-sdk/client-secrets-manager": "~3.145.0",
"@aws-sdk/signature-v4-crt": "^3.130.0",
"@middy/core": "^2.5.7",
"@middy/core": "^5.4.2",
"@prisma/client": "5.16.1",
"@prisma/internals": "5.16.1",
"@prisma/migrate": "5.16.1",
Expand All @@ -38,7 +38,7 @@
"memoizee": "^0.4.15"
},
"devDependencies": {
"@chax-at/transactional-prisma-testing": "^0.5.0",
"@chax-at/transactional-prisma-testing": "^1.2.0",
"@faker-js/faker": "^7.5.0",
"@types/aws-lambda": "^8.10.70",
"@types/memoizee": "^0.4.7",
Expand Down
2 changes: 1 addition & 1 deletion backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

generator client {
provider = "prisma-client-js"
previewFeatures = []
previewFeatures = ["postgresqlExtensions"]
binaryTargets = ["native"]
}

Expand Down
3 changes: 1 addition & 2 deletions backend/seed-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ set -euxo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR/.."

pnpm exec sst load-config -- \
pnpm tsx backend/src/db/seed/seedScript.ts
pnpm tsx backend/src/db/seed/seedScript.ts
24 changes: 10 additions & 14 deletions backend/src/api/resolver/greeting.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { testCallResolver } from '../../util/testResolver';
import { GREETING } from './greeting';

const { getGreeting, greetInner } = await import('./greeting');
import { testCallResolver } from '../../util/testResolver'
import { getGreeting, GREETING, greetInner } from './greeting'

describe('Greeting resolvers', () => {
it('gets current greeting', async () => {
const greeting = await testCallResolver({
args: {},
resolverFunc: getGreeting,
});
expect(greeting.currentGreeting).toBe(GREETING);
});
const greeting = getGreeting()
expect(greeting.currentGreeting).toBe(GREETING)
})

it('greets user by name', async () => {
const greeting = await testCallResolver({
userName: 'cognitoUsername',
args: { name: 'Lebowski' },
resolverFunc: greetInner,
});
expect(greeting.greeting).toBe(`${GREETING}, Lebowski!`);
});
});
})
expect(greeting.greeting).toBe(`${GREETING}, Lebowski!`)
})
})
28 changes: 14 additions & 14 deletions backend/src/api/resolver/greeting.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { defaultAppSyncMiddleware } from '@backend/middleware/lambda';
import { logger } from '@backend/util/logger';
import { incrementMetric } from '@backend/util/metrics';
import { AppSyncResolverEvent } from 'aws-lambda';
import { GQL } from '@common/index';
import { defaultAppSyncMiddleware } from '@backend/middleware/lambda'
import { logger } from '@backend/util/logger'
import { incrementMetric } from '@backend/util/metrics'
import { AppSyncResolverEvent } from 'aws-lambda'
import { GreetingResponse, GreetingState, MutationGreetArgs } from '@common/generated/graphql/graphql'

export const GREETING = 'Yo yo';
export const GREETING = 'Yo yo'

// sample query
export const getGreeting = (): GQL.GreetingState => ({
export const getGreeting = (): GreetingState => ({
currentGreeting: GREETING,
});
})

// sample mutation
export const greetInner = async ({
arguments: { name },
}: AppSyncResolverEvent<GQL.MutationGreetArgs>): Promise<GQL.GreetingResponse> => {
incrementMetric('SaidHello');
logger.debug('Saying greeting to', { name });
}: AppSyncResolverEvent<MutationGreetArgs>): Promise<GreetingResponse> => {
incrementMetric('SaidHello')
logger.debug('Saying greeting to', { name })

return {
greeting: `${GREETING}, ${name}!`,
};
};
export const greet = defaultAppSyncMiddleware()(greetInner);
}
}
export const greet = defaultAppSyncMiddleware()(greetInner)
Loading

0 comments on commit 66753eb

Please sign in to comment.