Skip to content

Repository files navigation

async-graphql

a high-performance graphql server library that's fully specification compliant

Book中文文档DocsGitHub repositoryCargo package


ci statuscode coverageUnsafe Rust forbiddenCrates.io versiondocs.rs docsdownloadsPRs Welcome

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

Static schema

use std::error::Error;use async_graphql::{http::GraphiQLSource,EmptyMutation,EmptySubscription,Object,Schema};use async_graphql_poem::*;use poem::{listener::TcpListener, web::Html,*};structQuery;#[Object]implQuery{asyncfnhowdy(&self) -> &'staticstr{"partner"}}#[handler]asyncfngraphiql() -> implIntoResponse{Html(GraphiQLSource::build().finish())}#[tokio::main]asyncfnmain() -> Result<(),Box<dynError>>{// create the schemalet schema = Schema::build(Query,EmptyMutation,EmptySubscription).finish();// start the http serverlet app = Route::new().at("/",get(graphiql).post(GraphQL::new(schema)));println!("GraphiQL: http://localhost:8000");Server::new(TcpListener::bind("0.0.0.0:8000")).run(app).await?;Ok(())}

Dynamic schema

Requires the dynamic-schema feature to be enabled.

use std::error::Error;use async_graphql::{dynamic::*, http::GraphiQLSource};use async_graphql_poem::*;use poem::{listener::TcpListener, web::Html,*};#[handler]asyncfngraphiql() -> implIntoResponse{Html(GraphiQLSource::build().finish())}#[tokio::main]asyncfnmain() -> Result<(),Box<dynError>>{let query = Object::new("Query").field(Field::new("howdy",TypeRef::named_nn(TypeRef::STRING),
|_| FieldFuture::new(async{"partner"}),));// create the schemalet schema = Schema::build(query,None,None).register(query).finish()?;// start the http serverlet app = Route::new().at("/",get(graphiql).post(GraphQL::new(schema)));println!("GraphiQL: http://localhost:8000");Server::new(TcpListener::bind("0.0.0.0:8000")).run(app).await?;Ok(())}

⚠️Security

I strongly recommend limiting the complexity and depth of queries in a production environment to avoid possible DDos attacks.

Features

  • Static and dynamic schemas are fully supported
  • Fully supports async/await
  • Type safety
  • Rustfmt friendly (Procedural Macro)
  • Custom scalars
  • Minimal overhead
  • Easy integration (poem, axum, actix-web, tide, warp, rocket ...)
  • Upload files (Multipart request)
  • Subscriptions (WebSocket transport)
  • Custom extensions
  • Error extensions
  • Limit query complexity/depth
  • Batch queries
  • Apollo Persisted Queries
  • Apollo Tracing extension
  • Apollo Federation(v2)

Note: Minimum supported Rust version: 1.86.0 or later

Examples

All examples are in the sub-repository, located in the examples directory.

git submodule update # update the examples repocd examples && cargo run --bin [name]

For more information, see the sub-repository README.md.

Integrations

Integrations are what glue async-graphql with your web server, here are provided ones, or you can build your own!

Crate features

This crate offers the following features. Most are not activated by default, except the integrations of GraphiQL (graphiql) and GraphQL Playground (playground):

featureenables
apollo_tracingEnable the Apollo tracing extension.
apollo_persisted_queriesEnable the Apollo persisted queries extension.
boxed-traitEnables async-trait for all traits.
bsonIntegrate with the bson crate.
bigdecimalIntegrate with the bigdecimal crate.
cborSupport for serde_cbor.
chronoIntegrate with the chrono crate.
chrono-tzIntegrate with the chrono-tz crate.
dataloaderSupport DataLoader.
decimalIntegrate with the rust_decimal crate.
dynamic-schemaSupport dynamic schema
fast_chemailIntegrate with the fast_chemail crate.
graphiqlEnables the GraphiQL IDE integration
hashbrownIntegrate with the hashbrown crate.
logEnable the Logger extension.
opentelemetryEnable the OpenTelemetry extension.
playgroundEnables the GraphQL playground IDE integration
rawvalueSupport raw values from serde_json
secrecyIntegrate with the secrecy crate.
smol_strIntegrate with the smol_str crate.
string_numberEnable the StringNumber.
timeIntegrate with the time crate.
tracingEnable the Tracing extension.
tempfileSave the uploaded content in the temporary file.
tokio-syncIntegrate with the tokio::sync::RwLock and tokio::sync::Mutex.
unblockSupport Asynchronous reader for Upload
uuidIntegrate with the uuid crate.
urlIntegrate with the url crate.

Observability

One of the tools used to monitor your graphql server in production is Apollo Studio. Apollo Studio is a cloud platform that helps you build, monitor, validate, and secure your organization's data graph. Add the extension crate async_graphql_apollo_studio_extension to make this avaliable.

Who's using async-graphql in production?

Community Showcase

  • rust-actix-graphql-sqlx-postgresql Using GraphQL with Rust and Apollo Federation
  • entity-rs A simplistic framework based on TAO, Facebook's distributed database for Social Graph.
  • vimwiki-server Provides graphql server to inspect and manipulate vimwiki files.
  • Diana Diana is a GraphQL system for Rust that's designed to work as simply as possible out of the box, without sacrificing configuration ability.
  • cindythink
  • sudograph

Blog Posts

References

License

Licensed under either of

About

A GraphQL server library implemented in Rust

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages