![actix juniper actix juniper](https://blog.graphqleditor.com/articles/graphql-rust-juniper/query_result.optimized.png)
- #ACTIX JUNIPER HOW TO#
- #ACTIX JUNIPER INSTALL#
- #ACTIX JUNIPER UPDATE#
- #ACTIX JUNIPER CODE#
- #ACTIX JUNIPER FREE#
GraphQL has become a new alternative for developing APIs other than the common REST approach. env file in our working directory: echo DATABASE_URL=postgres://localhost/rust_graphql_example >.
#ACTIX JUNIPER INSTALL#
Let's install it: cargo install diesel_cli -no-default-features -features postgresĪfter that, we will add a connection URL to a. The diesel CLI will walk us through initializing our tables. You can also use a different database like MySQL in case you are more familiar with it. See here to install Postgres locally on your machine.
#ACTIX JUNIPER HOW TO#
Installing Postgres and setting up your own database belongs in a different article, but I'll walk through how to install diesel, the popular Rust library for handling SQL databases. A static list of two members that I wrote myself at dev time will not fly in this publication. If I stopped here, I wouldn't even be doing the examples in the docs much justice. It does seem to take a little more effort than setting up a GraphQL server with Node.js and Apollo but the static typing of Rust combined with its incredible performance makes it a worthy trade - if you're willing to work at it. With cargo run and navigation to we can try out the field we configured.
![actix juniper actix juniper](https://i.ytimg.com/vi/QXJ0wKBLt-8/hqdefault.jpg)
We only need to specify which path is serving our GraphQL schema, which is "/graphql" in this case. This handler is much simpler, it merely returns the html of the GraphiQL interactive playground. Let html = graphiql_source(" HttpResponse::Ok() In order to test out our GraphQL schema, we'll also add a handler for "/graphiql". For a greater explanation of Futures in Rust, I recommend this article by Joe Jackson.
#ACTIX JUNIPER CODE#
It does this by creating a future from web::block and chaining handlers for success and error states.įutures are analogous to Promises in JavaScript, which is enough to understand this code snippet. Our implementation of the "/graphql" route takes executes a GraphQL request against our schema from application state. Starting with the "/graphql" route: // main.rs We must now implement the handlers for those two services.
![actix juniper actix juniper](https://i2.hdslb.com/bfs/archive/6206d68fde2d51d42338f0011cb83e9f3ecb1a7c.jpg)
service(web::resource("/graphql").route(web::post().to_async(graphql))) Let schema = std::sync::Arc::new(create_schema())
#ACTIX JUNIPER UPDATE#
Let's update the dependencies sections of Cargo.toml like so:ĭiesel = Installing the necessary libraries in Rust means adding a line containing the library name and version number.
#ACTIX JUNIPER FREE#
main.rsĪs a sanity check, feel free to run cargo run in order to execute the program. The initialization command bootstraps our Cargo.toml file which contains our projects dependencies as well as a main.rs file which has a simple "Hello World" example. To begin, we need to initialize our project with cargo and then install dependencies.
![actix juniper actix juniper](https://raw.githubusercontent.com/clifinger/canduma/master/docs/images/new-insomnia-grahql-get-jwt.png)
This article assumes some preliminary knowledge of the Rust toolchain. Note that I will not go into detail regarding installing Rust or Cargo.