Thursday, May 27, 2021

Dynamic data using a fake api server linked to a json file

For adding dynamic data to graphql application, we can use a fake api server for development.


To setup a fake api server: https://github.com/typicode/json-server

npm i --save json-server

//db.json

{
 "users":[
  {"id": "23", "firstName": "Bill", "age":20},
  {"id": "40", "firstName": "Alex", "age":40}
]  
}

Inside the package.json add the below script.
//package.json
"scripts":{
  "json:server":"json-server --watch db.json"
}

Inside terminal: npm run json:server

it generates an url in the output, like: http://localhost:3000

sample query: http://localhost:3000/users/23


No comments:

Post a Comment