Giter Site home page Giter Site logo

devdeno / fullstack-graphql-app Goto Github PK

View Code? Open in Web Editor NEW

This project forked from taneba/fullstack-graphql-app

0.0 0.0 0.0 4.02 MB

An opinionated fullstack GraphQL monorepo boilerplate using pnpm, Turborepo, Prisma, GraphQL Yoga 2, Fastify, Nextjs, urql, and React

JavaScript 7.40% TypeScript 92.49% CSS 0.11%

fullstack-graphql-app's Introduction

fullstack-graphql-app

An opinionated fullstack GraphQL monorepo Boilerplate using modern tech stack.

Concepts

🛡Type-safe

  • graphql-code-generator
  • prisma
  • ts-pattern (for type-safe error handling)
  • and I code in a type-safe way and don't choose a library that doesn't have a good TypeScript support.

🛠Customizable

  • envelop (plugin system for GraphQL)
  • urql (highty customizable GraphQL Client)

📈Simple but scalable

Ever evolving

  • I constantly maintain this repository.
  • TypeScript, React, GraphQL are ever evolving world too.
  • In that sense, any tech stack might be replaced in the future.

Tech Stack

Common

  • TypeScript
  • Turborepo
  • pnpm
  • GraphQL
  • graphql-codegen

Backend

  • Prisma
  • fastify
  • envelop
  • graphql-yoga 2.0
  • graphiql
  • ts-pattern
  • mysql

Frontend

  • React
  • Next.js
  • urql
  • tailwindcss
  • radix ui
  • shadcn/ui
  • React Hook Form

Authentication

  • Auth0

Testing

  • testing-library
  • jest
  • msw

Getting Started

Setting up auth0

As this project uses auth0 for authentication, you need to setup auth0 to make everything work. If you don't have any auth0 account, then sign up and create account.

You need to create API, Application (Single Page Application) in the auth0 console. In Application, go to Application URIs section in the middle of the settings page and specify http://localhost:3000 to Allowed Callback URLs, Allowed Logout URLs, Allowed Web Origins, Allowed Origins (CORS).

Once you have set up API and Application, collect credentials below which will be used in your application:

  • Client Id: Your Auth0 application's Client ID. can be found on the Application setting page.
  • Domain: Your Auth0 application's Domain. can be found on the Application setting page.
  • Audience: API Identifier for an access token. can be found on the API setting page.

Add roles to a user using Rules

You can manage role-based authorization with auth0 Rules, which is a mechanism that allows us to run some code when user register an account on auth0.

To do so, got to the Auth0 dashboard and create a new Rule (which can be found in Auth Pipeline on the side menu). Then fill the field with example code (Note that you should specify your own audience to namespace e.g. const namespace = 'https://api.fullstack-graphql-app.com'). And finally name the rule whatever you like.

After attached the rule, auth0 now add role to the user after a user registered the account on auth0.

const { loginWithRedirect } = useAuth0()

// Here "ADMIN" will be sent to auth0 and the rule put the role to jwt token.
loginWithRedirect({
  role: 'ADMIN',
})

Configure environment variables

In the root directory, Specify .env and .env.localhost file with the following environment variables:

.env

DATABASE_URL="mysql://fga:fga@db:3306/fga"
AUTH0_CLIENT_ID=<Client Id>
AUTH0_DOMAIN=<Domain>
AUTH0_AUDIENCE=<Audience>
GRAPHQL_END_POINT=http://localhost:5001/graphql

.env.localhost

DATABASE_URL="mysql://fga:fga@localhost:3306/fga"
AUTH0_CLIENT_ID=<Client Id>
AUTH0_DOMAIN=<Domain>
AUTH0_AUDIENCE=<Audience>
GRAPHQL_END_POINT=http://localhost:5001/graphql

And in the frontend root directory, Specify .env.local file with the following environment variables:

NEXT_PUBLIC_AUTH0_CLIENT_ID=<Client Id>
NEXT_PUBLIC_AUTH0_DOMAIN=<Domain>
NEXT_PUBLIC_AUTH0_AUDIENCE=<Audience>
NEXT_PUBLIC_GRAPHQL_END_POINT=http://localhost:5001/graphql

Backend

install deps

pnpm install

start server

docker-compose up

Graphql Playground will start on localhost:5001

scripts

The scripts you might frequently use:

  • db:migration:generate: Generates migration file (not apply automatically). Run this whenever you change your database schema.
  • db:migration:run: Runs generated migration file. Run this after you generate the migration file.
  • prisma-gen: Generates the Prisma client
  • prisma-studio: Starts Prisma Studio on localhost:5555 where you can inspect your local development database.

connect to your mysql database

 docker exec -it backend_db_1 mysql -u root -p

 mysql> use fga

Frontend

start

Run the command below. which uses turborepo cli internally and it runs shared ui package's watch script as well.

pnpm run dev

workflow

Update GraphQL API

1. Edit Schema file

path: apps/backend/src/api/graphql/typeDefs.ts

2. run graphql-codegen to generate files based on the latest schema

pnpm run generate:graphql

this will run turbo run codgen:graphql, which runs all codegen:graphql in each package.

fullstack-graphql-app's People

Contributors

renovate[bot] avatar renovate-bot avatar taneba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.