• Stars
    star
    246
  • Rank 164,726 (Top 4 %)
  • Language
    Rust
  • Created almost 5 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An example GraphQL server written in Rust

Rust + GraphQL + Juniper + Diesel + Postgres + Actix

Yes, I know that this is a borderline absurd web stack for the ubiquitous TODO application but I had a lot of trouble getting this all to work. I started using these things for a more ambitious project and I'd love to spare you the trouble. So here's some basic boilerplate to get you up and running.

Components

Here's what does what:

Component Tool/lib
Web server actix-web
Database PostgreSQL
SQL engine Diesel
GraphQL library Juniper
GraphQL UI GraphQL Playground

Run locally

Before you get started, make sure that you have PostgreSQL, Rust, Cargo, and the Diesel CLI installed and that you have Postgres running somewhere.

# Fetch the repo
git clone https://github.com/lucperkins/rust-actix-diesel-postgres-juniper
cd rust-actix-diesel-postgres-juniper

# Set up the database
cp .env.example .env # Modify this file to match your Postgres installation

diesel setup
diesel migration run

cargo run # could take a while!

The DATABASE_URL can be any Postgres installation. For my purposes, I have it set to postgres://localhost:5432/todos.

Once the server is running, you can access the GraphQL Playground UI at http://localhost:4000/graphql.

Schema

The server implements the following GraphQL schema:

type Todo {
  id: ID!
  task: String!
  done: Boolean!
}

input CreateTodoInput {
  task: String!
  done: Boolean
}

type Query {
  allTodos: [Todo!]!
  getTodoById(id: Int): Todo
}

type Mutation {
  createTodo(input: CreateTodoInput): Todo
  markTodoAsDone(id: Int): Todo
  markTodoAsNotDone(id: Int): Todo
}

schema {
  Query
  Mutation
}

Tour of the codebase

File What it provides
context.rs The GraphQL context that handles query execution
data.rs A Todos struct and some helper functions encapsulate the Diesel-powered Postgres querying logic
db.rs The connection pool that handles the Postgres connection
endpoints.rs The /graphql HTTP endpoint that makes GraphQL and the GraphQL Playground work
graphql.rs The Query, Mutation, and Schema objects that undergird the GraphQL interface
lib.rs Just the standard lib.rs
main.rs Actix HTTP server setup
models.rs All of the data types used for querying Postgres and providing GraphQL results
schema.rs The Diesel-generated table schema

Future TODOs

Get it? Anyway, here's some areas for improvement (pull requests very much welcome):

  • Error handling — Right now errors basically propagate directly from Diesel/Postgres into the GraphQL JSON output, which is subpar. If any of you can point me to good educational resources on this, please file an issue!
  • Better execution engine — The server uses the extremely powerful actix-web but the actual DB interactions don't use Actix actors and it'd take this setup to the next level if they did.
  • Use macros for schema generation — The powerful juniper_from_schema macro could help reduce boilerplate and improve development velocity.

Acknowledgments

I'm basically a beginner with Rust and would not have been able to put this together without peeking long and hard at the example projects and blog posts listed below. The lower-level bits you see here are basically stolen from BrendanBall. All that I've added is the Todos data construct for executing queries.

Example projects

Blog posts

More Repositories

1

colossus

Colossus — An example microservice architecture for Kubernetes using Bazel, Go, Java, Docker, Kubernetes, Minikube, Gazelle, gRPC, Prometheus, Grafana, and more
Starlark
1,011
star
2

rek

An easy HTTP client for Go. Inspired by the immortal Requests.
Go
381
star
3

spray-akka-slick-postgres

Spray + Akka + Slick + Postgres (simple web service)
Scala
88
star
4

bulma-dashboard

Bulma dashboard
Sass
85
star
5

nix-monorepo

An illustration of how you might use Nix in a large, multi-language project and in accordance with best practices
Elixir
53
star
6

nix-home-config

Nix configuration for my macOS laptop
Nix
48
star
7

prometheus-playground-old

Turnkey sandbox projects demonstrating a wide variety of Prometheus use cases
Go
38
star
8

git-license

Shell
32
star
9

nix-home-manager-config

Nix
19
star
10

dart-spray-akka-example

Scala
12
star
11

jelly

A next-generation tool for building technical documentation sites
Rust
12
star
12

kubectl-notes

Pod Notes: an example kubectl extension
Go
8
star
13

nix-channel

Nix
6
star
14

vrl-vscode

Vector Remap Language support for Visual Studio Code
Makefile
5
star
15

data-driven-documentation

5
star
16

grpc-gradle

gRPC + Gradle project template
Java
5
star
17

opa-ci-example

Open Policy Agent
4
star
18

nix-buf

An example Buf workspace powered by Nix
Nix
3
star
19

unix-bootstrap

UNIX Bootstrap
3
star
20

pdxjs-dart-presentation

Example Evernote clone created for the PDX JavaScript Admirers (February 2014)
Dart
3
star
21

ezpubsub

A Go library providing convenient abstractions over the Google Pub/Sub API
Go
3
star
22

dropwizard-riak

Riak driver for Dropwizard
Java
2
star
23

ssg

A static site generation toolkit for Rust
Rust
2
star
24

python-memory-leak

Datadog example project
Python
1
star
25

real-world-devops-with-nix

Nix
1
star
26

party

Party: a Go library for working with multipart form data
Go
1
star