mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 15:44:59 +00:00
Fix GraphQL and clean guidelines
This commit is contained in:
parent
a8aa89b5e3
commit
c358e72b3c
@ -9,6 +9,7 @@ const _ = require('lodash');
|
||||
|
||||
// Strapi helper for GraphQL.
|
||||
const helpers = require('strapi/lib/configuration/hooks/graphql/helpers/');
|
||||
const utils = require('./');
|
||||
|
||||
/**
|
||||
* Utils functions for BookShelf
|
||||
@ -132,6 +133,8 @@ module.exports = {
|
||||
update: function (collectionIdentity, rootValue, args) {
|
||||
_.merge(args, rootValue.context.request.body);
|
||||
|
||||
const PK = utils.getPK(collectionIdentity.toLowerCase(), null, strapi.models);
|
||||
|
||||
return strapi.services[collectionIdentity.toLowerCase()]
|
||||
.edit(_.set({}, PK, args[PK]), _.omit(args, PK))
|
||||
.then(function (data) {
|
||||
|
||||
@ -76,14 +76,14 @@ module.exports = function (scope, cb) {
|
||||
}
|
||||
});
|
||||
|
||||
const history = () => {
|
||||
const history = (function () {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'data', 'migrations', '.history'), 'utf8'));
|
||||
} catch (err) {
|
||||
// File not existing
|
||||
return {};
|
||||
}
|
||||
}();
|
||||
})();
|
||||
|
||||
// Register every model.
|
||||
const migrations = glob.sync(path.resolve(scope.rootPath, 'api', '**', 'models', '*.json')).map((filepath) => {
|
||||
@ -171,7 +171,7 @@ module.exports = function (scope, cb) {
|
||||
// If it's an existing attribute.
|
||||
|
||||
// Try to identify attribute updates
|
||||
const toDrop = () => {
|
||||
const toDrop = (function () {
|
||||
if (details.hasOwnProperty('collection') && details.hasOwnProperty('via') &&
|
||||
(_.get(scope.models[modelName].oldAttributes[attribute], 'collection') !== details.collection || _.get(scope.models[modelName].oldAttributes[attribute], 'via') !== details.via)) {
|
||||
return true;
|
||||
@ -198,7 +198,7 @@ module.exports = function (scope, cb) {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}();
|
||||
})();
|
||||
|
||||
// The attribute has been updated.
|
||||
// We will drop it then create it again with the new options.
|
||||
|
||||
@ -40,7 +40,6 @@ module.exports = function (models, modelName) {
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
// Template: drop the table for the `down` export.
|
||||
// This adds a `down` logic for the current model.
|
||||
// Then, every `down` logic of every model call the
|
||||
|
||||
@ -29,12 +29,11 @@ module.exports = function (rootModels, modelName, details, attribute, toDrop, on
|
||||
let infos = {};
|
||||
let oldInfos = {};
|
||||
|
||||
|
||||
if (!onlyDrop && toDrop) {
|
||||
infos = utilsModels.getNature(details, attribute, rootModels);
|
||||
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
|
||||
|
||||
const isDifferentVerbose = oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature ? false : true;
|
||||
const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);
|
||||
|
||||
if (isDifferentVerbose) {
|
||||
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
|
||||
@ -52,8 +51,7 @@ module.exports = function (rootModels, modelName, details, attribute, toDrop, on
|
||||
handleRelation(infos, rootModels, modelName, details, attribute);
|
||||
}
|
||||
|
||||
function handleRelation (infos, models, modelName, details, attribute, toDrop, onlyDrop) {
|
||||
|
||||
function handleRelation(infos, models, modelName, details, attribute, toDrop, onlyDrop) {
|
||||
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.create'))) {
|
||||
_.set(rootModels[modelName].attributes, attribute + '.create', {
|
||||
drop: '',
|
||||
@ -133,7 +131,7 @@ module.exports = function (rootModels, modelName, details, attribute, toDrop, on
|
||||
rootModels[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
||||
tableName: modelName,
|
||||
attribute: attribute,
|
||||
details: details,
|
||||
details: details
|
||||
}));
|
||||
|
||||
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
||||
@ -164,7 +162,7 @@ module.exports = function (rootModels, modelName, details, attribute, toDrop, on
|
||||
rootModels[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
||||
tableName: modelName,
|
||||
attribute: attribute,
|
||||
details: details,
|
||||
details: details
|
||||
}));
|
||||
|
||||
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
||||
@ -182,7 +180,7 @@ module.exports = function (rootModels, modelName, details, attribute, toDrop, on
|
||||
rootModels[modelName].attributes[attribute].create.drop += _.unescape(_.template(tplRelationDown)({
|
||||
tableName: modelName,
|
||||
attribute: attribute,
|
||||
details: details,
|
||||
details: details
|
||||
}));
|
||||
|
||||
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
|
||||
|
||||
@ -34,8 +34,6 @@ module.exports = {
|
||||
*/
|
||||
|
||||
getObject: function (matchedRoute) {
|
||||
// TODO:
|
||||
// - Improve way to detect collection/ressource/relationships/related
|
||||
switch (_.size(matchedRoute.regexp.keys)) {
|
||||
case 0:
|
||||
return 'collection';
|
||||
|
||||
@ -68,7 +68,11 @@ module.exports = {
|
||||
current: '',
|
||||
other: ''
|
||||
};
|
||||
|
||||
|
||||
if (_.isUndefined(models)) {
|
||||
models = global['strapi'].models;
|
||||
}
|
||||
|
||||
if (association.hasOwnProperty('via') && association.hasOwnProperty('collection')) {
|
||||
const relatedAttribute = models[association.collection].attributes[association.via];
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user