A webserver example
This example uses Diesel to connect to a Postgres instance. For its webserver component it's using Actix and Actix-web.
To get this working you need to create a .env
file that lives at the same level as the Cargo.toml
file. YOU MUST have the variables:
DATABASE_HOST=localhost
DATABASE_USER=your-postgres-username
DATABASE=qrust
DATABASE_URL="postgres://$DATABASE_USER:$DATABASE_PASSWORD@$DATABASE_HOST/$DATABASE"
PORT=:8080
HOST=localhost$PORT
GRAPHQL_SOURCE="http://$HOST/graphql"
Get it going (assuming Rust is already at your fingertips):
- Get
psql
(Postgres) installed on your machine by following these docs - Install the Diesel CLI:
cargo install diesel_cli
- Make sure your
.env
file is ready to go as demonstrated above - Run
diesel setup
- Run
diesel migration run
- Run
cargo build && cargo run
- ...your server should now be running on whichever host/port you defined in
.env
- Graphiql should be available at your
host:port/graphiql
(e.g.localhost:8080/graphiql
) - Add some fake posts, on
/graphiql
run the mutation:
mutation {
createPost(data: {title:"new post", body: "This is a new post"}) {
title
id
}
}
...now you can query that same post:
query {
posts {
title
}
}
Congrats! You have a Rust/Graphql/Postgres server running!
Explore the patterns, read the Diesel/GraphQL/Postgres documentation, and make something awesome.