Langsung ke konten
Skillpedia
API & server (backend)8.1k · #12599

webiny-dependency-injection

Fungsinya

Skill ini menjelaskan pola bersama yang dipakai setiap ekstensi Webiny untuk mendapatkan layanannya. Skill ini untuk developer yang menulis ekstensi Webiny apa pun dan perlu memahami cara kodenya mendapatkan dependensi.

Kapan dipakai

  • ✓Saat Anda menulis ekstensi Webiny dan perlu mengakses logger atau konteks identitas.
  • ✓Saat Anda perlu memanggil operasi create, update, atau delete CMS dari kode Anda.
  • ✓Saat Anda belum yakin cara kerja daftar dependencies.

Link skill

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

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/webiny/webiny-js/blob/next/skills/user-skills/dependency-injection/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit →

Contoh permintaan ke AI

“Tunjukkan cara menginjeksi logger dan use case entri CMS ke kelas ekstensi Webiny saya.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill ini menjelaskan langkah yang sama untuk setiap jenis ekstensi: tulis kelas yang mengimplementasikan interface yang sesuai, deklarasikan dependensinya di constructor, lalu ekspor dengan factory dan daftar dependencies yang cocok.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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
Pertanyaan umum

Apa saja bagian dari pola ini?

Kelas yang mengimplementasikan interface, dependensi di constructor, dan factory yang diekspor beserta daftar dependencies-nya.

Apakah pola ini berlaku untuk semua jenis ekstensi?

Ya. Skill ini menyatakan pola ini berlaku untuk ekstensi API, admin, CLI, dan infrastruktur.

Layanan apa saja yang bisa diinjeksi?

Contohnya logger, parameter build, konteks identitas, dan use case entri CMS.

Detail

Deskripsi asli dari pembuat

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

Popularitas

Bintang GitHub
8.1k
Peringkat
#12599
Lisensi
—
Diperbarui
2/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa