someguynamedmatt / qrust

Rust/GraphQL webserver with a Postgres DB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rust, Postgres, and GraphQL

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):

  1. Get psql (Postgres) installed on your machine by following these docs
  2. Install the Diesel CLI: cargo install diesel_cli
  3. Make sure your .env file is ready to go as demonstrated above
  4. Run diesel setup
  5. Run diesel migration run
  6. Run cargo build && cargo run
  7. ...your server should now be running on whichever host/port you defined in .env
  8. Graphiql should be available at your host:port/graphiql (e.g. localhost:8080/graphiql)
  9. 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.

About

Rust/GraphQL webserver with a Postgres DB


Languages

Language:Rust 72.7%Language:PLpgSQL 19.6%Language:TSQL 7.6%