本文へスキップ
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に貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/webiny/webiny-js/blob/next/skills/user-skills/dependency-injection/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に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

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル