react-admin-amplify
react-admin-amplify copied to clipboard
How to alter `getQueryName` and `getQueryNameMany`?
Hi @MrHertal !
I realized in latest amplify it generates queries like: dataPointsByMetricID
instead of listDatapointsByMetricId
which is generated via getQueryName
and getQueryNameMany
functions.
Although, there is no way to alter those functions, and DataProvider
is not exported nor its parts.
There are two options, which might be helpful:
- Make
DataProvider
customizable includinggetQueryName
- Update
getQueryName
according to recent updates
Also, I would suggest not using getQueryName
and similar functions as class methods preferring simple functions.
After some digging, I realized the following issues with the current approach to query name generation.
First, of all, I do believe the assumption of capitalizing and joining targets, sources, and resources is error-prone.
Since, the idea of generation hundreds of GraphQL queries, provided by amplify-cli
is great and it is a subject of constant change, we need to have the ability to override behavior in this library. Otherwise, we had to copy the code of providers
folder (thanks it's in TS) and modify it.
Also, the method getQueryNameMany
trims the last two letters, and adds Id
at the end. Which is, probably, was right in your case, but is wrong in mine. I have a query, ended with ID
(both letters uppercased)
So, I came up with the following solution:
- I copied the
providers
folder, kept onlybuildDataProvider
andAdminQueries.ts
,Filter.ts
,Pagination.ts
; - Created
utils.ts
file; - Modified
getQueryName
andgetQueryNameMany
public methods;
// DataProvider.ts
public getQueryName(operation: string, resource: string): string {
const pluralOperations = ["list"];
if (pluralOperations.includes(operation)) {
return `${operation}${capitalize(resource)}`;
}
// else singular operations ["create", "delete", "get", "update"]
const singularResource = depluralize(resource);
return `${operation}${capitalize(singularResource)}`;
}
public getQueryNameMany(
operation: string,
resource: string,
target: string
): string {
return `${capitalize(resource)}By${capitalize(target)}`;
}
export const capitalize = (string: string): string => {
return `${string[0].toUpperCase()}${string.slice(1)}`;
};
export const depluralize = (string: string): string => {
if (string.slice(-1) === "s") {
return string.slice(0, -1);
}
return string;
};