DEV Community

Cover image for Building Powerful GraphQL Servers with Rust

Building Powerful GraphQL Servers with Rust

Ian Wilson on September 13, 2019

Photo by Bill Oxford on Unsplash Setting up a GraphQL server with Rust, Juniper, Diesel, and Actix; learning about Rust's web frameworks and power...
Collapse
 
chevito profile image
Victor Gonzalez

This kind of articles are the ones that incites you to install a new tool and try some things out. 👌

Collapse
 
csfalcione profile image
Caleb Falcione

Nice article!

In the Exposing the Schema section, just prior to running cargo build and navigating to localhost:8080/graphiql, I couldn't get main.rs to compile without changing

use crate::schema::{create_schema, Schema};
Enter fullscreen mode Exit fullscreen mode

to

use graphql_schema::{create_schema, Schema};
Enter fullscreen mode Exit fullscreen mode

Am I missing something?

Collapse
 
iwilsonq profile image
Ian Wilson

Ah thank you, you found an error in the snippet. I just changed it but you're correct, since the module is called graphql_schema we must refer to it like that.

Originally I had it called 'schema.rs' until I added the diesel integration (whose migrations overwrite whatever you have in schema.rs)

Collapse
 
csfalcione profile image
Caleb Falcione

Another issue (you may be able to glean that I've been slowly working through the article over the past few days):
You don't seem to instruct adding the extern crate diesel; and mod schema; lines to main.rs. That prevented me from compiling after setting up diesel (immediately prior to adding the mutations).

Thread Thread
 
iwilsonq profile image
Ian Wilson

Thank you for being so thorough! I added another snippet at that point adding the imports. Hopefully that is the end of the errata - transpiling code from a project to an article in a digestible way comes with some struggles :D

Collapse
 
gklijs profile image
Gerard Klijs

Thanks, great article. Seems like soon they will also support subscriptions. Then I can finally 'use' it and compare to my Clojure implementation. Then one is 'stuck' at about 300 subscriptions/second hope for Rust to do better. Also there is github.com/tsegismont/graphql-serv..., maybe you can add a rust version?

Collapse
 
iwilsonq profile image
Ian Wilson

Yes - hopefully subscriptions will land soon, I saw your comment on that issue: github.com/graphql-rust/juniper/is.... Ah that's a pretty neat project, where is your Clojure implementation? I'll consider it, I too am interested in how it stacks up.

Collapse
 
gklijs profile image
Gerard Klijs

Haven't had the time yet. Preparing to speak at GraphQL Summit, polishing presentation and maybe make some improvements to the demo. I have github.com/openweb-nl/open-bank-mark but that's testing the Kafka backend more. Next iteration of that will focus on the GraphQL endpoint (and may in time have a rust variant for it).

Collapse
 
fattenap profile image
Frank Panetta

Great article! I have a repo that also uses these technologies together. Although it uses Diesel connection pool (R2D2) to connect to postgreSQL and passes the connection through to the Juniper GraphQL Context, to be accessed from the QueryRoot and MutationRoot.

i.e: #[juniper::object(Context = Context,)]

You probably already know how to hook all that up, but it took me ages to figure out. Just thought you might be interested. github.com/fattenap/actix-web-juni...

Anyway, thanks for the article, it really helped.

Collapse
 
barryblando profile image
Barry⚡ • Edited

Got sad news, actix-web is dead. I hope the maintainer will not abandon it. Damn. Ian, will you update the article in the future using an alternative non-blocking framework? warp/gotham/tide/rocket any framework that you feel it can be used for a small production project? Thanks!

Collapse
 
iwilsonq profile image
Ian Wilson

I heard that in this post words.steveklabnik.com/a-sad-day-f.... I may create a new article and link to it from here. That sucks indeed, the library is a joy to use so I hope this won't be the end.

Collapse
 
barryblando profile image
Barry⚡ • Edited

Update: actix-web is alive. Whew! Hope it won't happen again.

Source: github.com/actix/actix-web/issues/...

Thread Thread
 
iwilsonq profile image
Ian Wilson

That is great news! :D

Collapse
 
owencraston profile image
Owen Craston

Great tutorial. I am getting stuck on the diesel migrations. I used various DATABASE_URL and even looked at the diesel getting started guide where they said to use postgres://username:password@localhost/diesel_demo and everyone I run diesel setup I get

Creating database: garden_guru_server
fe_sendauth: no password supplied

and if I try to run diesel migration run I get
Err value: ConnectionError(BadConnection("fe_sendauth: no password supplied\n"))

Do you know why this could be ? I installed PostgreSQL with brew and thePGAdmin` seems to be working fine.

Collapse
 
5422m4n profile image
Sven Kanoldt

Dope man! Well made 👏 👏 👏

Collapse
 
jacobmgevans profile image
Jacob Evans

I ran across an issue with Actix with how the Response was structured inside of the web::block() moving the Response and Error construction outside resolved this for me. I found an issue related as well in their GH (github.com/actix/actix-web/issues/857)

Code Snippet of Changes

fn graphql(
    st: web::Data<Arc<Schema>>,
    data: web::Json<GraphQLRequest>,) -> impl Future<Item = HttpResponse , Error = error::Error>{
        web::block(move || {
            let res = data.execute(&st, &());
            Ok::<_,serde_json::error::Error>(serde_json::to_string(&res)?)
        }).map_err(Error::from)
        .and_then(|user| {
            Ok(HttpResponse::Ok()
        .content_type("application/json")
        .body(user))
        })
}
Collapse
 
clifinger profile image
Julien Lenne • Edited

Hello, thank you for the article and the inspiration!

I did a little boilerplate project with GraphQL and JWT Authentication, Feel free to comment :)

github.com/clifinger/canduma

Collapse
 
iwilsonq profile image
Ian Wilson

Nice! I like what you did there, authentication is a pretty serious part of writing non-abusable and secure APIs

Collapse
 
compasses profile image
Jet He • Edited

great!

Collapse
 
carlillo profile image
Carlos Caballero

Thanks!!

Collapse
 
strottos profile image
Steven Trotter

Brilliant article. Thanks.