In this guide i’ll walk you through how to set up node s server frote point of view of a beginner who’s being tinkering around for the last 6 months and help you avoid the common issues i ran into in the process
I;ll assume you know the basic in javascript,typescript and graphql
But i’ll try and organise the github commits from the most basic
the typescript setup might be little cumbersome so i’d advise to clone the repo first before beginning this tutorial
Navigate to the initial commit and download or clone repo if you have git installed
You’ll also need to install and setup mongodb on your device or use mongo atlas , am not a fan of mongodb compass because of its lack of dark mode so i use the vscode extension MySQL database viewer:
our server is now ready navigate to http://localhost:4000/graphql
to preview our server in apollo's playground and run our first query
Image description
on the right hand side we have all the operatons which you can navigate to by clicking the the pluss button and adding field then run it and the response is displayed in the left hand side.
add the respective code and import it in the index.ts
The typedefs define what the data should look like and all it’s types
For example we have a custom type Item which is an object with the fields title of type Strung and desc of type String too
We also have to define the queries,mutations and subscriptions
These definitions are used to shape the data we’ll pass to and receive from our resolvers
Our resolver is comprised of a simple getItems query which returns an array of items from our mongo db
Image descriptionImage description
The addItem mutation takes title and desc and saves it to mongo then is returns an item response
Image descriptionImage description
[the docs page]:(https://www.apollographql.com/docs/) has more information for more complex mutations and queries
but if you've noticeed my code is still full of //@ts-ignore decorators because we are not using typescript it to it's fullest next time we'll set up type-graphql and typegoose for better type-checking which makes development much easier
we'll also handle delete and update in mongodb feel free to explore more till then