Juniper - fast & type-safe GraphQL Servers in Rust

tomekponiat

Tomek Poniatowicz

Posted on September 8, 2020

Juniper - fast & type-safe GraphQL Servers in Rust

GraphQL started being developed internally at Facebook back in 2012, before being publicly released as open-source in 2015. GraphQL is a query language for APIs, its main advantage is allowing clients to query exactly for the type of data that is required. The definitions of all the data clients can query through the service are stored in GraphQL schema. Each field in the schema is attached to a function, called a resolver, which gets called to produce the values when a query is executed.

GraphQL is language-agnostic which makes it fairly easy to be adopted across different technologies, one of its successful implementations is done in Rust, by a library called Juniper.

Code- first, fast & type-safe GraphQL Servers in Rust

Juniper is a code-first approach that allows you to write type-safe and super fast GraphQL servers in Rust, keeping declaring and resolving GraphQL schemas as easy as possible.

GraphQL query language is fully supported, including interfaces, unions, schema introspection, validations, as well as some basic Rust crates integrations allowing you to use popular Rust types as uuid, url, chrono, or bson out of the box.

As previously mentioned Juniper is a code-first library, if you prefer the schema-first approach you can use juniper-from-schema to auto-generate resolvers for your GraphQL schema.

Although Juniper does not provide a built-in web server functionality it comes with building blocks to make integration with your servers as seamless as possible, as well as optionally providing pre-built integration for popular Rust web frameworks like:

  • Actix,
  • Hyper,
  • Iron,
  • Rocket,
  • Warp.

Example of Juniper integration for Actix

Below you can find a sample Juniper integration with the Actix web framework. The server runs on 127.0.0.1:8080 and has built-in GraphiQL for easier testing. Star it with:

cd examples/juniper
cargo run (or ``cargo watch -x run``)
Enter fullscreen mode Exit fullscreen mode

Simple query example:
Query and its result example

Simple mutation example with its result:
Mutation and its result example

If you are looking for more advanced usage examples make sure to visit juniper-advanced example repo.

Source: github.com/actix/examples

Want to speed up your GraphQL schema development?

GraphQL Editor is a supportive tool for both advanced GraphQL users as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your GraphQL API much faster. Try GraphQL Editor for free!

GraphQL Editor

💖 💪 🙅 🚩
tomekponiat
Tomek Poniatowicz

Posted on September 8, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related