Skip to content

Commit a4fe77d

Browse files
committed
Merge branch 'main' into feature/CMS-45548-npm
2 parents adafe5d + b0f3bdd commit a4fe77d

37 files changed

+1372
-245
lines changed

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

Lines changed: 240 additions & 224 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ describe('createFragment()', () => {
1717
[
1818
"fragment _IExperience on _IExperience { composition {...ICompositionNode }}",
1919
"fragment ICompositionNode on ICompositionNode { __typename key type nodeType displayName displayTemplateKey displaySettings {key value} ...on CompositionStructureNode { nodes @recursive } ...on CompositionComponentNode { nodeType component { ..._IComponent } } }",
20-
"fragment CallToAction on CallToAction { label link }",
21-
"fragment ExpSection on ExpSection { heading }",
20+
"fragment CallToAction on CallToAction { __typename label link }",
21+
"fragment ExpSection on ExpSection { __typename heading }",
2222
"fragment _IComponent on _IComponent { __typename ...CallToAction ...ExpSection }",
23-
"fragment MyExperience on MyExperience { ..._IExperience }",
23+
"fragment MyExperience on MyExperience { __typename ..._IExperience }",
2424
]
2525
`);
2626
});

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function convertPropertyField(
134134
}
135135

136136
const uniqueSubfields = [...new Set(subfields)].join(' '); // remove duplicates
137-
fields.push(`${name} { __typename ${uniqueSubfields} }`);
137+
fields.push(`${name} { ${uniqueSubfields} }`);
138138
} else if (property.type === 'richText') {
139139
fields.push(`${name} { html, json }`);
140140
} else if (property.type === 'url') {
@@ -209,7 +209,7 @@ export function createFragment(
209209
if (visited.size === 0) refreshCache();
210210
visited.add(fragmentName);
211211

212-
const fields: string[] = [];
212+
const fields: string[] = ['__typename'];
213213
const extraFragments: string[] = [];
214214

215215
// Built‑in CMS baseTypes ("_image", "_video", "_media" etc.)
@@ -255,12 +255,6 @@ export function createFragment(
255255
}
256256
}
257257

258-
// If there are no fields (e.g., empty properties or all properties have indexingType "disabled"),
259-
// We return an empty array to avoid creating empty fragments.
260-
if (!fields.length) {
261-
return [];
262-
}
263-
264258
// Convert base type key to GraphQL fragment format
265259
// eg: "_image" -> "_Image"
266260
const parsedFragmentName = toBaseTypeFragmentKey(fragmentName);
@@ -281,14 +275,15 @@ export function createFragment(
281275
*/
282276
export function createSingleContentQuery(contentType: string) {
283277
const fragment = createFragment(contentType);
278+
const fragmentName = fragment.length > 0 ? '...' + contentType : '';
284279

285280
return `
286281
${fragment.join('\n')}
287282
query GetContent($where: _ContentWhereInput, $variation: VariationInput) {
288283
_Content(where: $where, variation: $variation) {
289284
item {
290285
__typename
291-
...${contentType}
286+
${fragmentName}
292287
_metadata {
293288
variation
294289
}
@@ -307,14 +302,15 @@ query GetContent($where: _ContentWhereInput, $variation: VariationInput) {
307302
*/
308303
export function createMultipleContentQuery(contentType: string) {
309304
const fragment = createFragment(contentType);
305+
const fragmentName = fragment.length > 0 ? '...' + contentType : '';
310306

311307
return `
312308
${fragment.join('\n')}
313309
query ListContent($where: _ContentWhereInput, $variation: VariationInput) {
314310
_Content(where: $where, variation: $variation) {
315311
items {
316-
__typename
317-
...${contentType}
312+
313+
${fragmentName}
318314
_metadata {
319315
variation
320316
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ export class GraphClient {
152152
const type = data._Content?.item?._metadata?.types?.[0];
153153

154154
if (!type) {
155-
throw new GraphResponseError('No content found', {
156-
request: {
157-
query: GET_CONTENT_METADATA_QUERY,
158-
variables: input,
159-
},
160-
});
155+
return null;
161156
}
162157

163158
if (typeof type !== 'string') {
@@ -187,7 +182,7 @@ export class GraphClient {
187182
* @param contentType - A string representing the content type. If omitted, the method
188183
* will try to get the content type name from the CMS.
189184
*
190-
* @returns An iterator that traverses all found items
185+
* @returns An array of all items matching the path and options. Returns an empty array if no content is found.
191186
*/
192187
async getContentByPath<T = any>(
193188
path: string,
@@ -198,6 +193,11 @@ export class GraphClient {
198193
variation: options?.variation,
199194
};
200195
const contentTypeName = await this.getContentType(input);
196+
197+
if (!contentTypeName) {
198+
return [];
199+
}
200+
201201
const query = createMultipleContentQuery(contentTypeName);
202202
const response = (await this.request(query, input)) as ItemsResponse<T>;
203203

0 commit comments

Comments
 (0)