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

FCT 598: Omit __typename field when building graphql draft models #474

Closed
wants to merge 2 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/unlucky-phones-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/core': minor
---

"fix(graphql drafts): omit \_\_typename field from built graphql draft models when isGraphqlDraft option is passed to transformer"
43 changes: 43 additions & 0 deletions core/src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,49 @@ describe('building', () => {
],
});
});
it('should omit __typename field when building nested builders and isGraphqlDraft is true', () => {
const teamTransformers = {
graphql: Transformer<TestTeam, TestTeam>('graphql', {
buildFields: ['users'],
isGraphqlDraft: true,
}),
};
const userTransformers = {
graphql: Transformer<
TestExpandedUserReference,
TestExpandedUserReferenceGraphql
>('graphql', {
addFields: () => ({
__typename: 'User',
}),
isGraphqlDraft: true,
}),
};
const userBuilder1 = Builder<TestExpandedUserReference>({
transformers: userTransformers,
}).name('My name');
const userBuilder2 = Builder<TestExpandedUserReference>({
transformers: userTransformers,
}).name('My other name');
const built = Builder<TestTeam>({
transformers: teamTransformers,
})
.id('my-id')
.users<TestExpandedUserReference>([userBuilder1, userBuilder2])
.buildGraphql<TestTeam>();

expect(built).toEqual({
id: 'my-id',
users: [
{
name: 'My name',
},
{
name: 'My other name',
},
],
});
});
});
});
});
Expand Down
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
function deleteKeyFromObject(inputObject: any, keyToDelete: string) {
for (let [currentObjectKey, currentObjectValue] of Object.entries(
inputObject
)) {
if (currentObjectKey === keyToDelete) {
delete inputObject[keyToDelete];
} else if (Array.isArray(currentObjectValue)) {
deleteKeyFromObjectInArray(currentObjectValue, keyToDelete);
} else if (isObject(currentObjectValue)) {
deleteKeyFromObject(currentObjectValue, keyToDelete);
}
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function deleteKeyFromObjectInArray(inputArray: any[], keyToDelete: string) {
for (let currentIndex = 0; currentIndex < inputArray.length; currentIndex++) {
let currentElement = inputArray[currentIndex];
if (Array.isArray(currentElement)) {
deleteKeyFromObjectInArray(currentElement, keyToDelete);
} else if (isObject(currentElement)) {
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
Loading