-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement graphql backend, automate type generation
- Loading branch information
Showing
51 changed files
with
7,632 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ yarn-error.log* | |
.pnpm-debug.log* | ||
|
||
# local env files | ||
.env | ||
.env*.local | ||
|
||
# vercel | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
overwrite: true | ||
schema: "./src/graphql/generated/schema.graphql" | ||
documents: "./src/graphql/operations/**/*.graphql" | ||
generates: | ||
./src/graphql/generated/codegen.ts: | ||
plugins: | ||
- "typescript" | ||
- "typescript-operations" | ||
- "typescript-react-apollo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
-- CreateTable | ||
CREATE TABLE "users" ( | ||
"id" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"firstName" TEXT NOT NULL, | ||
"lastName" TEXT NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"password" TEXT NOT NULL, | ||
|
||
CONSTRAINT "users_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "events" ( | ||
"id" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"slug" TEXT NOT NULL, | ||
"venue" TEXT NOT NULL, | ||
"address" TEXT NOT NULL, | ||
"performers" TEXT[], | ||
"date" TEXT NOT NULL, | ||
"time" TEXT NOT NULL, | ||
"image" TEXT, | ||
"userId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "events_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "events_slug_key" ON "events"("slug"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "events" ADD CONSTRAINT "events_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20220901170407_update_events_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `description` to the `events` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "events" ADD COLUMN "description" TEXT NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id String @id @default(cuid()) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
firstName String | ||
lastName String | ||
email String @unique | ||
password String | ||
events Event[] | ||
@@map("users") | ||
} | ||
|
||
model Event { | ||
id String @id @default(cuid()) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
name String | ||
slug String @unique | ||
venue String | ||
address String | ||
performers String[] | ||
date String | ||
time String | ||
description String | ||
image String? | ||
userId String | ||
user User @relation(fields: [userId], references: [id], onDelete: Cascade) | ||
@@map("events") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
import { events } from '../src/utils/data.json'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export async function main() { | ||
try { | ||
console.log(`Start seeding ...`); | ||
await prisma.event.createMany({ data: events }); | ||
console.log(`Seeding finished.`); | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} finally { | ||
await prisma.$disconnect(); | ||
} | ||
} | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { useMemo } from 'react'; | ||
import { | ||
ApolloClient, | ||
HttpLink, | ||
InMemoryCache, | ||
NormalizedCacheObject, | ||
} from '@apollo/client'; | ||
|
||
let apolloClient: ApolloClient<NormalizedCacheObject> | undefined; | ||
|
||
export type ResolverContext = { | ||
req?: IncomingMessage; | ||
res?: ServerResponse; | ||
}; | ||
|
||
function createIsomorphLink(context: ResolverContext = {}) { | ||
if (typeof window === 'undefined') { | ||
const { SchemaLink } = require('@apollo/client/link/schema'); | ||
const { schema } = require('./schema'); | ||
|
||
return new SchemaLink({ schema, context }); | ||
} else { | ||
const { HttpLink } = require('@apollo/client'); | ||
return new HttpLink({ | ||
uri: '/api/graphql', | ||
credentials: 'same-origin', | ||
}); | ||
} | ||
} | ||
|
||
function createApolloClient(context?: ResolverContext) { | ||
return new ApolloClient({ | ||
ssrMode: typeof window === 'undefined', | ||
link: new HttpLink({ | ||
uri: 'http://localhost:3000/api/graphql', | ||
credentials: 'same-origin', | ||
}), | ||
cache: new InMemoryCache(), | ||
}); | ||
} | ||
|
||
export function initializeApollo( | ||
initialState: any = null, | ||
// Pages with Next.js data fetching methods, like `getStaticProps`, can send | ||
// a custom context which will be used by `SchemaLink` to server render pages | ||
context?: ResolverContext, | ||
) { | ||
const _apolloClient = apolloClient ?? createApolloClient(context); | ||
|
||
// If your page has Next.js data fetching methods that use Apollo Client, the initial state | ||
// get hydrated here | ||
if (initialState) { | ||
_apolloClient.cache.restore(initialState); | ||
} | ||
// For SSG and SSR always create a new Apollo Client | ||
if (typeof window === 'undefined') return _apolloClient; | ||
// Create the Apollo Client once in the client | ||
if (!apolloClient) apolloClient = _apolloClient; | ||
|
||
return _apolloClient; | ||
} | ||
|
||
export function useApollo(initialState: any) { | ||
const store = useMemo(() => initializeApollo(initialState), [initialState]); | ||
return store; | ||
} |
Oops, something went wrong.