GraphQL backend for an imaginary chocolate shop, written with Rust, Rocket, SQLx and Postgres

Roman Tolmachev
4 min readAug 11, 2021

--

There are many ways of building a sound backend nowadays, hundreds of tools and paradigms. Alongside the traditional REST architecture, GraphQL has become a quite popular alternative. It addresses an issue of over and under fetching and allows you to tailor the data you interchange with the server, combining multiple objects with the fields you specify in one request.
Besides, many GraphQL libraries allow for client code generation, both for schemas and operations. We’ll see it later in the second part when building a companion Android app. In short, using GraphQL can bring clarity and speed of development, especially in situations when you have complicated data relations, and making dozens of REST requests quickly becomes clumsy and inefficient. I invite you to read more in this excellent comparison between GraphQL and REST.

In this article we’ll see an example of GraphQL server implementation using Rust programming language and popular libraries (aka crates) such as async-graphql, sqlx, rocket and Postgres database running locally. After trying different approaches, I found an excellent repository by lionkeng. We’ll use it as a bootstrap project.

TLDR: See the complete code at tolmachevroman/chocolate-shop

Second part: building a companion Android app

Imagine we have a cozy chocolate shop and we want to manage its content via a web interface and a mobile app. GraphQL is all about types, their relations and operations on them. A good way to start is to define a Schema:

One interesting note here. We could’ve defined fillings to be a list of enum as well, but there’s one caveat. Let’s say you have ten different fillings: raisins, cocoa, pistachio etc. And then you add a new one. Then, depending on the particular client implementation, you could get away with it relatively easy or it could throw a runtime error and ruin the whole query parsing. In case of Android you’ll have to recompile the client to update the enum. Using Strings makes things easier I guess.

A chocolate item looks something like this:

Rust implementations of GraphQL, including the async-graphql, all use the code-first approach compared to schema-first. In practical terms it means we derive the schema from the code, not the other way around. Huge advantage of this is the consistency of code and schema, especially valuable for big and fast-changing projects. Let’s take a look at how to translate that Schema above to the code:

Let’s see how to setup the local Postgres server and connect it with our Rust project. Make sure you have Postgres up and running on your machine. Go ahead and create a database chocolate_shop. Then, put the database url in a .env file:

Next, go to SQLx CLI and install the Postgres-only CLI. Follow the instructions and add the first migration to create products table:

Run a migration and check the database to make sure that you got the table created. You should have something similar to this:

We want to fetch products and modify them, which are Query and Mutation operations in the GraphQL jargon. These implementations contain the core CRUD logic connecting your Schema with the underlying database:

Finally, let’s combine the whole thing in the main file:

At this point we have everything in order. Go ahead and cargo run to start the server. Navigate to http://localhost:7600/ and you’ll see a nice GraphQL playground interface:

It allows you to export the Schema, which will come in handy when we’ll be building an Android app using Apollo client.

Congratulations, you’ve built a simple but powerful GraphQL web server with Rust running a Postgres database! I strongly recommend cloning and running the Github project to get a better understanding of concepts presented above. Running code worth a thousand words, they say.

See you in the next part!

--

--