Rust GraphQL server example. This example uses crates juniper, rocket and diesel.
Before running example, you have to create database and user.
CREATE USER IF NOT EXISTS maxroach;
CREATE DATABASE IF NOT EXISTS review;
GRANT ALL ON DATABASE review TO maxroach;
Or use scripts in bin
directory.
./bin/create_db.sh
After creating database and user, you can migrate tables by diesel cli. Following command generate migration directory and up/down.sql.
diesel setup
Then you have to fix up/down.sql under the migrations/00000000000000_diesel_initial_setup
.
Bacause sql files generated by diesel setup
set custom functions to database,
but cockroachdb does not support function yet.
-- up.sql
CREATE TABLE person (
id UUID NOT NULL,
name VARCHAR NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE post (
id UUID NOT NULL,
person_id UUID NOT NULL,
text VARCHAR NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (person_id)
REFERENCES person (id)
);
-- down.sql
DROP TABLE IF EXISTS post;
DROP TABLE IF EXISTS person;
Run migration.
diesel migration run
diesel migration run command generates diesel table schema file src/db/schemas.rs
.