Skip to content

Commit

Permalink
AbstractModelService: make create/update/delete query names customiza…
Browse files Browse the repository at this point in the history
…ble #10387

Because it cannot always be determined from the service name
By default, still derived from the service name
  • Loading branch information
stissot committed May 15, 2024
1 parent 0968914 commit 8886074
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions projects/natural/src/lib/services/abstract-model.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ export abstract class NaturalAbstractModelService<
protected readonly naturalDebounceService = inject(NaturalDebounceService);
readonly #plural: string;

/**
*
* @param name service and single object query name (eg. userForFront or user).
* @param oneQuery GraphQL query to fetch a single object from ID (eg. userForCrudQuery).
* @param allQuery GraphQL query to fetch a filtered list of objects (eg. usersForCrudQuery).
* @param createMutation GraphQL mutation to create an object.
* @param updateMutation GraphQL mutation to update an object.
* @param deleteMutation GraphQL mutation to delete a list of objects.
* @param plural list query name (eg. usersForFront or users).
* @param createName create object mutation name (eg. createUser).
* @param updateName update object mutation name (eg. updateUser).
* @param deleteName delete object mutation name (eg. deleteUsers).
*/
public constructor(
protected readonly name: string,
protected readonly oneQuery: DocumentNode | null,
Expand All @@ -54,6 +67,9 @@ export abstract class NaturalAbstractModelService<
protected readonly updateMutation: DocumentNode | null,
protected readonly deleteMutation: DocumentNode | null,
plural: string | null = null,
protected readonly createName: string | null = null,
protected readonly updateName: string | null = null,
protected readonly deleteName: string | null = null,
) {
this.#plural = plural ?? makePlural(this.name);
}
Expand Down Expand Up @@ -549,23 +565,23 @@ export abstract class NaturalAbstractModelService<
* This is used to extract only the created object out of the entire fetched data
*/
protected mapCreation(result: MutationResult<unknown>): Tcreate {
const name = 'create' + upperCaseFirstLetter(this.name);
const name = this.createName ?? 'create' + upperCaseFirstLetter(this.name);
return (result.data as any)[name]; // See https://github.com/apollographql/apollo-client/issues/5662
}

/**
* This is used to extract only the updated object out of the entire fetched data
*/
protected mapUpdate(result: MutationResult<unknown>): Tupdate {
const name = 'update' + upperCaseFirstLetter(this.name);
const name = this.updateName ?? 'update' + upperCaseFirstLetter(this.name);
return (result.data as any)[name]; // See https://github.com/apollographql/apollo-client/issues/5662
}

/**
* This is used to extract only flag when deleting an object
*/
protected mapDelete(result: MutationResult<unknown>): Tdelete {
const name = 'delete' + upperCaseFirstLetter(this.#plural);
const name = this.deleteName ?? 'delete' + upperCaseFirstLetter(this.#plural);
return (result.data as any)[name]; // See https://github.com/apollographql/apollo-client/issues/5662
}

Expand Down

0 comments on commit 8886074

Please sign in to comment.