Open Carduelis opened 1 year ago
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:
providers
folder, kept only buildDataProvider
and AdminQueries.ts
, Filter.ts
, Pagination.ts
;utils.ts
file;getQueryName
and getQueryNameMany
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;
};
Hi @MrHertal !
I realized in latest amplify it generates queries like:
dataPointsByMetricID
instead oflistDatapointsByMetricId
which is generated viagetQueryName
andgetQueryNameMany
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:
DataProvider
customizable includinggetQueryName
getQueryName
according to recent updatesAlso, I would suggest not using
getQueryName
and similar functions as class methods preferring simple functions.