Optimiser les performances et simplifier l'architecture
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.
@Resolver(() => Cwt)
export class CwtFieldResolver {
@FieldResolver(() => Document, { nullable: true })
document(@Root() cwt: Cwt, @Ctx() { dataloaders }) {
if (!cwt.documentPath) return null;
return dataloaders.documentLoaders
.documentPathLoader.load(cwt.documentPath)
}
@FieldResolver(() => User, { nullable: true })
user(@Root() cwt: Cwt, @Ctx() { dataloaders }) {
if (!cwt.userId) return null;
return dataloaders.usersLoader
.getUserLoader.load(cwt.userId)
}
}
// â Ancien code - Use Case lourd
export class GetCwtListUsecase {
 constructor(
  private readonly cwtRepository,
  private readonly mulGateway,     // Dépendance
  private readonly documentGateway   // Dépendance
 ) {}
 async execute(filters) {
  const cwts = await this.cwtRepository.getList(filters)
  // Appels directs aux gateways
  for (const cwt of cwts) {
   cwt.document = await this.documentGateway
    .getDocument(cwt.documentPath)
   cwt.merchant = await this.mulGateway
    .getMerchant(cwt.merchantId)
  }
  return cwts;
 }
}
// Frontend demande :
{ id, amount }
// API retourne TOUT :
{
 "id": "123",
 "amount": 1000,
 "documentPath": "/docs/file.pdf",
 "merchantName": "Merchant Corp",
 "beneficiaryDetails": {...},
 "createdAt": "2024-01-01",
 "updatedAt": "2024-01-15"
}
# Frontend demande :
query GetCwtList {
 cwts { id, amount }
}
# GraphQL retourne exactement :
{
 "data": {
  "cwts": [
   { "id": "123", "amount": 1000 }
  ]
 }
}
1 requĂȘte liste + N requĂȘtes dĂ©tails = Performance catastrophique
// DataLoader qui appelle une API REST
class DocumentDataLoader {
constructor(private restClient: RestClient) {}
async batchLoad(documentPaths: string[]) {
// Appel REST optimisé en batch
const response = await this.restClient.post('/documents/batch', {
paths: documentPaths
})
return response.documents
}
}
// N appels individuels
const user1 = await getUserById(1)
const user2 = await getUserById(2)
const user3 = await getUserById(3)
// 1 seul appel groupé
const [user1, user2, user3] =
await userLoader.loadMany([1, 2, 3])
Merci pour votre attention ! đ