The Blog-server is built node.js (typescript), Postgresql(prisma) and Graphql (apollo-server).
npm init -y
npm i validator bcryptjs
npm i -D nodemon typescript ts-node-dev @types/node @types/validator @types/bcryptjs
tsc --initPrisma is an open source next-generation ORM. It consists of the following parts:
- Prisma Client: Auto-generated and type-safe query builder for Node.js & TypeScript
- Prisma Migrate: Migration system
- Prisma Studio: GUI to view and edit data in your database
npm i prisma
npx prisma init.env :
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
prisma > schema.prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
npx prisma db push
npx prisma studioApollo Studio is a cloud platform that helps you build, validate, and secure your organization's graph. Get started here!
npm install apollo-server graphqlmutation Signup($credentials: CredentialsInput!, $name: String!, $bio: String!) {
signup(credentials: $credentials, name: $name, bio: $bio) {
userErrors {
message
}
token
}
}
mutation Signin($credentials: CredentialsInput!) {
signin(credentials: $credentials) {
token
userErrors {
message
}
}
}
mutation PostCreate($post: PostInput!) {
postCreate(post: $post) {
userErrors {
message
}
post {
id
title
content
published
createdAt
}
}
}
query Profile($userId: ID!, $take: Int!, $skip: Int!) {
profile(userId: $userId) {
id
bio
isMyProfile
user {
id
email
name
posts(take: $take, skip: $skip) {
id
title
content
published
createdAt
}
}
}
}Let's get started. https://studio.apollographql.com/sandbox/explorer
JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted.
npm i jsonwebtoken
npm i -D @types/jsonwebtokenconst token = JWT.sign({ userId: user.id }, JSON_SIGNATURE, {
expiresIn: 3600000,
}),
const userInfo= JWT.verify(token, JSON_SIGNATURE) as { userId: number };DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
npm i dataloaderexport const userLoader = new Dataloader<number, User>(batchUsers);
userLoader.load(id);