• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

lucperkins/rust-graphql-juniper-actix-diesel-postgres: An example GraphQL server ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

lucperkins/rust-graphql-juniper-actix-diesel-postgres

开源软件地址(OpenSource Url):

https://github.com/lucperkins/rust-graphql-juniper-actix-diesel-postgres

开源编程语言(OpenSource Language):

Rust 88.7%

开源软件介绍(OpenSource Introduction):

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap