Skip to content

Commit 12f9980

Browse files
author
Jan Lenoch
authored
Merge pull request #22 from Kentico/graphql-vs-assets
Work around GraphQL property name limitations
2 parents bd08604 + 2ef1b1d commit 12f9980

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ All nodes have a `usedByContentItems` property that reflects the other nodes in
156156

157157
Currently, the plugin exhibits an [issue with nodes of multiple types](https://github.com/gatsbyjs/gatsby/issues/9154) in foreign key relationships (`___NODE`). In practice, only content items of one content type can be put into a particular *Linked items* element or *Rich text* element. We're in touch with Gatsby Inc. and work towards resolving that issue soon.
158158

159+
Gatsby's GraphQL libraries won't accept object properties with names starting with numbers. This conflicts with the way image assets are named in the Delivery API response (in the JS SDK output, respectively). Therefore, the names of assets are prefixed by the source plugin with `image-`.
160+
161+
"images": {
162+
"image-79bd9e11-f643-4cd6-9ea5-d1be17cf7de2": {
163+
"image_id": "79bd9e11-f643-4cd6-9ea5-d1be17cf7de2",
164+
"description": null,
165+
"url": "https://assets-eu-01.kc-usercontent.com:443/5ac93d1e-567d-01e6-e3b7-ac435f77b907/f7a357f7-643a-4d2d-8078-0fc371914d25/clearing-cache-with-webhooks---blog-post-image@2x_t.png"
166+
},
167+
"image-d2b4bdb2-a586-45d8-9920-bc1dd6846498": {
168+
"image_id": "d2b4bdb2-a586-45d8-9920-bc1dd6846498",
169+
"description": null,
170+
"url": "https://assets-eu-01.kc-usercontent.com:443/5ac93d1e-567d-01e6-e3b7-ac435f77b907/36cb3d1b-1aa7-4809-9606-82c3c0f00b7f/fcyTulxr.jpg"
171+
}
172+
173+
Should you need to refer to the properties, just bear in mind the addition of the `image-` suffix. The GUID in the child `image_id` property is never prefixed or otherwise transformed.
174+
159175
## Further information
160176

161177
For more developer resources, visit the Kentico Cloud Developer Hub at https://developer.kenticocloud.com.

src/normalize.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,18 @@ const createContentItemNode =
6666
.keys(contentItem)
6767
.filter((key) => key !== `system` && key !== `elements`)
6868
.forEach((key) => {
69-
elements[key] = contentItem[key];
69+
let propertyValue;
70+
71+
if (_.has(contentItem[key], `type`)
72+
&& contentItem[key].type === `rich_text`
73+
&& _.has(contentItem.elements[key], `images`)
74+
&& !_.isEmpty(contentItem.elements[key].images)) {
75+
propertyValue = prefixImagesInRichText(contentItem.elements[key]);
76+
} else {
77+
propertyValue = contentItem[key];
78+
}
79+
80+
elements[key] = propertyValue;
7081
});
7182

7283
const itemWithElements = {
@@ -228,8 +239,9 @@ of valid objects.`);
228239
const linkPropertyName = `${propertyName}_nodes___NODE`;
229240

230241
const linkedNodes = allNodesOfSameLanguage
231-
.filter((node) =>
232-
property.linkedItemCodenames.includes(node.system.codename)
242+
.filter((node) => _.has(property, `linkedItemCodenames`)
243+
&& _.isArray(property.linkedItemCodenames)
244+
&& property.linkedItemCodenames.includes(node.system.codename)
233245
);
234246

235247
itemNode.elements[linkPropertyName] = [];
@@ -305,6 +317,34 @@ const addLinkedItemsLinks = (itemNode, linkedNodes, linkPropertyName) => {
305317
}
306318
};
307319

320+
321+
const prefixImagesInRichText = (richTextPropertyValue) => {
322+
const imagesIdentifier = `images`;
323+
const prefixLiteral = `image-`;
324+
let transformedPropertyValue = {};
325+
326+
Object
327+
.keys(richTextPropertyValue)
328+
.filter((key) => key !== imagesIdentifier)
329+
.forEach((key) => {
330+
transformedPropertyValue[key] = richTextPropertyValue[key];
331+
});
332+
333+
let transformedImagesProperty = {};
334+
335+
Object
336+
.keys(richTextPropertyValue[imagesIdentifier])
337+
.forEach((key) => {
338+
const prefixedKey = prefixLiteral + key;
339+
transformedImagesProperty[prefixedKey] =
340+
richTextPropertyValue[imagesIdentifier][key];
341+
});
342+
343+
transformedPropertyValue[imagesIdentifier] = transformedImagesProperty;
344+
345+
return transformedPropertyValue;
346+
};
347+
308348
exports.createContentTypeNode = createContentTypeNode;
309349
exports.createContentItemNode = createContentItemNode;
310350
exports.decorateTypeNodesWithItemLinks = decorateTypeNodesWithItemLinks;

0 commit comments

Comments
 (0)