Skip to content

mehradi-github/ref-blog-server

Repository files navigation

Implementing API by Graphql

The Blog-server is built node.js (typescript), Postgresql(prisma) and Graphql (apollo-server).

Installing prerequisite

npm init -y
npm i validator bcryptjs
npm i -D nodemon typescript ts-node-dev @types/node @types/validator @types/bcryptjs
tsc --init

Installing Prisma

Prisma 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 studio

Installing Apollo server

Apollo Studio is a cloud platform that helps you build, validate, and secure your organization's graph. Get started here!

npm install apollo-server graphql
mutation 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

Installing JSON Web Token (JWT)

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/jsonwebtoken
const token = JWT.sign({ userId: user.id }, JSON_SIGNATURE, {
        expiresIn: 3600000,
      }),
const userInfo= JWT.verify(token, JSON_SIGNATURE) as { userId: number };

Installing dataloader

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 dataloader
export const userLoader = new Dataloader<number, User>(batchUsers);

userLoader.load(id);

About

The blog-server is built node.js (typescript), Postgresql (prisma) and Apollo-server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published