跳到正文
Skillpedia
API 与服务器(后端)8.1k · #12599

webiny-dependency-injection

它能做什么

这个技能讲解每个 Webiny 扩展用来获取服务的通用模式。适用于编写任何 Webiny 扩展、需要了解代码如何获得依赖项的开发者。

适用场景

  • ✓编写任何 Webiny 扩展并需要访问日志或身份上下文的时候。
  • ✓需要在代码中调用 CMS 创建、更新或删除操作的时候。
  • ✓不确定依赖列表如何工作的时候。

技能链接

https://github.com/webiny/webiny-js/blob/next/skills/user-skills/dependency-injection/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/webiny/webiny-js/blob/next/skills/user-skills/dependency-injection/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请给我看看如何在 Webiny 扩展类中注入日志记录器和 CMS 条目用例。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能说明每种扩展类型都遵循相同步骤:编写实现对应接口的类,在构造函数中声明依赖项,并使用对应的工厂和依赖列表导出。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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:

  1. One class per file -- each extension file exports a single implementation.
  2. Constructor injection -- dependencies are received as constructor parameters, in the same order as the dependencies array.
  3. Dependencies array -- must exactly match the constructor parameter order and types.
  4. 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 createImplementation pattern 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
常见问题

这个模式包含哪些部分?

实现接口的类、构造函数中的依赖项,以及带依赖列表的导出工厂。

它适用于所有扩展类型吗?

是的。该技能说明它适用于 API、管理后台、CLI 和基础设施扩展。

哪些服务可以注入?

例如日志记录器、构建参数、身份上下文和 CMS 条目用例。

详情

作者的原始描述

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

热度

GitHub 星标
8.1k
排名
#12599
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能