Giter VIP home page Giter VIP logo

graphql-server-typescript's Introduction

GraphQL + MongoDB express server (in Typescript)

This is just a starter example on how to build a GraphQL server with MongoDB database in Typescript.

The advanage of this solution is a strong type checking, thanks to Typescript, and a easy GraphQL schema definition using GraphQLSchema and GraphQLObjectType.

The logic for the database models are inside the src/db folder while for the GraphQL types, query and mutation are inside the src/schema folder. The schema is then built in the src/schema/index.ts file.

For the authetication the method setupPassportAuth in the file src/authetication.ts will handle all the routes and access to the user table.

More details

  • Using Express and GraphQL HTTP Server to set up a graphql server.
  • Using Passport and Passport JSON Web Token for user authentication with bcrypt for hashing the password during user creation.
  • Using Mongoose to interact with MongoDB and take advantage of easy model definitions and support for Typescript.
  • Using GraphQL.js dynamically create a GraphQLSchema with types, query and mutation.
  • Using Typescript type checking compilation for development and production.

Install

The main Express App entry point is src/server.ts, it can be launch with the following commands:

For development (with live reload)

npm start

For production (app served from dist folder)

npm run build

Usage

In this example we open a MongoDB connection on mongodb://localhost:27017 and open the heros database, so we need to make sure mongod service is running and the default port is set to 27017 (or you can adjust the parameters in the src/server.ts file).

Run the express server (with npm start or npm build).

We need then to create a user to login with, we can create an admin user by visiting the debug url http://localhost:3000/add-admin-user.

We can then login from the auth form by visiting http://localhost:3000/auth (default username: admin, password: admin).

The login page will respond with an auth token, if you use a client we can save it and set it in the headers for the following requests, if you use a Browser you can use an extension (like this for Chrome) to set the Authorization header with the token (the token need to be preceded by JWT like JWT eyJhbGcixxxxxxxxx.

Now you are ready to open the GraphQL inspector by visiting http://localhost:3000/graphql and browse the schema and do query, mutation, etc.

License

MIT

graphql-server-typescript's People

Contributors

thomasgazzoni avatar

Stargazers

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

Watchers

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