-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(graphql drafts): attempt at removing __typename from transformed fields for graphql drafts #444
feat(graphql drafts): attempt at removing __typename from transformed fields for graphql drafts #444
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-test-data/core': minor | ||
--- | ||
|
||
feat(graphql drafts): remove \_\_typename added by builders of transformed fields from graphql drafts using isGrpahqlDraft param |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,33 @@ const upperFirst = (value: string): string => | |
const lowerFirst = (value: string): string => | ||
value.charAt(0).toLowerCase() + value.slice(1); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function deleteKeyFromObject(inputObject: any, keyToDelete: string) { | ||
for (let [currentObjectKey, currentObjectValue] of Object.entries( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iterate over all keys in the current object |
||
inputObject | ||
)) { | ||
if (currentObjectKey === keyToDelete) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the key is the one we want to delete, delete it |
||
delete inputObject[keyToDelete]; | ||
} else if (Array.isArray(currentObjectValue)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise, if the key's value is an array, send it to the fn that handles arrays |
||
deleteKeyFromObjectInArray(currentObjectValue, keyToDelete); | ||
} else if (isObject(currentObjectValue)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise, if the key's value is an object, run this function over the object |
||
deleteKeyFromObject(currentObjectValue, keyToDelete); | ||
} | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I know - any help/suggestions for typing this in a more specific manner would be appreciated. |
||
function deleteKeyFromObjectInArray(inputArray: any[], keyToDelete: string) { | ||
for (let currentIndex = 0; currentIndex < inputArray.length; currentIndex++) { | ||
let currentElement = inputArray[currentIndex]; | ||
if (Array.isArray(currentElement)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it's an array in an array, run this function over the array |
||
deleteKeyFromObjectInArray(currentElement, keyToDelete); | ||
} else if (isObject(currentElement)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it's an object in the array, run the delete function over the object to remove the unwanted key |
||
deleteKeyFromObject(currentElement, keyToDelete); | ||
} | ||
} | ||
} | ||
|
||
const omitOne = <T, K extends keyof T>(entity: T, prop: K): Omit<T, K> => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { [prop]: deleted, ...newState } = entity; | ||
|
@@ -175,6 +202,7 @@ export { | |
isBuilderFunction, | ||
upperFirst, | ||
lowerFirst, | ||
deleteKeyFromObject, | ||
omitMany, | ||
pickMany, | ||
convertBuiltNameToTransformName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
"@commercetools-test-data/attribute-definition": "5.11.2", | ||
"@commercetools-test-data/category": "6.6.0", | ||
"@commercetools-test-data/commons": "6.6.0", | ||
"@commercetools-test-data/core": "6.6.0", | ||
"@commercetools-test-data/core": "link:../../../../core", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will need to be reverted, but is needed for testing changes to |
||
"@commercetools-test-data/product-type": "6.6.0", | ||
"@commercetools-test-data/product-variant": "5.11.2", | ||
"@commercetools-test-data/tax-category": "6.6.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,17 +180,20 @@ describe('builder', () => { | |
expect.objectContaining({ | ||
name: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
__typename: 'LocalizedString', | ||
locale: expect.any(String), | ||
value: expect.any(String), | ||
}), | ||
]), | ||
slug: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
__typename: 'LocalizedString', | ||
locale: expect.any(String), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
value: expect.any(String), | ||
}), | ||
]), | ||
description: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
__typename: 'LocalizedString', | ||
locale: expect.any(String), | ||
value: expect.any(String), | ||
}), | ||
]), | ||
categories: expect.arrayContaining([ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,7 +103,6 @@ describe(`with anniversaryShirt preset`, () => { | |
{ | ||
"categories": [ | ||
{ | ||
"__typename": "Reference", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all these snapshots were updated with |
||
"key": "tops-kids", | ||
"typeId": "category", | ||
}, | ||
|
@@ -129,29 +128,25 @@ describe(`with anniversaryShirt preset`, () => { | |
"metaTitle": undefined, | ||
"name": [ | ||
{ | ||
"__typename": "LocalizedString", | ||
"locale": "en-US", | ||
"value": "Sample Anniversary Shirt", | ||
}, | ||
], | ||
"priceMode": undefined, | ||
"productType": { | ||
"__typename": "Reference", | ||
"key": "shirts", | ||
"typeId": "product-type", | ||
}, | ||
"publish": false, | ||
"searchKeywords": undefined, | ||
"slug": [ | ||
{ | ||
"__typename": "LocalizedString", | ||
"locale": "en-US", | ||
"value": "sample-anniversary-shirt", | ||
}, | ||
], | ||
"state": undefined, | ||
"taxCategory": { | ||
"__typename": "Reference", | ||
"key": "standard-tax", | ||
"typeId": "tax-category", | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I know - any help/suggestions for typing this in a more specific manner would be appreciated.