Install prisma, docker and docker compose
Prisma : https://www.prisma.io/
npm install -g prisma
Docker https://www.docker.com/ (refer to the docker doc for your OS)
Then run :
docker-compose up -d
yarn bootstrap
yarn seed
yarn startFor simplicity purpose a graphql playground is provided. Making an HTTP endpoint is trivial once the queries and mutations are working (http://myapi/graphql?query={me{name}}).
Playground is available at http://localhost:4000
- Prisma : Very good ORM / API layer, creates a javascript API to interact with your database using GraphQL in a very efficient and fast way
- GraphQL Yoga : Combine express, node and apollo-server to reduce boilerplate.
- Typescript : Painless use of ES6 features in node
- A lot of iteration on paper to get a good idea of how to solve the problem
- Choosing the right tool for the job (See tech choices explanation)
- Setting up prisma and the dev environment
- Creating the database schema using the notes taken at step 1
- Seeding the database and playing with the prisma client
- Creating a proper GraphQL schema
- Writing a server with the required CRUD operations for our endpoint
- Adding the requested features :
- Notification
- UpdateOrderStatus
- CreateFirstUpdate
- Cleaning up code and writing document and readme
Look at the console output from the yarn start script
A third party provider send an update to our graphql endpoint :
mutation {
createOrderUpdate(
consignmentId: 1
coordinate: [5, 6]
status: EN_ROUTE_TO_SENDER
) {
id
status
}
}The following logic happen :
- The update gets stored in the DB
- The order associated to the consignment gets updated
- A notification get sent to the user
The API gets accessed like this to create a new order
mutation {
createOrder(
userId: "Use a valid user id (output from yarn seed)"
consignmentId: 2
deliveryLocation: [0, 1]
senderLocation: [1, 2]
active: true
currentStatus: BEING_PREPARED
) {
id
active
}
}Once created it creates a new update, then send it to the user.
Here are a few examples queries to inspect the orders :
query {
activeOrders {
id
consignmentId
}
}query {
orderByConsignmentId(consignmentId: 1) {
id
user {
id
name
}
deliveryLocation {
latitute
longitude
}
senderLocation {
latitute
longitude
}
updates {
id
coordinate {
latitute
longitude
}
status
}
active
currentStatus
}
}We can leverage the full GraphQL power to query only the field we need for our front end.
A few things could be improved:
- Directory structure => As the service scale, properly structure the directory will be important.
- Extract the helper methods from index.ts, and clean up some of the code.
- Performances, schema, and query efficiency can all be improved.
- Adding middlewares and other micro-services, ejecting from graphql yoga (https://github.com/prisma/graphql-yoga) to access the underlying express instance, or switch to express + node, apollo server or equivalent. The third party provider might not want to send a graphQL HTTP request, or we might want to do some parsing and checks before writing to the DB.
- Authentification.
- Write tests to make sure everything works.