Skip to content

A lesson using express and graphql for managing an external db example

Notifications You must be signed in to change notification settings

Loonz806/express-graphql

Repository files navigation

Github Actions CI Commitizen friendly code style: prettier JavaScript Style Guide

Express-GraphQL

Learning the bits about Express/GraphQL via a course

to get started

npm install

then make a .env file at the root of the project and add the following, or whatever port you would want

PORT=4000

then to start the fake server via json-server package as an outside integration, on http://localhost:3000

npm run json:server

then to create a running nodemon graphQL instance you can run

npm run start

or generate the combination by running

npm run dev

Things to do

Using the http://localhost:4000/graphql you can add a query a user by querying on the left panel In this case user with id of 23 is in the db.json ( you can remove or add fields on this person )

{
  user (id:"23") {
    id
    firstName
    age
    company {
      name
      description
      id
    }
  }
}

Or add users by leveraging a mutation

mutation {
  addUser(firstName:"Bobby", age:58) {
    id,
    age,
    firstName
  }
}

Or edit that user

mutation {
  editUser(id: <id in db.json>, companyId: "2") {
    id,
    firstName,
    age
  }
}

Or even delete that user

mutation {
  deleteUser(id: <id in db.json>) {
    id
  }
}

Alot of these tips follow the Restful Conventions For more read the schema/schema.js

Restful Conventions / Routing Examples

restful meaning readable methods that you can assert actions on

  • /name POST Create a record
  • /name GET Fetch all records
  • /name/:id GET Fetch record with given id
  • /name/:id PUT Update details of user with id
  • /name/:id DELETE Delete user with id

things can get complicated the more deeper and nested as you go

  • /users/23/posts POST Create a post associated with user 23
  • /users/23/posts GET Fetch all posts created by user 23
  • /users/23/posts/14 GET Fetch post 14 by user 23
  • /users/23/posts/15 PUT Update post 15 by user 23
  • /users/23/posts/18 DELETE Delete post 18 created by user 23

However these breakdown once we get into lots of http requests or super nested data

Hence why using GraphQL perhaps can be a benefit

About

A lesson using express and graphql for managing an external db example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages