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:
@ -12,7 +12,9 @@ describe('cleanGraphQLResponse', () => {
|
||||
},
|
||||
};
|
||||
const expectedResult = {
|
||||
companies: [{ id: 'id', createdAt: '2023-01-01' }],
|
||||
data: {
|
||||
companies: [{ id: 'id', createdAt: '2023-01-01' }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(cleanGraphQLResponse(data)).toEqual(expectedResult);
|
||||
@ -20,6 +22,13 @@ describe('cleanGraphQLResponse', () => {
|
||||
it('should remove nested edges/node from results', () => {
|
||||
const data = {
|
||||
companies: {
|
||||
totalCount: 14,
|
||||
pageInfo: {
|
||||
hasNextPage: true,
|
||||
startCursor:
|
||||
'WyIwMDliYjNkYy1hNGEyLTRiNWUtYTZmYi1iMTFiMmFlMGI1MmIiXQ==',
|
||||
endCursor: 'WyIyMDIwMjAyMC0wNzEzLTQwYTUtODIxNi04MjgwMjQwMWQzM2UiXQ==',
|
||||
},
|
||||
edges: [
|
||||
{
|
||||
node: {
|
||||
@ -34,20 +43,33 @@ describe('cleanGraphQLResponse', () => {
|
||||
},
|
||||
};
|
||||
const expectedResult = {
|
||||
companies: [
|
||||
{
|
||||
id: 'id',
|
||||
createdAt: '2023-01-01',
|
||||
people: [{ id: 'id1' }, { id: 'id2' }],
|
||||
},
|
||||
],
|
||||
data: {
|
||||
companies: [
|
||||
{
|
||||
id: 'id',
|
||||
createdAt: '2023-01-01',
|
||||
people: [{ id: 'id1' }, { id: 'id2' }],
|
||||
},
|
||||
],
|
||||
},
|
||||
totalCount: 14,
|
||||
pageInfo: {
|
||||
hasNextPage: true,
|
||||
startCursor: 'WyIwMDliYjNkYy1hNGEyLTRiNWUtYTZmYi1iMTFiMmFlMGI1MmIiXQ==',
|
||||
endCursor: 'WyIyMDIwMjAyMC0wNzEzLTQwYTUtODIxNi04MjgwMjQwMWQzM2UiXQ==',
|
||||
},
|
||||
};
|
||||
|
||||
expect(cleanGraphQLResponse(data)).toEqual(expectedResult);
|
||||
});
|
||||
it('should not format when no list returned', () => {
|
||||
const data = { company: { id: 'id' } };
|
||||
const expectedResult = {
|
||||
data: {
|
||||
company: { id: 'id' },
|
||||
},
|
||||
};
|
||||
|
||||
expect(cleanGraphQLResponse(data)).toEqual(data);
|
||||
expect(cleanGraphQLResponse(data)).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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];
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user