webiny-dependency-injection

Implements constructor-based dependency injection for Webiny extensions using the createImplementation pattern.

8.0k|673|Updated Jan 9, 2018
One-click install
npx skills add https://github.com/webiny/webiny-js --skill webiny-dependency-injection
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: webiny-dependency-injection
Source: https://github.com/webiny/webiny-js/tree/main/skills/user-skills/dependency-injection
Command: npx skills add https://github.com/webiny/webiny-js --skill webiny-dependency-injection

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Webiny extensions need a consistent way to access services like Logger, BuildParams, IdentityContext, and CMS use-cases without manual wiring. This Skill documents the universal DI pattern so developers can correctly declare dependencies, register implementations, and resolve services across API, Admin, CLI, and Infrastructure extensions.

Core Features & Use Cases

  • Universal createImplementation Pattern: Define a class implementing a factory Interface, declare constructor dependencies, and export via createImplementation with a matching dependencies array.
  • Advanced Injection Options: Supports optional dependencies, multi-injection of all registered implementations, decorators, composites, and singleton vs transient lifetime scopes.
  • Use Case: When building a custom GraphQL schema extension, use resolver-level DI via addResolver with dependencies like IdentityContext to access the current user's identity at request time.

Quick Start

Show me how to create a Webiny CLI command extension that injects the Ui service using the createImplementation dependency injection pattern.

Frequently Asked Questions about webiny-dependency-injection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I inject dependencies into a Webiny extension?

Define a class implementing the factory's Interface, declare dependencies as constructor parameters typed as Feature.Interface, and export via createImplementation with a dependencies array matching the constructor parameter order exactly.

How do I inject multiple implementations of the same abstraction in Webiny?

Use [Abstraction, { multiple: true }] in the dependencies array and type the constructor parameter as T[]. The container calls resolveAll internally and injects all registered implementations in registration order.

Can I make a Webiny dependency optional?

Yes, use [Abstraction, { optional: true }] in the dependencies array. The container injects undefined instead of throwing when the abstraction is not registered, so type the constructor parameter as possibly undefined.

What is the difference between transient and singleton scope in Webiny DI?

Transient is the default and creates a new instance on every resolve. Singleton, enabled via .inSingletonScope() during registration, caches the instance after first resolution. Convention: use cases are transient, repositories and services are singleton.

Why does BuildParams.get return null in Webiny extensions?

BuildParams.get<T>(name) returns T | null because build parameters may not be set. Always type the receiving variable as nullable and handle the null case before using the value.