Skip to main content
Version: Next (2.x)

First Module

Learn how to create your first GraphQL module with Baeta. We'll create a simple user module to demonstrate Baeta's schema-first and modular approach.

Creating a Module

1. Define the schema

Create your first schema file at src/modules/user/user.gql:

type User {
id: ID!
name: String!
}

type Query {
user(id: ID): User
}
tip

You can split your schema into multiple files (types, inputs, queries) as your module grows.

2. Generate the types

Generate TypeScript definitions for your schema:

yarn generate
# or
yarn baeta generate

This creates src/modules/user/typedef.ts with all necessary type definitions.

tip

Baeta can watch for changes with the --watch flag.

3. Create the resolvers

Create src/modules/user/resolvers.ts to implement your query:

import { UserModule } from "./typedef.ts";

const { Query, User } = UserModule;

export const userResolver = User.$fields({
id: User.id.key("id"),
name: User.name.key("name"),
});

export const queryResolver = Query.$fields({
user: Query.user.resolve(({ args }) => {
return {
id: args.id,
name: "John Doe",
};
}),
});
tip

The key is a helper that picks the specified field from the source object and returns it.

By default the source of a type is the same as the GraphQL type itself, but the source can always be overridden.

4. Export the module

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

import { queryResolver, userResolver } from "./resolvers.ts";
import { UserModule } from "./typedef.ts";

export default UserModule.$schema({
User: userResolver,
Query: queryResolver,
});

Project Structure

Your module should now look like this:

src/modules/user/
├── user.gql # Schema definition
├── resolvers.ts # Query/Mutation implementations
├── typedef.ts # Auto-generated types
└── index.ts # Module export

Next Steps

Now that you have your first module, learn how to extend it with another module.