GithubHelp home page GithubHelp logo

wavymav / boilerplate-nexus-prisma-apollo-graphql-express Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arbytez/boilerplate-nexus-prisma-apollo-graphql-express

0.0 1.0 0.0 298 KB

Boilerplate project for a graphql server using nexus-prisma and apollo-server-express

License: MIT License

JavaScript 6.17% TypeScript 92.99% Shell 0.84%

boilerplate-nexus-prisma-apollo-graphql-express's Introduction

✨ Boilerplate project for nexus-prisma apollo-express-graphql backend server

⚠️ Disclaimer: the project uses the prisma framework that is not ready for production yet. See isprisma2ready.com.

Code written using nodejs version LTS >= 12.x.x.

This project wants to be a starting point for more complex backend projects using graphql. Of his own, it manages the backend for a simple todo application but it handles important other topics besides the basic crud api for todos.

Getting started

The project uses nps (npm-package-scripts) so in the file package.json there are only 3 scripts, one for each main environment, and the scripts logic inside package-scripts.js:

"scripts": {
    "dev": "cross-env NODE_ENV=development nps",
    "prod": "cross-env NODE_ENV=production nps",
    "test": "cross-env NODE_ENV=test nps"
  },
npm i                                       # install dependencies packages (there are a few 😧)
npm run <dev|test|prod> generate            # generate prisma-client-js library and typescript definitions for nexus-prisma. Before doing it, set the environment variables needed
npm run dev                                 # start development server
npm run test                                # start jest tests suite
npm run prod                                # generate, build and start the production server

npm run <dev|test|prod> prisma.migrate         # create migration
npm run <dev|test|prod> prisma.migrate.up      # apply migration
npm run <dev|test|prod> prisma.migrate.down    # rollback migration

npm run dev generate.nexus                  # useful to generate only the nexus typescript definitions while implementing new features

Environment variables

Check file environment.example.env or test.example.env, then create for each environment the relative env file with the env var wanted.

config/development.env                      # for development
config/test.env                             # for test
config/production.env                       # for production
  • COOKIE_SECRET cookie secret for signed cookies
  • FRONTEND_URLS list of urls separated by a comma for cors options
  • JWT_SECRET jsonwebtoken secret for auth operations
  • JWT_TOKEN_EXPIRES_SEC_IN amount of seconds that the generated jwt token will be valid
  • PORT server port, default 4000
  • DATABASE_URL database (postgres) db access url
  • REDIS_HOST redis db hostname, used for graphql subscriptions
  • REDIS_PASSWORD redis db password
  • REDIS_PORT redis db port, default 6379
  • WRITE_CUSTOM_LOG_TO_FILE yes/no, write log only to console or console and filesystem

Graphql server security

Tests (Broken [todo])

> ⚠️ with the latest versions the property enabled on datasource section on schema.prisma is no longer available and since the test environment changes the db source dynamically for every test cases, for the moment, it is not possible to run the test suite. Although this feature is implemented (see here prisma/prisma#1487) the test suite is still broken because it uses the sqlite provider with an enum for the roles but sqlite does not support enum types. It is needed to switch the test db provider to postgres.

Before run npm run test, set the config/test.env file and run npm run test prisma.migrate.up (check prisma docs to see the correct flow to follow). This command will set your sqlite db on /src/tests/db/data.sqlite. This db should not contain data and schema must be updated (run prisma.migrate.up in test env every time the schema.prisma is updated).

Each test suite will do (see testUtils.ts):

  1. a copy of the data.sqlite into the /src/tests/db/generated folder
  2. start the backend server, the port is choosen using arbitrary-unused-port
  3. seed the copied db with 'dummy' data that test will use it.
  4. run tests
  5. clean test environment (close backend server, Prisma-Client connection and delete the copied sqlite db)

Todo

  • test graphql subscriptions
  • optimize tests flow (tests run need a lot of pc resources to bootstrap the server for each test suite and compilation from typescript)
  • upload file endpoint (not needed but nice to have into the boilerplate project)
  • process manager in production (npm run prod will start the api with pm2 in cluster mode)
  • use some kind of autogenerated documentation tool system and publish it to GET / of the backend server. Used https://github.com/2fd/graphdoc.

Other

The backend is ready to be used with CapRover, indeed captain-definition file exist on the root project (see also dockerfile).

One instance of the backend project is up and running on: https://nexus-prisma-boilerplate.epbe.dynu.net/graphql (auto-generated docs: https://nexus-prisma-boilerplate.epbe.dynu.net/). The FRONTEND_URLS env var is set to http://127.0.0.1:3000,http://localhost:3000 so it is possible to play with the backend with a local frontend environment (like nextjs).

boilerplate-nexus-prisma-apollo-graphql-express's People

Contributors

arbytez avatar

Watchers

 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.