Fix GraphQL and clean guidelines

This commit is contained in:
Aurélien Georget 2016-04-19 17:29:19 +02:00
parent a8aa89b5e3
commit c358e72b3c
6 changed files with 17 additions and 15 deletions

View File

@ -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) {

View File

@ -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.

View File

@ -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

View File

@ -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');

View File

@ -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';

View File

@ -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];