API in Rust
Creating a GraphQL and MongoDB web API in Rust.
During my 2 years of self study between 2017-2019, I spent a few months studying MongoDB. At that time, it was recommended as a good technology for the web. I knew some basic SQL, and I spent some time with SQLite in a Python project tutorial during my study, but MongoDB was a database that I really put in some effort and finished the free certificates that they have on their official website. I have to say that I like MongoDB, and I was lucky to find my current company starting 2019, where MongoDB is used exclusively.
Most of my work is focused on the backend of a big portal website for users and admins. Specifically, the creation and maintenance of APIs. Written in TypeScrip, stored in MongoDB and served with GraphQL. GraphQL was new to me when I started in the company, but once I got used to it, serving data via REST feels like some totally antiquated. Of course, we also use REST for necessary things, but the type safety of GraphQL and the schemas is something amazing.
First I read “The book” from the official website. The book took a while, but it is very good. I prefer taking my time while studying anything and go step by step a bit every day. So I didn’t mind the length of the book. After maybe a few months, I started with tutorials on Exercism. It is an amazing website and I might make an article about it exclusively in the future.
After a few weeks / months of tutorials, I felt the need to try something personal. It doesn’t need to be something production-ready, I just wanted to try to put together something new. I soon began thinking if my favorite MongoDB + GraphQL could work with Rust. I found an article from 2018 that looked a little scary, but when reading the docs of the GraphQL library for Rust, I realized 2018 might be already old information.
I made the simple web API by the end of 2019. I will not be talking about the technical specifics of the project and its libraries. Under the link above, in the project’s readme.md, you can find the technical details. I also left plenty of comments in the code itself for me or anybody else if you are interested. But below are some memories that I have of the experience writing it.
Rust with MongoDB
Rust with GraphQL
The library for GraphQL on the other hand was surprisingly nice to use. I loved how it used macros and simple Rust structs to generate its necessary objects. It was easy to create the schemas, much nicer than I remembered from the crazy 2018 article. I remember being amazed that I could easily recommend this, even though I’m not a programming veteran, and I am writing this in Rust, supposedly one of the fastest and most powerful languages out there.
So, I was very happy when this was running. Later I changed the module tree and I implemented logging. Both simply for experience and making the project feel a little more complete, even though this was basically more like a proof of concept. A challenge for myself.
Would I change it?
I imagine that in the past 7 months since then, both libraries got only better. I didn’t look at them since then. Also, I had a hard time to choose the web server library. I tried several till I stuck with Iron. Were I to do it over, at this time of writing, I would probably use Actix-web instead. But the library landscape of Rust is evolving quickly and it’s no surprise that there are many changes. I will not touch this project again. It serves as the memory of my experience from that time. My first personal github project.