GraphQL 教程

FastAPI + GraphQL 教程

目录

介绍

GraphQL 是一种用于 API 的查询语言,它提供了一种更高效、强大和灵活的替代 REST 的方案。本教程将介绍如何在 FastAPI 项目中使用 GraphQL。

为什么选择 GraphQL?

  1. 按需获取数据

    • 客户端可以精确指定需要的数据
    • 避免过度获取和数据不足的问题
    • 减少网络传输量
  2. 单个请求获取多个资源

    • 减少 HTTP 请求次数
    • 提高应用性能
    • 简化前端逻辑
  3. 强类型系统

    • 编译时错误检查
    • 自动生成文档
    • 更好的开发体验

GraphQL 基础

1. Schema 定义

python 复制代码
@strawberry.type
class User:
    id: int
    username: str
    email: str
    is_active: bool

@strawberry.type
class Query:
    @strawberry.field
    def user(self, id: int) -> Optional[User]:
        return get_user(id)

    @strawberry.field
    def users(self) -> List[User]:
        return get_users()

2. 类型系统

graphql 复制代码
# 标量类型
Int: 整数
Float: 浮点数
String: 字符串
Boolean: 布尔值
ID: 唯一标识符

# 对象类型
type User {
  id: ID!
  username: String!
  email: String!
  isActive: Boolean!
}

查询操作

1. 基本查询

graphql 复制代码
# 查询单个用户
query {
  user(id: 1) {
    id
    username
    email
  }
}

# 查询用户列表
query {
  users {
    id
    username
    email
    isActive
  }
}

2. 带参数的查询

graphql 复制代码
# 带过滤条件的查询
query {
  users(filter: {
    isActive: true
    role: "admin"
  }) {
    id
    username
    email
  }
}

3. 查询片段

graphql 复制代码
fragment UserFields on User {
  id
  username
  email
}

query {
  user(id: 1) {
    ...UserFields
    isActive
  }
}

变更操作

1. 创建资源

graphql 复制代码
mutation {
  createUser(
    username: "newuser"
    email: "new@example.com"
    password: "password123"
  ) {
    id
    username
    email
  }
}

2. 更新资源

graphql 复制代码
mutation {
  updateUser(
    id: 1
    data: {
      username: "updateduser"
      email: "updated@example.com"
    }
  ) {
    id
    username
    email
  }
}

3. 删除资源

graphql 复制代码
mutation {
  deleteUser(id: 1) {
    success
    message
  }
}

高级特性

1. 批量操作

graphql 复制代码
mutation {
  createUsers(
    users: [
      { username: "user1", email: "user1@example.com" }
      { username: "user2", email: "user2@example.com" }
    ]
  ) {
    id
    username
  }
}

2. 嵌套查询

graphql 复制代码
query {
  users {
    id
    username
    posts {
      id
      title
      comments {
        id
        content
      }
    }
  }
}

3. 订阅

graphql 复制代码
subscription {
  userCreated {
    id
    username
    email
  }
}

最佳实践

1. 查询优化

  • 使用片段复用字段
graphql 复制代码
fragment UserFields on User {
  id
  username
  email
}

query {
  user1: user(id: 1) {
    ...UserFields
  }
  user2: user(id: 2) {
    ...UserFields
  }
}
  • 限制查询深度
python 复制代码
@strawberry.field
def users(self, limit: int = 10, offset: int = 0) -> List[User]:
    return get_users(limit=limit, offset=offset)

2. N+1 问题解决

python 复制代码
from strawberry.dataloader import DataLoader

async def load_posts_by_user_ids(keys: List[int]) -> List[List[Post]]:
    posts = await get_posts_for_users(keys)
    return group_posts_by_user(posts)

@strawberry.field
async def posts(self, info: Info) -> List[Post]:
    loader = info.context["posts_loader"]
    return await loader.load(self.id)

3. 错误处理

python 复制代码
@strawberry.mutation
def create_user(self, username: str, email: str) -> Union[User, Error]:
    try:
        user = create_user_in_db(username, email)
        return user
    except ValidationError as e:
        return Error(message=str(e))

4. 认证和授权

python 复制代码
@strawberry.field
def protected_data(self, info: Info) -> str:
    user = info.context.get("user")
    if not user:
        raise PermissionError("Not authenticated")
    if not user.is_admin:
        raise PermissionError("Not authorized")
    return "sensitive data"

使用工具

1. GraphQL Playground

2. curl 请求

bash 复制代码
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "{ users { id username } }"}' \
  http://localhost:8899/graphql

3. Python 请求

python 复制代码
import requests

def query_graphql():
    query = """
    {
        users {
            id
            username
            email
        }
    }
    """
    
    response = requests.post(
        'http://localhost:8899/graphql',
        json={'query': query}
    )
    return response.json()

调试技巧

  1. 使用 __typename
graphql 复制代码
query {
  users {
    __typename
    id
    username
  }
}
  1. 查询架构信息
graphql 复制代码
query {
  __schema {
    types {
      name
      fields {
        name
        type {
          name
        }
      }
    }
  }
}
  1. 使用别名
graphql 复制代码
query {
  activeUsers: users(filter: { isActive: true }) {
    id
    username
  }
  inactiveUsers: users(filter: { isActive: false }) {
    id
    username
  }
}
相关推荐
uhakadotcom3 分钟前
PostgreSQL的CVE-2025-1094漏洞解析:SQL注入与元命令执行
后端·面试·github
zhuyasen10 分钟前
Go语言开发实战:app库实现多服务启动与关闭的优雅方案
后端·go
ITlinuxP15 分钟前
2025最新Postman、Apipost和Apifox API 协议与工具选择方案解析
后端·测试工具·postman·开发工具·apipost·apifox·api协议
计算机-秋大田34 分钟前
基于Spring Boot的宠物健康顾问系统的设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·课程设计
uhakadotcom1 小时前
OpenHands:AI 驱动的软件开发框架
后端·面试·github
uhakadotcom1 小时前
FinGPT:金融领域的开源语言模型框架
后端·面试·github
计算机学姐1 小时前
基于Asp.net的教学管理系统
vue.js·windows·后端·sqlserver·c#·asp.net·visual studio
Asthenia04121 小时前
TCP的阻塞控制算法:无控制随便发/固定窗口/慢启动+阻塞避免/快恢复/TCP Cubic/BBR
后端
AntBlack2 小时前
Python 打包笔记 : 你别说 ,PyStand 确实简单易上手
后端·python·创业
xiaozaq2 小时前
Spring Boot静态资源访问顺序
java·spring boot·后端