webiny-custom-graphql-api
它能做什么
这项技能在你的 AI 为 Webiny 后端添加新的 GraphQL 端点时提供指导。适合需要使用平台服务的自定义查询或变更的开发者。
适用场景
- ✓当你需要内置之外的自定义 GraphQL 查询或变更的时候。
- ✓当解析器必须调用后端服务,例如身份或 CMS 操作的时候。
- ✓当 GraphQL 解析器需要授权检查的时候。
技能链接
https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/graphql-api/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请添加一个 GraphQL 查询,返回当前登录用户最近的十个订单,并加入权限检查。”
每周 5 个新技能,1 分钟读完。
工作原理
技能说明如何实现一个 schema 工厂,用于添加类型定义和解析器。每个解析器通过容器获取其依赖,并将该工厂作为默认导出。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Custom GraphQL API
TL;DR
Add custom GraphQL queries and mutations using GraphQLSchemaFactory. Implement GraphQLSchemaFactory.Interface, use the schema builder to add type definitions and resolvers (with per-resolver DI), and export with GraphQLSchemaFactory.createImplementation(). Register as <Api.Extension>.
YOU MUST include the full file path with the .ts extension in every src prop. For example, use src={"/extensions/MySchema.ts"}, NOT src={"/extensions/MySchema"}. Omitting the file extension will cause a build failure.
YOU MUST use export default for the createImplementation() call when the file is targeted directly by an Extension src prop. Using a named export (export const Foo = SomeFactory.createImplementation(...)) will cause a build failure. Named exports are only valid inside files registered via createFeature.
The GraphQLSchemaFactory Pattern
The execute method receives a schema builder and returns it after adding type defs and resolvers.
// extensions/mySchema/MyGraphQLSchema.ts
import { GraphQLSchemaFactory } from "webiny/api/graphql";
class MySchema implements GraphQLSchemaFactory.Interface {
async execute(
builder: GraphQLSchemaFactory.SchemaBuilder
): Promise<GraphQLSchemaFactory.SchemaBuilder> {
builder.addTypeDefs(/* GraphQL */ `
extend type Query {
hello: String!
}
`);
builder.addResolver({
path: "Query.hello",
resolver: () => {
return () => "Hello, World!";
}
});
return builder;
}
}
export default GraphQLSchemaFactory.createImplementation({
implementation: MySchema,
dependencies: []
});
Register as an extension:
// extensions/mySchema/Extension.tsx
import React from "react";
import { Api } from "webiny/extensions";
export const MySchema = () => {
return <Api.Extension src={"@/extensions/mySchema/MyGraphQLSchema.ts"} />;
};
Schema Builder API Reference
| Method | Description |
|---|---|
builder.addTypeDefs(typeDefs: string) |
Add GraphQL type definitions (use extend type Query/Mutation to add to existing root types) |
builder.addResolver<TArgs>(config) |
Add a resolver with optional per-resolver DI dependencies |
addResolver Config
builder.addResolver<TArgs>({
path: "TypeName.fieldName", // dot-separated path
dependencies: [SomeAbstraction], // optional: DI tokens resolved at request time
resolver: (dep1, dep2, ...) => { // factory: receives resolved deps
return ({ parent, args, context, info }) => {
// actual resolver logic
return result;
};
}
});
Key points:
path: Dot-separated GraphQL type path, e.g."Query.hello","Mutation.createOrder","OrderMutation.create"dependencies: Array of DI abstraction tokens. Resolved per-request fromcontext.container, not at schema build timeresolver: A factory function that receives resolved dependencies and returns the actual resolver function- Resolver params: The inner function receives
{ parent, args, context, info }(named object, not positional)
Per-Resolver Dependency Injection
Dependencies in addResolver are resolved at request time from the request-scoped container. This is different from class-level constructor DI — it gives each resolver access to request-scoped services like identity and tenant context.
import { GraphQLSchemaFactory } from "webiny/api/graphql";
import { IdentityContext } from "webiny/api/security";
class WhoAmISchema implements GraphQLSchemaFactory.Interface {
async execute(
builder: GraphQLSchemaFactory.SchemaBuilder
): Promise<GraphQLSchemaFactory.SchemaBuilder> {
builder.addTypeDefs(/* GraphQL */ `
extend type Query {
whoAmI: String
}
`);
builder.addResolver({
path: "Query.whoAmI",
dependencies: [IdentityContext],
resolver: (identityContext: IdentityContext.Interface) => {
return () => {
const identity = identityContext.getIdentity();
return `Hello, ${identity.displayName}!`;
};
}
});
return builder;
常见问题
为什么导出必须是默认导出?
技能说明,如果直接注册文件时使用具名导出,就会导致构建失败。
路径中需要文件扩展名吗?
需要。源路径必须包含 .ts 扩展名。
解析器能使用后端服务吗?
能。技能涵盖把身份、租户和 CMS 用例等服务注入到 schema 中。
详情
作者的原始描述
Adding custom GraphQL queries and mutations using GraphQLSchemaFactory. Use this skill when the developer wants to add custom GraphQL endpoints, create custom queries or mutations, add business logic to the API layer, build custom resolvers, inject backend services (identity, tenancy, CMS use-cases) into their GraphQL schema, add authorization checks to resolvers, or build dynamic GraphQL inputs from CMS models. Covers the full pattern from simple queries to complex resolvers with dependency injection and permission transformers.
repo: webiny/webiny-js
热度
- GitHub 星标
- 8.1k
- 排名
- #12587
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
prd-to-plan
8.1k帮你的智能体把产品需求文档转化为分阶段的计划,由许多小的端到端切片组成,并保存为 Markdown 文件。
webiny-admin-architect
8.1k教你的智能体如何为 Webiny 平台构建管理界面,使用其业务逻辑层和呈现器模式。
webiny-admin-permissions
8.1k教你的智能体为 Webiny 管理界面添加权限控制,包括自动生成的权限表单以及显示或隐藏检查。
webiny-form-model
8.1k教你的智能体使用声明式字段构建器、布局、校验和条件规则,在 Webiny 中构建表单。