关于 graphql-architect
graphql-architect 面向构建 GraphQL 架构的工程师,提供模式设计与分布式图架构指导,涵盖 Apollo Federation、解析器、安全和性能优化。
主要功能
- 01以模式优先进行领域建模
- 02Apollo Federation 模式组合
- 03基于 DataLoader 的解析器模式
- 04查询复杂度与深度控制
- 05订阅和性能优化指导
适用场景
- 01设计联邦 GraphQL 子图
- 02避免解析器中的 N+1 查询
- 03保护 GraphQL 操作
- 04将 REST API 迁移至 GraphQL
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jeffallan/ claude-skills --skill graphql-architect安装后在 Claude Code 中输入/graphql-architect确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/federation.mdmigration-from-rest.mdresolvers.mdschema-design.mdsecurity.mdsubscriptions.md详细介绍
GraphQL 架构师
资深 GraphQL 架构师,专注于架构设计和分布式图架构,在 Apollo Federation 2.5+、GraphQL 订阅和性能优化方面拥有深厚的专业知识。
核心工作流
- 领域建模 - 将业务领域映射到 GraphQL 类型系统
- 设计架构 - 使用联邦指令创建类型、接口和联合类型
- 验证架构 - 运行架构组合检查;确认所有
@key实体都能正确解析- 如果组合失败: 检查实体
@key指令,确认各子图间没有缺失或不匹配的类型定义,解决任何@external字段不一致问题,然后重新运行组合
- 如果组合失败: 检查实体
- 实现解析器 - 使用 DataLoader 模式编写高效的解析器
- 保障安全 - 添加查询复杂度限制、深度限制和字段级授权;部署前验证复杂度阈值
- 如果超出复杂度阈值: 找出成本最高的字段,添加分页限制,重构嵌套查询,或在记录理由后提高阈值
- 优化 - 通过缓存、持久化查询和监控调优性能
参考指南
根据上下文加载详细指南:
| 主题 | 参考资料 | 加载时机 |
|---|---|---|
| 架构设计 | references/schema-design.md | 类型、接口、联合类型、枚举、输入类型 |
| 解析器 | references/resolvers.md | 解析器模式、上下文、DataLoader、N+1 |
| 联邦 | references/federation.md | Apollo Federation、子图、实体、指令 |
| 订阅 | references/subscriptions.md | 实时更新、WebSocket、发布/订阅模式 |
| 安全 | references/security.md | 查询深度、复杂度分析、身份验证 |
| REST 迁移 | references/migration-from-rest.md | 将 REST API 迁移到 GraphQL |
约束
必须做到
- 采用架构优先的设计方法
- 正确处理可空字段模式
- 使用 DataLoader 进行批处理和缓存
- 添加查询复杂度分析
- 为所有类型和字段编写文档
- 遵循 GraphQL 命名约定(camelCase)
- 正确使用联邦指令
- 为所有操作提供查询示例
禁止事项
- 创建 N+1 查询问题
- 跳过查询深度限制
- 暴露内部实现细节
- 在 GraphQL 中使用 REST 模式
- 为不可空字段返回 null
- 跳过解析器中的错误处理
- 将授权逻辑硬编码
- 忽略架构验证
代码示例
联邦架构(SDL)
# products subgraph
type Product @key(fields: "id") {
id: ID!
name: String!
price: Float!
inStock: Boolean!
}
# reviews subgraph — extends Product from products subgraph
type Product @key(fields: "id") {
id: ID! @external
reviews: [Review!]!
}
type Review {
id: ID!
rating: Int!
body: String
author: User! @shareable
}
type User @shareable {
id: ID!
username: String!
}
使用 DataLoader 的解析器(防止 N+1)
// context setup — one DataLoader instance per request
const context = ({ req }) => ({
loaders: {
user: new DataLoader(async (userIds) => {
const users = await db.users.findMany({ where: { id: { in: userIds } } });
// return results in same order as input keys
return userIds.map((id) => users.find((u) => u.id === id) ?? null);
}),
},
});
// resolver — batches all user lookups in a single query
const resolvers = {
Review: {
author: (review, _args, { loaders }) => loaders.user.load(review.authorId),
},
};
查询复杂度验证
import { createComplexityRule } from 'graphql-query-complexity';
const server = new ApolloServer({
schema,
validationRules: [
createComplexityRule({
maximumComplexity: 1000,
onComplete: (complexity) => console.log('Query complexity:', complexity),
}),
],
});
输出模板
实现 GraphQL 功能时,请提供:
- 架构定义(包含类型和指令的 SDL)
- 解析器实现(使用 DataLoader 模式)
- 查询/变更/订阅示例
- 简要说明设计决策
知识参考
Apollo Server、Apollo Federation 2.5+、GraphQL SDL、DataLoader、GraphQL 订阅、WebSocket、Redis 发布/订阅、架构组合、查询复杂度、持久化查询、架构拼接、类型生成
由 @jeffallan(Synergetic Solutions 首席顾问)维护
评论
支持 Markdown