> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wyrly.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Framework adapters

> Use Wyrly DI with Next.js, Express, Hono, Fresh, and GraphQL.

Wyrly keeps framework integration thin. The core package owns tokens, providers, containers, scopes,
validation, and graph inspection. Adapters create request scopes and expose them in framework-native
ways.

## Next.js App Router

Install:

```sh theme={null}
npm install @wyrly/next @wyrly/core next react
```

Use `withDI` for Route Handlers:

```ts theme={null}
import { createContainer } from "@wyrly/core";
import { withDI } from "@wyrly/next";

const container = createContainer();

export const GET = withDI(container, async (_req, { di }) => {
  const usecase = di.resolve(MyUseCase);
  return Response.json(await usecase.run());
});
```

The Next.js adapter also provides helpers for Server Actions and Server Components.

## Express

Use the Express adapter to create one scope per request and access it from the request object.

```ts theme={null}
import { diMiddleware } from "@wyrly/express";

app.use(diMiddleware(container));
```

## Hono

Use the Hono adapter for edge-friendly request scopes.

```ts theme={null}
import { di, getDI } from "@wyrly/hono";

app.use("*", di(container));

app.get("/users/:id", async (c) => {
  const scope = getDI(c);
  const usecase = scope.resolve(GetUserUseCase);
  return c.json(await usecase.execute(c.req.param("id")));
});
```

## Fastify

`@wyrly/fastify` provides `diPlugin` for Fastify 5 (one scope per request, auto dispose on response
`finish` / `close`). Peer: `fastify` ^5. The plugin uses `fastify-plugin` so hooks apply to routes
on the root instance.

| Resource | Link                                                                                        |
| -------- | ------------------------------------------------------------------------------------------- |
| Package  | [`packages/fastify`](https://github.com/valid-lab/wyrly/tree/main/packages/fastify)         |
| Example  | [`examples/fastify-api`](https://github.com/valid-lab/wyrly/tree/main/examples/fastify-api) |

```ts theme={null}
import Fastify from "fastify";
import { diPlugin, getDI } from "@wyrly/fastify";

const app = Fastify();
await app.register(diPlugin(container));

app.get("/users/:id", async (request) => {
  const useCase = getDI(request).resolve(GetUserUseCase);
  return { id: await useCase.execute(request.params.id) };
});
```

For GraphQL on Fastify, combine with [`@wyrly/apollo`](https://github.com/valid-lab/wyrly/tree/main/packages/apollo) or [`@wyrly/yoga`](https://github.com/valid-lab/wyrly/tree/main/packages/yoga).

## Fresh

Use `@wyrly/fresh` for Fresh 2.x applications. The package is published on JSR.

```sh theme={null}
deno add jsr:@wyrly/fresh
```

## GraphQL

Use `@wyrly/graphql` when each GraphQL request should own a DI scope and request-local loaders.

```ts theme={null}
import { createGraphQLDIContext } from "@wyrly/graphql";

const ctx = await createGraphQLDIContext(container, {
  request,
  configureScope(scope) {
    scope.set(CurrentUserToken, { id: userId });
  },
});

try {
  return await runResolvers(ctx.di);
} finally {
  await ctx.dispose();
}
```

This is useful for DataLoader instances, request context, authentication state, and disposable
resources.

### GraphQL Yoga

`@wyrly/yoga` provides `yogaDIPlugin` and `yogaContext` for GraphQL Yoga 5 (one scope per
request, auto dispose on `onResponse`). Peer: `graphql-yoga` ^5.

| Resource | Link                                                                                          |
| -------- | --------------------------------------------------------------------------------------------- |
| Package  | [`packages/yoga`](https://github.com/valid-lab/wyrly/tree/main/packages/yoga)                 |
| Example  | [`examples/yoga-graphql`](https://github.com/valid-lab/wyrly/tree/main/examples/yoga-graphql) |
| Guide    | [GRAPHQL\_DISPOSE](https://github.com/valid-lab/wyrly/blob/main/guides/GRAPHQL_DISPOSE.md)    |

### Apollo Server

`@wyrly/apollo` provides `apolloDIPlugin` for Apollo Server 4+ (one scope per operation, auto
dispose in `willSendResponse`). Peers: `@apollo/server` ^4, `graphql` ^16.

| Resource         | Link                                                                                                              |
| ---------------- | ----------------------------------------------------------------------------------------------------------------- |
| Package          | [`packages/apollo`](https://github.com/valid-lab/wyrly/tree/main/packages/apollo)                                 |
| Example          | [`examples/apollo-graphql`](https://github.com/valid-lab/wyrly/tree/main/examples/apollo-graphql)                 |
| Express + Apollo | [`examples/apollo-express-graphql`](https://github.com/valid-lab/wyrly/tree/main/examples/apollo-express-graphql) |
| Guide            | [GRAPHQL\_DISPOSE](https://github.com/valid-lab/wyrly/blob/main/guides/GRAPHQL_DISPOSE.md)                        |

## Pick the adapter at the edge

Keep framework adapters in the outer layer of your application. Domain objects and use cases should
depend on ports and value objects, not on `Request`, `Response`, or framework context objects.
