This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class CommandHandler<T extends Command<unknown>> { | |
constructor(public readonly type: string) {} | |
public abstract async execute(command: T); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getOrCreateProduct(data: ProductData) { | |
let existingProduct = productRepository.findByAttributes(data); | |
if (!existingProduct) { | |
existingProduct = productRepository.save(data); | |
} | |
return existingProduct; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Command<T> { | |
constructor(public readonly type: string, public readonly payload: T) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface RegisterCustomerPayload { | |
email: string; | |
password: string; | |
name: string; | |
} | |
export const REGISTER_CUSTOMER_COMMAND_TYPE = "customer/register"; | |
class RegisterCustomerCommand extends Command< | |
RegisterCustomerPayload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Dependencies { | |
customerRepository: CustomerRepository; | |
} | |
class RegisterCustomerCommandHandler extends CommandHandler< | |
RegisterCustomerCommand | |
> { | |
constructor(private readonly dependencies: Dependencies) { | |
super(REGISTER_CUSTOMER_COMMAND_TYPE); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class CommandHandler<T extends Command<unknown>> { | |
constructor(public readonly type: string) {} | |
public abstract async execute(command: T); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface GetUserEnrolledDecksQueryHandlerDependencies { | |
enrolledDeckRepository: EnrolledDeckRepository; | |
deckElasticView: ElasticView<Deck>; | |
storageClient: StorageClient; | |
} | |
export class GetUserEnrolledDecksQueryHandler extends QueryHandler< | |
GetUserEnrolledDecks, | |
GetUserEnrolledDecksQueryResult | |
> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommandBus { | |
constructor(private readonly handlers: CommandHandler<any>[]) {} | |
public async execute(command: Command<unknown>) { | |
const handler = this.handlers.find( | |
(existingHandler) => existingHandler.type === command.type | |
); | |
if (!handler) { | |
throw new Error(`Handler for command: ${command.type} does not exist.`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const registerCustomerAction = ( | |
payload: RegisterCustomerPayload, | |
context: ApplicationContext | |
) => context.commandBus.execute(new RegisterCustomerCommand(payload)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const resolvers = { | |
Mutation: { | |
addCardToDeck, | |
removeCardFromDeck, | |
}, | |
Query: { | |
getCardsForDeck, | |
}, | |
}; |