Learning the bits about Express/GraphQL via a course referencing a local json db with json-server
to get started
npm install
then
npm run dev
to create an running nodemon graphql instance and then open http://localhost:4000/graphql
and to inspect the RESTFUL Json Server at http://localhost:3000/user or http://localhost:3000/companies
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
}
}
Same goes for adding companies
mutation {
addCompany(id: "idOfCompany", name: "nameOfCompany") {
id
}
}
Editing companies
mutation {
editCompany(id: "idOfCompany", description: "making soccer balls") {
id
}
}
Deleting company
mutation {
deleteCompany(id: "idOfCompany") {
id
}
}
Alot of these tips follow the Restful Conventions For more read the schema/schema.js
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
Hence why using GraphQL perhaps can be a benefit