webiny-dependency-injection
Skill này làm gì
Skill này giải thích mẫu chung mà mọi extension Webiny dùng để lấy dịch vụ. Skill dành cho lập trình viên viết bất kỳ extension Webiny nào và cần hiểu cách code của họ nhận các phụ thuộc.
Khi nào nên dùng
- ✓Khi bạn viết một extension Webiny và cần truy cập logger hoặc identity context.
- ✓Khi bạn cần gọi các thao tác tạo, cập nhật hoặc xóa của CMS từ code của mình.
- ✓Khi bạn chưa rõ danh sách dependencies hoạt động thế nào.
Liên kết skill
https://github.com/webiny/webiny-js/blob/next/skills/user-skills/dependency-injection/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Chỉ tôi cách inject logger và các use case mục CMS vào lớp extension Webiny của tôi.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill giải thích các bước giống nhau cho mọi loại extension: viết một lớp có interface tương ứng, khai báo phụ thuộc trong constructor, rồi export nó cùng factory và danh sách dependency tương ứng.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Dependency Injection Patterns
TL;DR
Every Webiny extension type uses the same DI pattern: define a class implementing *.Interface, declare dependencies in the constructor, and export via *.createImplementation({ implementation, dependencies }). The DI container automatically provides the required services, ensures type safety, and validates at compile time. This pattern is the connective tissue across all extension types -- API, Admin, CLI, and Infrastructure.
Working Context
This skill applies to both extension developers (working in extensions/) and core developers (working in packages/). The DI pattern is universal — only the import paths differ.
Extensions (extensions/) |
Core (packages/) |
|
|---|---|---|
createAbstraction |
from "webiny/api" or from "webiny/admin" |
from "@webiny/feature/api" or from "@webiny/feature/admin" |
createFeature |
from "webiny/api" or from "webiny/admin" |
from "@webiny/feature/api" or from "@webiny/feature/admin" |
| Factory imports | from "webiny/api/graphql", from "webiny/api/cms/model", etc. |
Direct package paths: from "@webiny/handler-graphql/...", from "@webiny/api-headless-cms/..." |
| Catalog paths | Use the Import: path |
Use the Source: path |
Detect which context you're in by checking the file path: extensions/ → extension mode, packages/ → core mode. The dependencies array, constructor injection, scoping rules, and all other patterns are identical in both contexts.
The Universal Pattern
import { SomeFactory } from "webiny/some/path";
import { Logger, BuildParams } from "webiny/api";
class MyImplementation implements SomeFactory.Interface {
constructor(
private logger: Logger.Interface,
private buildParams: BuildParams.Interface
) {}
execute(/* factory-specific params */) {
this.logger.info("Doing something...");
// buildParams.get() returns T | null — always account for null.
const value = this.buildParams.get<string>("MY_PARAM");
}
}
export default SomeFactory.createImplementation({
implementation: MyImplementation,
dependencies: [Logger, BuildParams]
});
Key rules:
- One class per file -- each extension file exports a single implementation.
- Constructor injection -- dependencies are received as constructor parameters, in the same order as the
dependenciesarray. - Dependencies array -- must exactly match the constructor parameter order and types.
- Interface types -- always type constructor params as
Feature.Interface.
Where This Pattern Appears
| Extension Type | Factory | Import Path |
|---|---|---|
| Content Models | ModelFactory |
"webiny/api/cms/model" |
| GraphQL Schemas | GraphQLSchemaFactory |
"webiny/api/graphql" |
| API Keys | ApiKeyFactory |
"webiny/api/security" |
| CLI Commands | CliCommandFactory |
"webiny/cli/command" |
| Pulumi Handlers | CorePulumi |
"webiny/infra/core" |
Event handlers use the same
createImplementationpattern but are not injectable dependencies.
Examples Across Extension Types
API Extension (GraphQL Schema with DI)
GraphQL schemas use the builder pattern. The execute method receives a builder and uses addTypeDefs and addResolver to define the schema. Resolver-level DI is declared per-resolver via dependencies in addResolver, resolved at request time from the request-scoped container.
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(/* Gr
Câu hỏi thường gặp
Mẫu này gồm những phần nào?
Một lớp triển khai interface, các phụ thuộc trong constructor, và factory được export cùng danh sách dependencies.
Nó có áp dụng cho mọi loại extension không?
Có. Skill nói mẫu này áp dụng cho extension API, admin, CLI và hạ tầng.
Những dịch vụ nào có thể được inject?
Ví dụ như logger, build parameters, identity context và các use case mục CMS.
Chi tiết
Mô tả gốc của tác giả
The universal createImplementation DI pattern and all injectable services. Use this skill when the developer is writing any Webiny extension and needs to understand dependency injection, constructor injection, how to access Logger/BuildParams/IdentityContext, how to inject CMS use-cases (list/get/create/update/delete entries), or how the dependencies array works. This is the connective tissue across all extension types -- API, Admin, CLI, and Infrastructure.
repo: webiny/webiny-js
Độ phổ biến
- Sao GitHub
- 8.1k
- Vị trí xếp hạng
- #12599
- Giấy phép
- —
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
prd-to-plan
8.1kGiúp agent của bạn biến tài liệu yêu cầu sản phẩm thành kế hoạch theo giai đoạn gồm các lát cắt đầu-cuối nhỏ, lưu thành file Markdown.
webiny-admin-architect
8.1kDạy agent của bạn cách xây dựng màn hình quản trị cho nền tảng Webiny, dùng các tầng logic nghiệp vụ và mẫu presenter.
webiny-admin-permissions
8.1kDạy agent của bạn thêm kiểm soát quyền vào màn hình quản trị Webiny, gồm form quyền tự sinh và kiểm tra ẩn hoặc hiện.
webiny-form-model
8.1kDạy agent của bạn xây dựng form trong Webiny bằng trình tạo trường khai báo, bố cục, kiểm tra hợp lệ và quy tắc có điều kiện.
Skill tương tự
api-connector-builder
280kGiúp agent của bạn xây dựng kết nối API mới theo đúng mẫu đã có, dùng khi bạn cần thêm một tích hợp mà không tạo cách làm mới.
api-design
280kGiúp agent của bạn thiết kế REST API theo các thực hành tốt nhất, hữu ích để định nghĩa cách API giao tiếp, hiển thị lỗi và tổ chức dữ liệu.
backend-patterns
280kGiúp agent của bạn với các mẫu kiến trúc phía máy chủ và tối ưu cơ sở dữ liệu, hữu ích khi xây dựng hoặc rà soát code chạy trên máy chủ.