Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

ByronDWall
Copy link
Contributor

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 a 400.

This is the beginnings of an attempt to add a transform option isGraphqlDraft that would remove typename from any built fields if passed.

Copy link

changeset-bot bot commented Dec 1, 2023

🦋 Changeset detected

Latest commit: 6e649f7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 30 packages
Name Type
@commercetools-test-data/core Minor
@commercetools-test-data/associate-role Minor
@commercetools-test-data/business-unit Minor
@commercetools-test-data/cart-discount Minor
@commercetools-test-data/cart Minor
@commercetools-test-data/category Minor
@commercetools-test-data/channel Minor
@commercetools-test-data/commons Minor
@commercetools-test-data/custom-view Minor
@commercetools-test-data/customer-group Minor
@commercetools-test-data/customer Minor
@commercetools-test-data/discount-code Minor
@commercetools-test-data/inventory-entry Minor
@commercetools-test-data/order Minor
@commercetools-test-data/payment Minor
@commercetools-test-data/product-discount Minor
@commercetools-test-data/product-selection Minor
@commercetools-test-data/product-type Minor
@commercetools-test-data/quote-request Minor
@commercetools-test-data/quote Minor
@commercetools-test-data/review Minor
@commercetools-test-data/shipping-method Minor
@commercetools-test-data/staged-quote Minor
@commercetools-test-data/store Minor
@commercetools-test-data/tax-category Minor
@commercetools-test-data/zone Minor
@commercetools-test-data/product Minor
@commercetools-test-data/shopping-list Minor
@commercetools-test-data/graphql-types Minor
@commercetools-test-data/utils Minor

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

core/src/transformer.ts Outdated Show resolved Hide resolved
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Copy link
Contributor Author

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
Copy link
Contributor Author

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),
Copy link
Contributor Author

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",
Copy link
Contributor Author

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",
Copy link
Contributor Author

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,
Copy link
Contributor Author

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(
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

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)) {
Copy link
Contributor Author

@ByronDWall ByronDWall Dec 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant