Skip to main content

Extend modules

One of the powerful features of Baeta is the ability to extend existing modules. In this guide, we'll extend our user module by adding photo capabilities.

1. Define Extension Schema

Create a new schema file at src/modules/user-photos/user-photos.gql:

type UserPhoto {
id: ID!
url: String!
}

extend type User {
photos: [UserPhoto!]
}
tip

Use the extend keyword to add new fields to existing types from other modules.

2. Generate Types

Generate TypeScript definitions for your new schema:

yarn generate
tip

If you are using the watch mode, types will be automatically generated on each change.

3. Add Resolver

Create src/modules/user-photos/resolvers.ts to implement the photos field:

import { getUserPhotosModule } from "./typedef";

const { User } = getUserPhotosModule();

User.photos(({ args, root, info, ctx }) => {
return [
{
id: "1",
url: "https://baeta.io/img/logo.svg",
},
];
});

4. Export Module

Create src/modules/user-photos/index.ts to export your extension module:

import "./resolvers";
import { getUserPhotosModule } from "./typedef";

export const userPhotosModule = getUserPhotosModule();
tip

Remember that @baeta/plugin-autoload can automatically handle module loading and exports for you.

Project Structure

Your extended modules should now look like this:

src/modules/
├── user/
│ ├── user.gql
│ ├── resolvers.ts
│ ├── typedef.ts
│ └── index.ts
└── user-photos/
├── user-photos.gql
├── resolvers.ts
├── typedef.ts
└── index.ts

Next Steps

Let's wrap everything up and see how to use our modules.