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 the 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 the 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. Create the resolvers
Create src/modules/user-photos/resolvers.ts to implement the photos field:
import { UserPhotosModule } from "./typedef.ts";
const { UserPhoto, User } = UserPhotosModule;
export const userPhotosResolver = UserPhoto.$fields({
id: UserPhoto.id.key("id"),
url: UserPhoto.url.key("url"),
});
export const userResolver = User.$fields({
photos: User.photos.resolve(() => {
return [
{
id: "1",
url: "https://example.com/photo.jpg",
},
];
}),
});
4. Export the module
Create src/modules/user-photos/index.ts to export your extension module:
import { userPhotosResolver, userResolver } from "./resolvers.ts";
import { UserPhotosModule } from "./typedef.ts";
export default UserPhotosModule.$schema({
UserPhoto: userPhotosResolver,
User: userResolver,
});
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
└── index.ts # All modules are automatically collected and exported by Baeta
Next Steps
Let's wrap everything up and see how to use our modules.