The @inject directive enables dependency injection in GraphQL schemas by allowing one field to inject expansion variables into other fields within the same request.
- A field annotated with
@injectis resolved first when any target field is accessed - The injecting field must return an object with key-value pairs
- These pairs become expansion variables available to target fields
- Target fields can access these variables using the standard expansion variable syntax
- The
onargument specifies which fields have access to the injected variables
# Inject electronic product (JSON) data as expansion variables available to any Query field
_inject_electronic_products: JSON
@inject(on: [{ expose: true, types: "Query", fields: ".*" }])
@materializer(
query: "products"
arguments: { name: "category", const: "electronics" }
)- user-context - Demonstrates simple user context injection for regional e-commerce operations with role-based filtering and currency conversion.