GraphQL server demo via express and express-graphql

rezahazegh - Jan 15 '23 - - Dev Community

The following repository demonstrates a simple GraphQL server app via express and express-graphql.
https://github.com/rezahazegh/demo-express-graphql

Project Description

This project contains Course and Teacher entities, It provides create, update and delete for them.

Entities relation: Every course would have a teacher.

Directory Description

  • models: It includes required mongoose models to store entities in MongoDB
  • schema: it include GraphQL related stuff
    • types: It includes types, something like dto in REST API
    • resolvers: It include resolvers, something like route handlers in REST API

Project Setup

Run docker compose up -d to setup MongoDB via docker

Run app and open localhost:3000/graphql to reach out to GraphiQL (An in-browser tool for writing, validating, and testing GraphQL queries)

Sample Requests

Create, Read and Delete Teacher

mutation {
  addTeacher(name: "Peter Parker", email: "spiderman@gmail.com", phone: "855-465-2386") {
    id
    name
    email
    phone
  }
}
Enter fullscreen mode Exit fullscreen mode
{
  teachers {
    id,
    name
  }
}
Enter fullscreen mode Exit fullscreen mode
{
  teacher(id: "63c421e024a2dcb1e5dcdce6") {
    id,
    name
  }
}
Enter fullscreen mode Exit fullscreen mode
mutation {
  deleteTeacher(id: "63c421e024a2dcb1e5dcdce6") {
    id,
    name
  }
}
Enter fullscreen mode Exit fullscreen mode

Create, Read, Update and Delete Course

mutation {
  addCourse(name: "GraphQL by Spider-Man", description: "This is the project description", status: new, teacherId: "63c424b724a2dcb1e5dcdcef") {
   name
   description
  }
}
Enter fullscreen mode Exit fullscreen mode
{
  courses {
    id,
    name,
    status
  }
}
Enter fullscreen mode Exit fullscreen mode
{
  course(id: "63c4252c24a2dcb1e5dcdcf1") {
    id,
    name,
    status
  }
}
Enter fullscreen mode Exit fullscreen mode
mutation {
  updateCourse(id: "63c4252c24a2dcb1e5dcdcf1", status: completed) {
   name
   status
  }
}
Enter fullscreen mode Exit fullscreen mode
mutation {
  deleteCourse(id: "63c4252c24a2dcb1e5dcdcf1") {
    id
  }
}
Enter fullscreen mode Exit fullscreen mode
. . . . .
Terabox Video Player