Folder structure
A database first BFF should have the following structure:
- Query: code related to making queries (getting data)
- Command: code related to making commands (a change)
- Infra: infra related code (servers, models, etc)
- Utils: general tools/shared code
- Listener: mappers for events that the BFF is consuming
- Trigger: rules for events that the BFF is publishing
Query folder (src/query)
The query folder holds the code related to making queries. Below details the structure.
Query folder example structure
- query
- initApplication.ts
- getSomething
- index.ts: exports getSomethingController
- return-type.ts: exports GetSomethingReturnType
- getSomethingElse
- index.ts: exports getSomethingElseController
- return-type.ts: exports GetSomethingElseReturnType
initApplication.ts (src/query/initApplication)
This file is responsible for initializing the application container.
A folder for each query resolver
Each graphql query resolver should have its own resolver. For example, take a look at the following queries in the resolver:
type Input<T> = { input: T }
export const resolvers = {
Query: {
getSomething: (_: DocumentNode, { input }: Input<{ id: string }>) => {
return getPostController(input.id)
},
getSomethingElse: (_: DocumentNode, { input }: Input<{ id: string }>) => {
return getUserPostsController(input.id)
}
}
...
}
Each of the above queries should have it's own folder:
- src/query/getSomething
- src/query/getSomethingElse
Each folder should have the following files:
index.ts
- the controller for the queryreturn-type.ts
- the return type of the query