-
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
Conversation
… fields for graphql drafts
🦋 Changeset detectedLatest commit: 6e649f7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…all nested arrays/objects in draft object if building a graphql draft
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
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.
@@ -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 |
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.
}), | ||
]), | ||
slug: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
__typename: 'LocalizedString', | ||
locale: expect.any(String), |
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.
builder.spec.ts
for any draft where the isGraphqlDraft
option is passed to the transformer needs to be updated manually, like so.
@@ -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 comment
The 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 core
@@ -103,7 +103,6 @@ describe(`with anniversaryShirt preset`, () => { | |||
{ | |||
"categories": [ | |||
{ | |||
"__typename": "Reference", |
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.
all these snapshots were updated with jest -u
@@ -41,6 +41,7 @@ const transformers = { | |||
'taxCategory', | |||
'state', | |||
], | |||
isGraphqlDraft: true, |
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.
here is where the magic happens on the model/draft side
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
iterate over all keys in the current object
for (let [currentObjectKey, currentObjectValue] of Object.entries( | ||
inputObject | ||
)) { | ||
if (currentObjectKey === keyToDelete) { |
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.
if the key is the one we want to delete, delete it
)) { | ||
if (currentObjectKey === keyToDelete) { | ||
delete inputObject[keyToDelete]; | ||
} else if (Array.isArray(currentObjectValue)) { |
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.
otherwise, if the key's value is an array, send it to the fn that handles arrays
delete inputObject[keyToDelete]; | ||
} else if (Array.isArray(currentObjectValue)) { | ||
deleteKeyFromObjectInArray(currentObjectValue, keyToDelete); | ||
} else if (isObject(currentObjectValue)) { |
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.
otherwise, if the key's value is an object, run this function over the object
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 comment
The 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
let currentElement = inputArray[currentIndex]; | ||
if (Array.isArray(currentElement)) { | ||
deleteKeyFromObjectInArray(currentElement, keyToDelete); | ||
} else if (isObject(currentElement)) { |
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.
if it's an object in the array, run the delete function over the object to remove the unwanted key
When transforming fields for graphql draft models, built fields will add a
__typename
, which is not a valid field to send in a draft and makes API's very sad and send us a400
.This is the beginnings of an attempt to add a transform option
isGraphqlDraft
that would removetypename
from any built fields if passed.