MCPcopy Index your code
hub / github.com/async-graphql/async-graphql

github.com/async-graphql/async-graphql @v7.0.13

Chat with this repo
repository ↗ · DeepWiki ↗ · release v7.0.13 ↗ · + Follow
3,509 symbols 7,649 edges 294 files 297 documented · 8%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

async-graphql

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

Book中文文档DocsGitHub repositoryCargo package


ci status code coverage Unsafe Rust forbidden Crates.io version docs.rs docs downloads PRs 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, *};

struct Query;

#[Object]
impl Query {
    async fn howdy(&self) -> &'static str {
        "partner"
    }
}

#[handler]
async fn graphiql() -> impl IntoResponse {
    Html(GraphiQLSource::build().finish())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // create the schema
    let schema = Schema::build(Query, EmptyMutation, EmptySubscription).finish();

    // start the http server
    let 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]
async fn graphiql() -> impl IntoResponse {
    Html(GraphiQLSource::build().finish())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let query = Object::new("Query").field(Field::new(
        "howdy",
        TypeRef::named_nn(TypeRef::STRING),
        |_| FieldFuture::new(async { "partner" }),
    ));

    // create the schema
    let schema = Schema::build(query, None, None).register(query).finish()?;

    // start the http server
    let 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.81.0 or later

Examples

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

git submodule update # update the examples repo
cd 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):

feature enables
apollo_tracing Enable the Apollo tracing extension.
apollo_persisted_queries Enable the Apollo persisted queries extension.
boxed-trait Enables async-trait for all traits.
bson Integrate with the bson crate.
bigdecimal Integrate with the bigdecimal crate.
cbor Support for serde_cbor.
chrono Integrate with the chrono crate.
chrono-tz Integrate with the chrono-tz crate.
dataloader Support DataLoader.
decimal Integrate with the rust_decimal crate.
dynamic-schema Support dynamic schema
fast_chemail Integrate with the fast_chemail crate.
graphiql Enables the GraphiQL IDE integration
hashbrown Integrate with the hashbrown crate.
log Enable the Logger extension.
opentelemetry Enable the OpenTelemetry extension.
playground Enables the GraphQL playground IDE integration
rawvalue Support raw values from serde_json
secrecy Integrate with the secrecy crate.
smol_str Integrate with the smol_str crate.
string_number Enable the StringNumber.
time Integrate with the time crate.
tracing Enable the Tracing extension.
tempfile Save the uploaded content in the temporary file.
tokio-sync Integrate with the tokio::sync::RwLock and tokio::sync::Mutex.
unblock Support Asynchronous reader for Upload
uuid Integrate with the uuid crate.
url Integrate 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 desi

Extension points exported contracts — how you extend this code

InputType (Interface)
Represents a GraphQL input type. [19 implementers]
src/base.rs
CursorType (Interface)
Cursor type A custom scalar that serializes as a string. [9 implementers]
src/types/connection/cursor.rs
ScalarType (Interface)
A GraphQL scalar. You can implement the trait to create a custom scalar. # Examples ```rust use async_graphql::*; st [55 …
src/resolver_utils/scalar.rs
ExtensionFactory (Interface)
Extension factory Used to create an extension instance. [11 implementers]
src/extensions/mod.rs
CustomValidator (Interface)
Represents a custom input value validator. [5 implementers]
src/validators/mod.rs
Guard (Interface)
(no doc) [6 implementers]
src/guard.rs
SubscriptionType (Interface)
A GraphQL subscription object [4 implementers]
src/subscription.rs
Visitor (Interface)
(no doc) [29 implementers]
src/validation/visitor.rs

Core symbols most depended-on inside this repo

clone
called by 319
src/schema.rs
map
called by 230
parser/src/pos.rs
map
called by 159
src/types/maybe_undefined.rs
as_ref
called by 130
src/types/id.rs
insert
called by 107
src/dataloader/cache.rs
get
called by 101
src/dynamic/value_accessor.rs
as_str
called by 96
value/src/lib.rs
iter
called by 84
src/request.rs

Shape

Method 1,731
Function 934
Class 652
Enum 141
Interface 51

Languages

Rust100%

Modules by API surface

tests/federation.rs107 symbols
parser/src/parse/generated.rs89 symbols
src/validation/visitor.rs78 symbols
src/validation/rules/arguments_of_correct_type.rs70 symbols
src/validation/test_harness.rs65 symbols
src/registry/mod.rs63 symbols
derive/src/args.rs56 symbols
src/dynamic/schema.rs50 symbols
value/src/serializer.rs49 symbols
tests/input_object.rs49 symbols
src/schema.rs49 symbols
src/context.rs49 symbols

For agents

$ claude mcp add async-graphql \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact