An open API service indexing awesome lists of open source software.

https://github.com/aaronleopold/axum-graphql-prisma

A template for creating services in Rust using Axum and Prisma.
https://github.com/aaronleopold/axum-graphql-prisma

axum graphql graphql-rust prisma prisma-client-rust rust

Last synced: 3 months ago
JSON representation

A template for creating services in Rust using Axum and Prisma.

Awesome Lists containing this project

README

          

# Rust + Axum + Prisma

A template for creating services in Rust using Axum and Prisma. This uses the super cool [Prisma Rust Client](https://github.com/Brendonovich/prisma-client-rust).

## Getting Started

You'll want to have `cargo-watch` installed for the best DX, however it isn't required.

```bash
cargo install cargo-watch
```

Then you can run the `cargo-watch -x run` command to watch for changes and automatically rebuild the project.

## Prisma

To set up prisma, run:

```bash
cargo prisma generate # outputs client to src/prisma.rs
cargo prisma db push # outputs sqlite db to prisma/dev.db (specified in schema.prisma)
cargo seed # seeds the database with some data (unimplemented, create a seed based on your needs)
```

For more in-depth information about the prisma client, see the [Prisma Client Rust Docs](https://github.com/Brendonovich/prisma-client-rust/tree/main/docs).

## Run the Server

To run the server, run:

```bash
cargo run # or cargo-watch -x run
```

## GraphQL Playground

Go to [localhost:8080/api/graphql](http://localhost:8080/api/graphql) to see the playground. You can see the schema and the docs, but a few examples would be:

```graphql
# Create user
mutation {
createUser(input: { displayName: "oromei" }) {
id
}
}

# Create post
mutation {
createPost(
input: {
content: "Woah there!"
userId: "5ab80953-c38c-4ec8-8b4b-3ecc4bc1196f"
}
) {
id
content
user {
displayName
}
}
}

# Get all users
query {
getUsers {
id
displayName
}
}

# Get all posts
query {
getPosts {
id
content
user {
displayName
}
}
}
```

## Notes

This template uses Axum, but the bulk of the setup is for async_graphql + prisma. You should be able to easily swap out Axum for your preferred framework (e.g. Rocket, actix, etc).

The simple use of async_graphql means that queries are done in a less efficient manner than could be, since fetching relations using `with` is never utilized and relations are loaded separately. Additionally, dataloader is not utilized because I can't be bothered.