Skip to content

Commit

Permalink
Merge pull request #46 from contentstack/createResolver_api
Browse files Browse the repository at this point in the history
Create resolver api
  • Loading branch information
asmit-patil authored Aug 14, 2020
2 parents a8211a8 + a346c13 commit aa60e3e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
12 changes: 8 additions & 4 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _extends4 = require('babel-runtime/helpers/extends');

var _extends5 = _interopRequireDefault(_extends4);

var _set = require('babel-runtime/core-js/set');

var _set2 = _interopRequireDefault(_set);
Expand Down Expand Up @@ -267,7 +271,7 @@ exports.createResolvers = function (_ref6) {

var resolvers = {};
references.forEach(function (reference) {
resolvers[reference.parent] = (0, _defineProperty3.default)({}, reference.uid, {
resolvers[reference.parent] = (0, _extends5.default)({}, resolvers[reference.parent], (0, _defineProperty3.default)({}, reference.uid, {
resolve: function resolve(source, args, context, info) {
if (source[reference.uid + '___NODE']) {
var nodesData = [];
Expand All @@ -282,17 +286,17 @@ exports.createResolvers = function (_ref6) {
}
return [];
}
});
}));
});
groups.forEach(function (group) {
resolvers[group.parent] = (0, _defineProperty3.default)({}, group.field.uid, {
resolvers[group.parent] = (0, _extends5.default)({}, resolvers[group.parent], (0, _defineProperty3.default)({}, group.field.uid, {
resolve: function resolve(source) {
if (group.field.multiple && !Array.isArray(source[group.field.uid])) {
return [];
}
return source[group.field.uid] || null;
}
});
}));
});
createResolvers(resolvers);
};
2 changes: 1 addition & 1 deletion src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const getPagedData = async (
) => {
query.skip = skip;
query.limit = limit;
query.include_global_field_schema = true
query.include_global_field_schema = true;
const response = await fetchCsData(url, config, query);
if (!aggregatedResponse) {
aggregatedResponse = response[responseKey];
Expand Down
26 changes: 14 additions & 12 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ exports.createSchemaCustomization = async ({
const contentTypeUid = ((contentType.uid).replace(/-/g, '_'));
const name = `${typePrefix}_${contentTypeUid}`;
const result = buildCustomSchema(contentType.schema, [], [], [], name, typePrefix);
references = references.concat(result.references)
groups = groups.concat(result.groups)
references = references.concat(result.references);
groups = groups.concat(result.groups);
const typeDefs = [
`type linktype{
title: String
Expand Down Expand Up @@ -201,11 +201,12 @@ exports.sourceNodes = async ({


exports.createResolvers = ({
createResolvers
createResolvers,
}) => {
let resolvers = {}
const resolvers = {};
references.forEach((reference) => {
resolvers[reference.parent] = {
...resolvers[reference.parent],
[reference.uid]: {
resolve(source, args, context, info) {
if (source[`${reference.uid}___NODE`]) {
Expand All @@ -221,20 +222,21 @@ exports.createResolvers = ({
}
return [];
},
}
}
})
},
};
});
groups.forEach((group) => {
resolvers[group.parent] = {
...resolvers[group.parent],
[group.field.uid]: {
resolve: (source) => {
if (group.field.multiple && !Array.isArray(source[group.field.uid])) {
return [];
}
return source[group.field.uid] || null;
},
}
}
})
createResolvers(resolvers)
}
},
};
});
createResolvers(resolvers);
};
21 changes: 10 additions & 11 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ exports.processEntry = (contentType, entry, createNodeId, createContentDigest, t
exports.normalizeEntry = (contentType, entry, entriesNodeIds, assetsNodeIds, createNodeId, typePrefix) => {
const resolveEntry = {
...entry,
...builtEntry(contentType.schema, entry, entry.publish_details.locale, entriesNodeIds, assetsNodeIds, createNodeId, typePrefix)
...builtEntry(contentType.schema, entry, entry.publish_details.locale, entriesNodeIds, assetsNodeIds, createNodeId, typePrefix),
};
return resolveEntry;
};
Expand Down Expand Up @@ -187,8 +187,8 @@ const buildBlockCustomSchema = (blocks, types, references, groups, parent, prefi

const buildCustomSchema = exports.buildCustomSchema = (schema, types, references, groups, parent, prefix) => {
const fields = {};
groups = groups || []
references = references || []
groups = groups || [];
references = references || [];
types = types || [];
schema.forEach((field) => {
switch (field.data_type) {
Expand Down Expand Up @@ -271,7 +271,7 @@ const buildCustomSchema = exports.buildCustomSchema = (schema, types, references
if (field.multiple && source[`${field.uid}___NODE`]) {
const nodesData = [];
context.nodeModel.getAllNodes({
type: `${prefix}_assets`
type: `${prefix}_assets`,
}).find((node) => {
source[`${field.uid}___NODE`].forEach((id) => {
if (node.id === id) {
Expand All @@ -284,8 +284,8 @@ const buildCustomSchema = exports.buildCustomSchema = (schema, types, references

if (source[`${field.uid}___NODE`]) {
return context.nodeModel.getAllNodes({
type: `${prefix}_assets`
})
type: `${prefix}_assets`,
})
.find((node) => node.id === source[`${field.uid}___NODE`]);
}
return null;
Expand Down Expand Up @@ -320,8 +320,8 @@ const buildCustomSchema = exports.buildCustomSchema = (schema, types, references
groups.push({
parent,
field,
})
});

if (field.mandatory) {
if (field.multiple) {
fields[field.uid] = `[${newparent}]!`;
Expand Down Expand Up @@ -378,7 +378,7 @@ const buildCustomSchema = exports.buildCustomSchema = (schema, types, references
types.push(unionType);

references.push({
parent: parent,
parent,
uid: field.uid,
});

Expand All @@ -390,12 +390,11 @@ const buildCustomSchema = exports.buildCustomSchema = (schema, types, references
}
break;
}

});
return {
fields,
types,
references,
groups,
};
};
};

0 comments on commit aa60e3e

Please sign in to comment.