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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-donuts-press.md
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
28 changes: 28 additions & 0 deletions core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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

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

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

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

deleteKeyFromObject(currentObjectValue, keyToDelete);
}
}
}

// 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.

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

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

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;
Expand Down Expand Up @@ -175,6 +202,7 @@ export {
isBuilderFunction,
upperFirst,
lowerFirst,
deleteKeyFromObject,
omitMany,
pickMany,
convertBuiltNameToTransformName,
Expand Down
7 changes: 6 additions & 1 deletion core/src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildField, buildFields } from './helpers';
import { buildField, buildFields, deleteKeyFromObject } from './helpers';
import type {
TTransformer,
TTransformerOptions,
Expand Down Expand Up @@ -36,6 +36,11 @@ function Transformer<Model, TransformedModel>(
};
}
});

if (transformOptions?.isGraphqlDraft) {
/**recursively delete '__typename' field from all built fields in graphql draft */
deleteKeyFromObject(transformedFields, '__typename');
}
}

// The default transformer only allows building nested fields to not
Expand Down
5 changes: 5 additions & 0 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export type TTransformerOptions<Model, TransformedModel> = {
removeFields?: (keyof Model)[];
replaceFields?: (args: { fields: Model }) => TransformedModel;
buildFields?: (keyof Model)[];
/** When transforming fields for GraphQL draft models,
* this flag removes all "__typename" fields from transformed models
* so that the draft can be used with the API
*/
isGraphqlDraft?: Boolean;
};

export interface TTransformer<Model> {
Expand Down
2 changes: 1 addition & 1 deletion models/product/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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

"@commercetools-test-data/product-type": "6.6.0",
"@commercetools-test-data/product-variant": "5.11.2",
"@commercetools-test-data/tax-category": "6.6.0",
Expand Down
9 changes: 6 additions & 3 deletions models/product/src/product/product-draft/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
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.

value: expect.any(String),
}),
]),
description: expect.arrayContaining([
expect.objectContaining({
__typename: 'LocalizedString',
locale: expect.any(String),
value: expect.any(String),
}),
]),
categories: expect.arrayContaining([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

"key": "tops-kids",
"typeId": "category",
},
Expand All @@ -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",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ describe(`with denimJacket preset`, () => {
{
"categories": [
{
"__typename": "Reference",
"key": "tops-men",
"typeId": "category",
},
Expand Down Expand Up @@ -328,29 +327,25 @@ describe(`with denimJacket preset`, () => {
"metaTitle": undefined,
"name": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "Sample Denim Jacket",
},
],
"priceMode": undefined,
"productType": {
"__typename": "Reference",
"key": "jackets",
"typeId": "product-type",
},
"publish": true,
"searchKeywords": undefined,
"slug": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "sample-denim-jacket",
},
],
"state": undefined,
"taxCategory": {
"__typename": "Reference",
"key": "standard-tax",
"typeId": "tax-category",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ describe(`with flairJeans preset`, () => {
{
"categories": [
{
"__typename": "Reference",
"key": "bottoms-women",
"typeId": "category",
},
Expand Down Expand Up @@ -166,29 +165,25 @@ describe(`with flairJeans preset`, () => {
"metaTitle": undefined,
"name": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "Sample Flair Jeans",
},
],
"priceMode": undefined,
"productType": {
"__typename": "Reference",
"key": "pants",
"typeId": "product-type",
},
"publish": true,
"searchKeywords": undefined,
"slug": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "sample-flair-jeans",
},
],
"state": undefined,
"taxCategory": {
"__typename": "Reference",
"key": "standard-tax",
"typeId": "tax-category",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ describe(`with halloweenTop preset`, () => {
{
"categories": [
{
"__typename": "Reference",
"key": "tops-men",
"typeId": "category",
},
Expand Down Expand Up @@ -302,29 +301,25 @@ describe(`with halloweenTop preset`, () => {
"metaTitle": undefined,
"name": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "Sample Halloween Top",
},
],
"priceMode": undefined,
"productType": {
"__typename": "Reference",
"key": "shirts",
"typeId": "product-type",
},
"publish": false,
"searchKeywords": undefined,
"slug": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "sample-halloween-top",
},
],
"state": undefined,
"taxCategory": {
"__typename": "Reference",
"key": "standard-tax",
"typeId": "tax-category",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ describe(`with maternityTop preset`, () => {
{
"categories": [
{
"__typename": "Reference",
"key": "tops-women",
"typeId": "category",
},
Expand Down Expand Up @@ -318,29 +317,25 @@ describe(`with maternityTop preset`, () => {
"metaTitle": undefined,
"name": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "Sample Maternity Top",
},
],
"priceMode": undefined,
"productType": {
"__typename": "Reference",
"key": "shirts",
"typeId": "product-type",
},
"publish": true,
"searchKeywords": undefined,
"slug": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "sample-maternity-top",
},
],
"state": undefined,
"taxCategory": {
"__typename": "Reference",
"key": "standard-tax",
"typeId": "tax-category",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ describe(`with necklace preset`, () => {
{
"categories": [
{
"__typename": "Reference",
"key": "other-women",
"typeId": "category",
},
Expand Down Expand Up @@ -261,29 +260,25 @@ describe(`with necklace preset`, () => {
"metaTitle": undefined,
"name": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "Sample Necklace",
},
],
"priceMode": undefined,
"productType": {
"__typename": "Reference",
"key": "accessories",
"typeId": "product-type",
},
"publish": true,
"searchKeywords": undefined,
"slug": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "sample-necklace",
},
],
"state": undefined,
"taxCategory": {
"__typename": "Reference",
"key": "standard-tax",
"typeId": "tax-category",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ describe(`with promDress preset`, () => {
{
"categories": [
{
"__typename": "Reference",
"key": "other-women",
"typeId": "category",
},
Expand Down Expand Up @@ -236,29 +235,25 @@ describe(`with promDress preset`, () => {
"metaTitle": undefined,
"name": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "Sample Prom Dress",
},
],
"priceMode": undefined,
"productType": {
"__typename": "Reference",
"key": "dresses",
"typeId": "product-type",
},
"publish": true,
"searchKeywords": undefined,
"slug": [
{
"__typename": "LocalizedString",
"locale": "en-US",
"value": "sample-prom-dress",
},
],
"state": undefined,
"taxCategory": {
"__typename": "Reference",
"key": "standard-tax",
"typeId": "tax-category",
},
Expand Down
Loading