Giter VIP home page Giter VIP logo

koa-graphql-boilerplate's Introduction

Simple Koa - GraphQL - Mongoose ES6 Boilerplate

Setting up

Use node >= 8.10.0

Clone this repo and change details in package.json. Remove the .git directory and git init to start fresh.

Read below for more!

Modules used

  • graphql
  • koa-graphql
  • @koa-cors
  • dotenv
  • jsonwebtoken
  • koa-jwt
  • koa-router
  • mongoose

Directory Structure and important files

  • auth - Auth related helper methods
  • database - keeps models for mongoose into a separate mongo directory
  • graphql - all the graphql related files stay here
  • services - used to make more precise and compact services for busines logic (called from graphql resolvers)
  • .env - stores all the environment related config
  • app.js - initializes the database layer, middlewares, graphql and starts the server

Some important info

  • mongoose models are defined in separate files under mongoose directory and are automatically populated and available in ctx.db on every koa request. For eg. ctx.db.User

  • graphql directory has been broken down into

    • mutations - keeps all the mutations that you want (See sample mutations)
    • resolvers - keeps all the query resolvers that you want (See sample resolver)
    • types - keeps all the custom graphql types
  • .env file need not to be checked into git and its best to keep it separate on each environment rather making .env.dev, .env.prod, don't do that.

  • Authentication - Authentication is being handled by json web token, check UserSignupMutation.js for more info on how to generate jwt tokens and send to client all in graphql

    • To enable auth for a particular resolver or mutation you just need to pass true while initializing in schema.js (check example)
    • For API, auth token needs to be sent into Authorization header of each request
    • For graphiql, token is picked up from cookie that is set when generating token the first time
  • All resolvers and mutation extends the BaseResolver which does things like handling resolver level auth check

How to run

  • Clone the project
  • Run npm install
  • node server.js
  • That's it!

Future additions

  • Add a simple logger capabale of handling different log levels

Contribution

  • Fork and make pull requests please!
  • Report any bugs and improvements.

Author

Suroor Wijdan @suroorwijdan

License

BSD-3-Clause

koa-graphql-boilerplate's People

Contributors

suroorwijdan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.