Autoloading
Baeta provides an autoloading plugin that automatically discovers and loads your modules and resolvers, eliminating the need for manual registration.
Installation
- yarn
- npm
- pnpm
- bun
yarn add @baeta/plugin-autoload -D
npm install @baeta/plugin-autoload -D
pnpm add @baeta/plugin-autoload -D
bun add @baeta/plugin-autoload -D
Configuration
Enable autoloading in your baeta.ts
:
import { defineConfig } from "@baeta/cli";
import { autoloadPlugin } from "@baeta/plugin-autoload";
export default defineConfig({
// ... other config
plugins: [autoloadPlugin()],
});
warning
For the plugin to work correctly, it should be registered before plugins that generate GraphQL files, modules or resolvers.
Project Structure
The autoloader expects a specific project structure:
src/
├── modules/
│ ├── user/
│ │ ├── user.gql
│ │ ├── resolvers.ts
│ │ └── typedef.ts
│ ├── post/
│ │ ├── post.gql
│ │ ├── post.resolvers.ts // The suffix is what matters
│ │ └── typedef.ts
│ ├── scalars/
│ │ ├── scalars.gql
│ │ ├──scalars.resolvers.ts
│ │ └── typedef.ts
│ └── autoload.ts // Generated by the plugin
└── baeta.ts
How It Works
With autoloading enabled:
- No need to create index.ts files
- No need to manually import resolvers
- No need to export modules
- No need to register resolvers in your application setup
The plugin will:
- Discover all .gql files in your modules directory
- Load corresponding resolver files
- Create
autoload.ts
with resolver registration and modules exports
Just import the autoload.ts
file in your application setup:
import { modules } from "./modules/autoload.ts";
const baeta = createApplication({
modules,
});
Without Autoloading
Without the plugin, you would need to manually:
// src/modules/user/index.ts
import "./resolvers";
import { getUserModule } from "./typedef";
export const userModule = getUserModule();
// src/app.ts
import { userModule } from "./modules/user";
import { postModule } from "./modules/post";
import { scalarsModule } from "./modules/scalars";
// Register each module manually
const baeta = createApplication({
modules: [userModule, postModule, scalarsModule],
});
Default Resolver Suffixes
By default, the plugin looks for files with these suffixes:
const defaultSuffixes = [
"auth",
"authorization",
"authorizations",
"cache",
"caches",
"resolver",
"resolvers",
"query",
"queries",
"mutation",
"mutations",
"subscription",
"subscriptions",
"baeta",
];
This means files like user.resolver.ts
, auth.ts
, or queries.ts
will be automatically loaded.
Configuration Options
Resolver Options
interface ResolverOptions {
// Custom suffixes to look for
suffix?: string | string[];
// Disable the default suffixes
disableDefaultSuffixes?: boolean;
// Custom matching function
match?: (filename: string) => boolean;
}
Module Options
interface ModuleOptions {
// Custom matching function for module names
match?: (moduleName: string) => boolean;
}
Autoload Plugin Options
interface AutoloadPluginOptions {
// Enable/disable resolver autoloading
resolvers?: boolean | ResolverOptions;
// Enable/disable module autoloading
modules?: boolean | ModuleOptions;
// Output path for generated files
output?: string;
}