-
Notifications
You must be signed in to change notification settings - Fork 31
/
create-schema-customization.js
218 lines (217 loc) · 10.7 KB
/
create-schema-customization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var _require = require('gatsby/graphql'),
GraphQLInt = _require.GraphQLInt,
GraphQLJSON = _require.GraphQLJSON,
GraphQLString = _require.GraphQLString;
var _require2 = require('./normalize'),
buildCustomSchema = _require2.buildCustomSchema,
extendSchemaWithDefaultEntryFields = _require2.extendSchemaWithDefaultEntryFields;
var _require3 = require('./fetch'),
fetchContentTypes = _require3.fetchContentTypes;
var _require4 = require('./utils'),
getContentTypeOption = _require4.getContentTypeOption;
var _require5 = require('./gatsby-plugin-image'),
resolveGatsbyImageData = _require5.resolveGatsbyImageData;
exports.createSchemaCustomization = /*#__PURE__*/function () {
var _ref = (0, _asyncToGenerator2["default"])(function (_ref2, configOptions) {
var cache = _ref2.cache,
actions = _ref2.actions,
schema = _ref2.schema,
reporter = _ref2.reporter,
createNodeId = _ref2.createNodeId;
return /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
var contentTypes, typePrefix, disableMandatoryFields, jsonRteToHtml, contentTypeOption, references, groups, fileFields, jsonRteFields, createTypes, contentTypeSchema, assetTypeSchema, _yield$import, getGatsbyImageFieldConfig, fieldConfig;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
typePrefix = configOptions.type_prefix || 'Contentstack';
disableMandatoryFields = configOptions.disableMandatoryFields || false;
jsonRteToHtml = configOptions.jsonRteToHtml || false;
_context2.prev = 3;
contentTypeOption = getContentTypeOption(configOptions);
_context2.next = 7;
return fetchContentTypes(configOptions, contentTypeOption);
case 7:
contentTypes = _context2.sent;
_context2.next = 10;
return cache.set(typePrefix, contentTypes);
case 10:
_context2.next = 15;
break;
case 12:
_context2.prev = 12;
_context2.t0 = _context2["catch"](3);
console.error('Contentstack fetch content type failed!');
case 15:
references = [], groups = [], fileFields = [], jsonRteFields = [];
if (!configOptions.enableSchemaGeneration) {
_context2.next = 52;
break;
}
createTypes = actions.createTypes;
/** Type definition for content-type schema */
contentTypeSchema = {
name: "".concat(typePrefix, "ContentTypes"),
fields: {
title: 'String!',
uid: 'String!',
created_at: {
type: 'Date',
extensions: {
dateformat: {}
}
},
updated_at: {
type: 'Date',
extensions: {
dateformat: {}
}
},
schema: 'JSON!',
description: 'String'
},
interfaces: ['Node'],
extensions: {
infer: false
}
};
/** Type definition for asset schema */
assetTypeSchema = {
name: "".concat(typePrefix, "_assets"),
fields: _objectSpread({
url: 'String'
}, configOptions.downloadImages ? {
localAsset: {
type: 'File',
extensions: {
link: {
from: "fields.localAsset"
}
}
}
} : {}),
interfaces: ['Node'],
extensions: {
infer: true
}
}; // Checks if gatsby-plugin-image is installed.
_context2.prev = 20;
_context2.next = 23;
return Promise.resolve().then(function () {
return _interopRequireWildcard(require('gatsby-plugin-image/graphql-utils'));
});
case 23:
_yield$import = _context2.sent;
getGatsbyImageFieldConfig = _yield$import.getGatsbyImageFieldConfig;
fieldConfig = {};
fieldConfig = getGatsbyImageFieldConfig(/*#__PURE__*/function () {
var _ref3 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(image, options) {
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", resolveGatsbyImageData({
image: image,
options: options,
cache: cache,
reporter: reporter
}));
case 1:
case "end":
return _context.stop();
}
}, _callee);
}));
return function (_x3, _x4) {
return _ref3.apply(this, arguments);
};
}(), {
fit: {
type: GraphQLString
},
crop: {
type: GraphQLString
},
trim: {
type: GraphQLString
},
pad: {
type: GraphQLString
},
quality: {
type: GraphQLInt,
defaultValue: 50
}
});
fieldConfig.type = GraphQLJSON;
assetTypeSchema.fields.gatsbyImageData = fieldConfig;
_context2.next = 34;
break;
case 31:
_context2.prev = 31;
_context2.t1 = _context2["catch"](20);
if (_context2.t1.code === 'MODULE_NOT_FOUND') {
reporter.info("Gatsby plugin image is required to use new gatsby image plugin's feature. Please check https://github.com/contentstack/gatsby-source-contentstack#the-new-gatsby-image-plugin for more help.");
}
case 34:
createTypes([schema.buildObjectType(contentTypeSchema), schema.buildObjectType(assetTypeSchema)]);
contentTypes && contentTypes.forEach(function (contentType) {
var contentTypeUid = contentType.uid.replace(/-/g, '_');
var name = "".concat(typePrefix, "_").concat(contentTypeUid);
var extendedSchema = extendSchemaWithDefaultEntryFields(contentType.schema);
var result = buildCustomSchema(extendedSchema, [], [], [], [], [], name, typePrefix, disableMandatoryFields, jsonRteToHtml, createNodeId, undefined);
references = references.concat(result.references);
groups = groups.concat(result.groups);
fileFields = fileFields.concat(result.fileFields);
jsonRteFields = jsonRteFields.concat(result.jsonRteFields);
var typeDefs = ["type linktype { title: String href: String }", schema.buildObjectType({
name: name,
fields: result.fields,
interfaces: ['Node'],
extensions: {
infer: true
}
})];
result.types = result.types.concat(typeDefs);
createTypes(result.types);
});
_context2.t2 = Promise;
_context2.next = 39;
return cache.set("".concat(typePrefix, "_").concat(configOptions.api_key, "_references"), references);
case 39:
_context2.t3 = _context2.sent;
_context2.next = 42;
return cache.set("".concat(typePrefix, "_").concat(configOptions.api_key, "_groups"), groups);
case 42:
_context2.t4 = _context2.sent;
_context2.next = 45;
return cache.set("".concat(typePrefix, "_").concat(configOptions.api_key, "_file_fields"), fileFields);
case 45:
_context2.t5 = _context2.sent;
_context2.next = 48;
return cache.set("".concat(typePrefix, "_").concat(configOptions.api_key, "_json_rte_fields"), jsonRteFields);
case 48:
_context2.t6 = _context2.sent;
_context2.t7 = [_context2.t3, _context2.t4, _context2.t5, _context2.t6];
_context2.next = 52;
return _context2.t2.all.call(_context2.t2, _context2.t7);
case 52:
case "end":
return _context2.stop();
}
}, _callee2, null, [[3, 12], [20, 31]]);
})();
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();
//# sourceMappingURL=create-schema-customization.js.map