Skip to content

Commit 45cdc58

Browse files
authored
Merge pull request #172 from episerver/bugfix/CMS-46783-handle-blocks
Handle blocks
2 parents fc09028 + 1ce9def commit 45cdc58

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed

packages/optimizely-cms-sdk/src/graph/__test__/createQuery.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,36 @@ describe('createFragment() empty objects', () => {
553553
`);
554554
});
555555
});
556+
557+
describe('createFragment() with component properties', () => {
558+
test('simple case', async () => {
559+
const ctBlock = contentType({
560+
key: 'ctBlock',
561+
baseType: '_component',
562+
properties: {
563+
p1: { type: 'string' },
564+
},
565+
});
566+
const ct1 = contentType({
567+
key: 'ct1',
568+
baseType: '_page',
569+
properties: {
570+
p1: { type: 'component', contentType: ctBlock },
571+
},
572+
});
573+
initContentTypeRegistry([ct1, ctBlock]);
574+
const result = await createFragment('ct1');
575+
expect(result).toMatchInlineSnapshot(`
576+
[
577+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
578+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
579+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
580+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
581+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
582+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
583+
"fragment ctBlockProperty on ctBlockProperty { __typename ctBlockProperty__p1:p1 }",
584+
"fragment ct1 on ct1 { __typename ct1__p1:p1 { ...ctBlockProperty } ..._IContent }",
585+
]
586+
`);
587+
});
588+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { removeTypePrefix } from '../index.js';
3+
4+
describe('removeTypePrefix()', () => {
5+
test('basic functionality', () => {
6+
const input = {
7+
__typename: 'T',
8+
T__p1: 'p1',
9+
T__p2: 42,
10+
T__p3: ['p3', 32],
11+
T__p4: { nested: 'p4' },
12+
T__p5: { nested: ['p5'] },
13+
ShouldBeKept__p6: 'p6',
14+
};
15+
const expected = {
16+
__typename: 'T',
17+
p1: 'p1',
18+
p2: 42,
19+
p3: ['p3', 32],
20+
p4: { nested: 'p4' },
21+
p5: { nested: ['p5'] },
22+
ShouldBeKept__p6: 'p6',
23+
};
24+
expect(removeTypePrefix(input)).toStrictEqual(expected);
25+
});
26+
27+
test('should remove prefixes only in the same level', () => {
28+
const input = {
29+
__typename: 'T',
30+
T__p1: { T_shouldBeKept: 'shouldBeKept' },
31+
};
32+
const expected = {
33+
__typename: 'T',
34+
p1: { T_shouldBeKept: 'shouldBeKept' },
35+
};
36+
expect(removeTypePrefix(input)).toStrictEqual(expected);
37+
});
38+
39+
test('should work for nested objects', () => {
40+
const input = {
41+
__typename: 'T',
42+
T__p1: {
43+
__typename: 'U',
44+
U__p1: 'p1',
45+
U__p2: {
46+
__typename: 'V',
47+
V__p1: 'p1',
48+
},
49+
},
50+
T__p2: [{ __typename: 'U', U__p1: 'p1' }],
51+
};
52+
const expected = {
53+
__typename: 'T',
54+
p1: {
55+
__typename: 'U',
56+
p1: 'p1',
57+
p2: {
58+
__typename: 'V',
59+
p1: 'p1',
60+
},
61+
},
62+
p2: [{ __typename: 'U', p1: 'p1' }],
63+
};
64+
expect(removeTypePrefix(input)).toStrictEqual(expected);
65+
});
66+
67+
test('should not do anything if __typename is not found', () => {
68+
const input = {
69+
T__p1: 'hello',
70+
T__p2: 42,
71+
T__p3: ['hello', 32],
72+
T__p4: { nested: 'nested' },
73+
T__p5: { nested: ['hello'] },
74+
};
75+
76+
expect(removeTypePrefix(input)).toStrictEqual(input);
77+
});
78+
});

packages/optimizely-cms-sdk/src/graph/createQuery.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,16 @@ function convertProperty(
6969
name: string,
7070
property: AnyProperty,
7171
rootName: string,
72+
suffix: string,
7273
visited: Set<string>
7374
): { fields: string[]; extraFragments: string[] } {
74-
const result = convertPropertyField(name, property, rootName, visited);
75+
const result = convertPropertyField(
76+
name,
77+
property,
78+
rootName,
79+
suffix,
80+
visited
81+
);
7582

7683
// logs warnings if the fragment generation causes potential issues
7784
const warningMessage = checkTypeConstraintIssues(rootName, property, result);
@@ -95,12 +102,13 @@ function convertPropertyField(
95102
name: string,
96103
property: AnyProperty,
97104
rootName: string,
105+
suffix: string,
98106
visited: Set<string>
99107
): { fields: string[]; extraFragments: string[] } {
100108
const fields: string[] = [];
101109
const subfields: string[] = [];
102110
const extraFragments: string[] = [];
103-
const nameInFragment = `${rootName}__${name}:${name}`;
111+
const nameInFragment = `${rootName}${suffix}__${name}:${name}`;
104112

105113
if (property.type === 'component') {
106114
const key = property.contentType.key;
@@ -146,7 +154,7 @@ function convertPropertyField(
146154
extraFragments.push(CONTENT_URL_FRAGMENT);
147155
fields.push(`${nameInFragment} { key url { ...ContentUrl }}`);
148156
} else if (property.type === 'array') {
149-
const f = convertProperty(name, property.items, rootName, visited);
157+
const f = convertProperty(name, property.items, rootName, suffix, visited);
150158
fields.push(...f.fields);
151159
extraFragments.push(...f.extraFragments);
152160
} else {
@@ -237,6 +245,7 @@ export function createFragment(
237245
propKey,
238246
prop,
239247
contentTypeName,
248+
suffix,
240249
visited
241250
);
242251
fields.push(...f);

packages/optimizely-cms-sdk/src/graph/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ type GetLinksResponse = {
144144
*
145145
* @param obj - The object to process (typically a GraphQL response)
146146
* @returns A new object with prefixes removed, or the original value for primitives
147+
*
148+
* Note: this function is exported only on this level for testing purposes.
149+
* It should not be exported in the user-facing API
147150
*/
148-
function removeTypePrefix(obj: any): any {
151+
export function removeTypePrefix(obj: any): any {
149152
if (Array.isArray(obj)) {
150153
return obj.map((e) => removeTypePrefix(e));
151154
}

0 commit comments

Comments
 (0)