Skip to content

Commit

Permalink
Merge pull request #33 from contentstack/customSchema
Browse files Browse the repository at this point in the history
bugfix: resolved "contentstack_asset" type node for the file field
  • Loading branch information
asmit-patil authored May 12, 2020
2 parents aaa28d4 + 714abca commit 8aa2776
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
21 changes: 19 additions & 2 deletions normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,25 @@ var buildCustomSchema = exports.buildCustomSchema = function (schema, types, par
var type = 'type ' + prefix + '_assets implements Node { url: String }';
types.push(type);
fields[field.uid] = {
resolve: function resolve(source) {
return source[field.uid] || null;
resolve: function resolve(source, args, context) {
if (field.multiple && source[field.uid + '___NODE']) {
var nodesData = [];
context.nodeModel.getAllNodes({ type: prefix + '_assets' }).find(function (node) {
source[field.uid + '___NODE'].forEach(function (id) {
if (node.id === id) {
nodesData.push(node);
}
});
});
return nodesData;
}

if (source[field.uid + '___NODE']) {
return context.nodeModel.getAllNodes({ type: prefix + '_assets' }).find(function (node) {
return node.id === source[field.uid + '___NODE'];
});
}
return null;
}
};
if (field.mandatory) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-contentstack",
"version": "2.2.2",
"version": "2.2.3",
"description": "Gatsby source plugin for building websites using Contentstack as a data source",
"scripts": {
"prepublish": "npm run build",
Expand Down
20 changes: 19 additions & 1 deletion src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,25 @@ const buildCustomSchema = exports.buildCustomSchema = (schema, types, parent, pr
const type = `type ${prefix}_assets implements Node { url: String }`;
types.push(type);
fields[field.uid] = {
resolve: (source) => source[field.uid] || null,
resolve: (source, args, context) => {
if (field.multiple && source[`${field.uid}___NODE`]) {
const nodesData = [];
context.nodeModel.getAllNodes({ type: `${prefix}_assets` }).find((node) => {
source[`${field.uid}___NODE`].forEach((id) => {
if (node.id === id) {
nodesData.push(node);
}
});
});
return nodesData;
}

if (source[`${field.uid}___NODE`]) {
return context.nodeModel.getAllNodes({ type: `${prefix}_assets` })
.find((node) => node.id === source[`${field.uid}___NODE`]);
}
return null;
},
};
if (field.mandatory) {
if (field.multiple) {
Expand Down

0 comments on commit 8aa2776

Please sign in to comment.