Refacto rest api, fix graphl playground, improve analytics (#5844)

- Improve the rest api by introducing startingAfter/endingBefore (we
previously had lastCursor), and moving pageInfo/totalCount outside of
the data object.
- Fix broken GraphQL playground on website
- Improve analytics by sending server url
This commit is contained in:
Félix Malfait
2024-06-12 21:54:33 +02:00
committed by GitHub
parent 04edf2bf7b
commit 374237a988
22 changed files with 277 additions and 131 deletions
@@ -1,16 +1,39 @@
// https://gist.github.com/ManUtopiK/469aec75b655d6a4d912aeb3b75af3c9
export const cleanGraphQLResponse = (input: any) => {
if (!input) return null;
const output = {};
const output = { data: {} }; // Initialize the output with a data key at the top level
const isObject = (obj: any) => {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
};
const cleanObject = (obj: any) => {
const cleanedObj = {};
Object.keys(obj).forEach((key) => {
if (isObject(obj[key])) {
if (obj[key].edges) {
// Handle edges by mapping over them and applying cleanObject to each node
cleanedObj[key] = obj[key].edges.map((edge) =>
cleanObject(edge.node),
);
} else {
// Recursively clean nested objects
cleanedObj[key] = cleanObject(obj[key]);
}
} else {
// Directly assign non-object properties
cleanedObj[key] = obj[key];
}
});
return cleanedObj;
};
Object.keys(input).forEach((key) => {
if (input[key] && input[key].edges) {
output[key] = input[key].edges.map((edge) =>
cleanGraphQLResponse(edge.node),
);
if (isObject(input[key]) && input[key].edges) {
// Handle collections with edges, ensuring data is placed under the data key
output.data[key] = input[key].edges.map((edge) => cleanObject(edge.node));
// Move pageInfo and totalCount to the top level
if (input[key].pageInfo) {
output['pageInfo'] = input[key].pageInfo;
}
@@ -18,9 +41,11 @@ export const cleanGraphQLResponse = (input: any) => {
output['totalCount'] = input[key].totalCount;
}
} else if (isObject(input[key])) {
output[key] = cleanGraphQLResponse(input[key]);
} else if (key !== '__typename') {
output[key] = input[key];
// Recursively clean and assign nested objects under the data key
output.data[key] = cleanObject(input[key]);
} else {
// Assign all other properties directly under the data key
output.data[key] = input[key];
}
});