API in Rust

Creating a GraphQL and MongoDB web API in Rust.

DB experience

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.

Day job

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.

Rust

During 2019, I wanted to learn a new programming language. Most of my time 2017-2019 I spent learning JavaScript. I spent a some time on C and Python too. I like python, but I already did a small project with it, some intermediate tutorials, and although I definitely could learn much more in Python, It didn’t really feel challenging enough and I wanted something that would push my knowledge as a developer. C was interesting, but way too hard for me to spend too much time on it. I wanted something practical and modern. The purpose was to learn to be a better programmer, but I felt like I would spend too much time on suffering while trying to build something in C. A colleague at work started to learn GOlang a little. I felt tempted to read up on it a little and stumbled upon this wave of new programming languages growing in 2019 like go, rust or julia. Julia felt too esoteric to me and GO somehow too popular to be cool. But when I was reading about Rust and their attempt to make memory management like in C accessible and safe for even beginner programmers, I was very intrigued. I don’t regret my choice in Rust a bit. It was exactly what I was looking for. Something more challenging on a technical level, but also holding your hand in the process, making for a great learning experience.

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.

Project

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

The library for MongoDB could have been better. I remember thinking that using MongoDB in TypeScript with Mongoose is honestly a much better experience. In rust, the objects have to be translated through a parsing library like Serde. But I guess this is not an issue exclusive to Rust, more like a benefit when using JavaScript with JSON. It was not trivial, but possible!

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.

Final thoughts

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.

Radim Janoš
Radim Janoš
Developer

Related