async-graphql 项目教程

async-graphql 项目教程

async-graphqlA GraphQL server library implemented in Rust项目地址:https://gitcode.com/gh_mirrors/as/async-graphql

1. 项目的目录结构及介绍

async-graphql 项目的目录结构如下:

async-graphql/
├── benches/
├── derive/
├── docs/
├── examples/
├── integrations/
├── parser/
├── src/
├── tests/
├── value/
├── .gitignore
├── .gitmodules
├── rustfmt.toml
├── ARCHITECTURE.md
├── CHANGELOG.md
├── Cargo.toml
├── LICENSE-APACHE
├── LICENSE-MIT
├── README.md
├── SECURITY.md

目录介绍:

  • benches/: 包含性能测试的代码。
  • derive/: 包含自定义派生宏的代码。
  • docs/: 包含项目文档。
  • examples/: 包含示例代码。
  • integrations/: 包含与其他Web服务器的集成代码。
  • parser/: 包含GraphQL解析器的代码。
  • src/: 包含项目的主要源代码。
  • tests/: 包含测试代码。
  • value/: 包含处理GraphQL值的代码。
  • .gitignore: Git忽略文件配置。
  • .gitmodules: Git子模块配置。
  • rustfmt.toml: Rust格式化配置文件。
  • ARCHITECTURE.md: 项目架构文档。
  • CHANGELOG.md: 项目变更日志。
  • Cargo.toml: Rust项目的依赖和元数据配置文件。
  • LICENSE-APACHE: Apache许可证文件。
  • LICENSE-MIT: MIT许可证文件。
  • README.md: 项目介绍和使用说明。
  • SECURITY.md: 项目安全相关文档。

2. 项目的启动文件介绍

async-graphql 项目的启动文件通常位于 src/ 目录下。主要的启动文件是 main.rs,它负责初始化和服务器的启动。

// src/main.rs

use async_graphql::{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 std::error::Error>> {
    // 创建Schema
    let schema = Schema::build(Query, EmptyMutation, EmptySubscription).finish();

    // 启动HTTP服务器
    let app = Route::new().at("/", get(graphiql).post(GraphQL::new(schema)));
    Server::new(TcpListener::bind("127.0.0.1:8000")).run(app).await?;
    Ok(())
}

启动文件介绍:

  • main.rs: 包含主要的启动逻辑,包括Schema的创建和HTTP服务器的启动。
  • Query: 定义了GraphQL查询对象。
  • graphiql: 提供GraphiQL接口。
  • Schema::build: 创建GraphQL Schema。
  • Server::new: 启动HTTP服务器。

3. 项目的配置文件介绍

async-graphql 项目的配置文件主要是 Cargo.toml,它包含了项目的依赖、元数据和其他配置信息。

# Cargo.toml

[package]
name = "async-graphql"
version = "3.0.0"
edition = "2018"

[dependencies]
async-graphql = { version = "3.0.0", features = ["graphiql"] }
async_graphql_poem = "0.1.0"
poem = "1.0.0"
tokio = { version = "1.0", features = ["full"] }

[features]
default = ["graphiql"]
apollo_tracing = []
apollo_persisted_queries = []
bson = []
bigdecimal = []
cbor = []
unblock = []
uuid = []
url = []

[dev-dependencies]
criterion = "

async-graphqlA GraphQL server library implemented in Rust项目地址:https://gitcode.com/gh_mirrors/as/async-graphql

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌昱有Melanie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值