Skip to content

Commit 398fa88

Browse files
authored
Merge pull request #184 from episerver/feat/CMS-Use-graph-to-check-dam-availability
Use Graph to determine DAM availability based on `cmp_Asset` type
2 parents bdc6a1d + 14c8a9f commit 398fa88

File tree

1 file changed

+21
-13
lines changed
  • packages/optimizely-cms-sdk/src/graph

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
type GraphOptions = {
2121
/** Graph instance URL. `https://cg.optimizely.com/content/v2` */
2222
graphUrl?: string;
23-
damEnabled?: boolean;
2423
};
2524

2625
export type PreviewParams = {
@@ -53,6 +52,10 @@ query GetContentMetadata($where: _ContentWhereInput, $variation: VariationInput)
5352
}
5453
}
5554
}
55+
# Check if "cmp_Asset" type exists which indicates that DAM is enabled
56+
damAssetType: __type(name: "cmp_Asset") {
57+
__typename
58+
}
5659
}
5760
`;
5861

@@ -219,12 +222,10 @@ function decorateWithContext(obj: any, params: PreviewParams): any {
219222
export class GraphClient {
220223
key: string;
221224
graphUrl: string;
222-
damEnabled: boolean = false;
223225

224226
constructor(key: string, options: GraphOptions = {}) {
225227
this.key = key;
226228
this.graphUrl = options.graphUrl ?? 'https://cg.optimizely.com/content/v2';
227-
this.damEnabled = options.damEnabled ?? false;
228229
}
229230

230231
/** Perform a GraphQL query with variables */
@@ -289,20 +290,25 @@ export class GraphClient {
289290
* @param previewToken - Optional preview token for fetching preview content.
290291
* @returns A promise that resolves to the first content type metadata object
291292
*/
292-
private async getContentType(input: GraphVariables, previewToken?: string) {
293+
private async getContentMetaData(
294+
input: GraphVariables,
295+
previewToken?: string
296+
) {
293297
const data = await this.request(
294298
GET_CONTENT_METADATA_QUERY,
295299
input,
296300
previewToken
297301
);
298302

299-
const type = data._Content?.item?._metadata?.types?.[0];
303+
const contentTypeName = data._Content?.item?._metadata?.types?.[0];
304+
// Determine if DAM is enabled based on the presence of cmp_Asset type
305+
const damEnabled = data.damAssetType !== null;
300306

301-
if (!type) {
302-
return null;
307+
if (!contentTypeName) {
308+
return { contentTypeName: null, damEnabled };
303309
}
304310

305-
if (typeof type !== 'string') {
311+
if (typeof contentTypeName !== 'string') {
306312
throw new GraphResponseError(
307313
"Returned type is not 'string'. This might be a bug in the SDK. Try again later. If the error persists, contact Optimizely support",
308314
{
@@ -314,7 +320,7 @@ export class GraphClient {
314320
);
315321
}
316322

317-
return type;
323+
return { contentTypeName, damEnabled };
318324
}
319325

320326
/**
@@ -339,13 +345,15 @@ export class GraphClient {
339345
...pathFilter(path, options?.host),
340346
variation: options?.variation,
341347
};
342-
const contentTypeName = await this.getContentType(input);
348+
const { contentTypeName, damEnabled } = await this.getContentMetaData(
349+
input
350+
);
343351

344352
if (!contentTypeName) {
345353
return [];
346354
}
347355

348-
const query = createMultipleContentQuery(contentTypeName, this.damEnabled);
356+
const query = createMultipleContentQuery(contentTypeName, damEnabled);
349357
const response = (await this.request(query, input)) as ItemsResponse<T>;
350358

351359
return response?._Content?.items.map(removeTypePrefix);
@@ -420,7 +428,7 @@ export class GraphClient {
420428
/** Fetches a content given the preview parameters (preview_token, ctx, ver, loc, key) */
421429
async getPreviewContent(params: PreviewParams) {
422430
const input = previewFilter(params);
423-
const contentTypeName = await this.getContentType(
431+
const { contentTypeName, damEnabled } = await this.getContentMetaData(
424432
input,
425433
params.preview_token
426434
);
@@ -431,7 +439,7 @@ export class GraphClient {
431439
{ request: { variables: input, query: GET_CONTENT_METADATA_QUERY } }
432440
);
433441
}
434-
const query = createSingleContentQuery(contentTypeName, this.damEnabled);
442+
const query = createSingleContentQuery(contentTypeName, damEnabled);
435443
const response = await this.request(query, input, params.preview_token);
436444

437445
return decorateWithContext(

0 commit comments

Comments
 (0)