Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to set a relation "private" #5494

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const uploadImg = () => {
describe.each([
[
'CONTENT MANAGER',
'/content-manager/explorer/application::withdynamiczone.withdynamiczone',
'/content-manager/explorer/application::withdynamiczonemedia.withdynamiczonemedia',
],
['GENERATED API', '/withdynamiczones'],
['GENERATED API', '/withdynamiczonemedias'],
])('[%s] => Not required dynamiczone', (_, path) => {
beforeAll(async () => {
const token = await registerAndLogin();
Expand Down Expand Up @@ -61,17 +61,9 @@ describe.each([
},
});

await modelsUtils.createContentTypeWithType(
'withdynamiczone',
'dynamiczone',
{
components: [
'default.single-media',
'default.multiple-media',
'default.with-nested',
],
}
);
await modelsUtils.createContentTypeWithType('withdynamiczonemedia', 'dynamiczone', {
components: ['default.single-media', 'default.multiple-media', 'default.with-nested'],
});

rq = authRq.defaults({
baseUrl: `http://localhost:1337${path}`,
Expand All @@ -82,7 +74,7 @@ describe.each([
await modelsUtils.deleteComponent('default.with-nested');
await modelsUtils.deleteComponent('default.single-media');
await modelsUtils.deleteComponent('default.multiple-media');
await modelsUtils.deleteContentType('withdynamiczone');
await modelsUtils.deleteContentType('withdynamiczonemedia');
}, 60000);

describe('Contains components with medias', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,20 @@ module.exports = {
return ctx.send({ error }, 400);
}

const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;

const contentTypes = Object.keys(strapi.contentTypes)
.filter(uid => {
if (uid.startsWith('strapi::')) return false;
if (uid === 'plugins::upload.file') return false; // TODO: add a flag in the content type instead

if (
kind &&
_.get(strapi.contentTypes[uid], 'kind', 'collectionType') !== kind
) {
if (kind && _.get(strapi.contentTypes[uid], 'kind', 'collectionType') !== kind) {
return false;
}

return true;
})
.map(uid =>
contentTypeService.formatContentType(strapi.contentTypes[uid])
);
.map(uid => contentTypeService.formatContentType(strapi.contentTypes[uid]));

ctx.send({
data: contentTypes,
Expand All @@ -53,8 +47,7 @@ module.exports = {
return ctx.send({ error: 'contentType.notFound' }, 404);
}

const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;

ctx.send({ data: contentTypeService.formatContentType(contentType) });
},
Expand All @@ -71,8 +64,7 @@ module.exports = {
try {
strapi.reload.isWatching = false;

const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;

const component = await contentTypeService.createContentType({
contentType: body.contentType,
Expand Down Expand Up @@ -112,8 +104,7 @@ module.exports = {
try {
strapi.reload.isWatching = false;

const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;

const component = await contentTypeService.editContentType(uid, {
contentType: body.contentType,
Expand All @@ -139,8 +130,7 @@ module.exports = {
try {
strapi.reload.isWatching = false;

const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;

const component = await contentTypeService.deleteContentType(uid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ module.exports = (obj, validNatures) => {
.test(isValidName)
.nullable(),
targetColumnName: yup.string().nullable(),
private: yup.boolean().nullable(),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,12 @@ function createSchemaBuilder({ components, contentTypes }) {

// init temporary ContentTypes
Object.keys(contentTypes).forEach(key => {
tmpContentTypes.set(
contentTypes[key].uid,
createSchemaHandler(contentTypes[key])
);
tmpContentTypes.set(contentTypes[key].uid, createSchemaHandler(contentTypes[key]));
});

// init temporary components
Object.keys(components).forEach(key => {
tmpComponents.set(
components[key].uid,
createSchemaHandler(components[key])
);
tmpComponents.set(components[key].uid, createSchemaHandler(components[key]));
});

return {
Expand Down Expand Up @@ -120,12 +114,14 @@ function createSchemaBuilder({ components, contentTypes }) {
columnName,
dominant,
autoPopulate,
private: isPrivate,
} = attribute;

const attr = {
unique: unique === true ? true : undefined,
columnName: columnName || undefined,
configurable: configurable === false ? false : undefined,
private: isPrivate === true ? true : undefined,
autoPopulate,
};

Expand Down
18 changes: 6 additions & 12 deletions packages/strapi-plugin-graphql/services/type-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const GraphQLLong = require('graphql-type-long');
const Time = require('../types/time');
const { toSingular, toInputName } = require('./naming');

const isScalarAttribute = ({ type }) =>
type && !['component', 'dynamiczone'].includes(type);
const isScalarAttribute = ({ type }) => type && !['component', 'dynamiczone'].includes(type);

module.exports = {
/**
Expand Down Expand Up @@ -90,9 +89,7 @@ module.exports = {
typeName =
action === 'update'
? `edit${_.upperFirst(toSingular(globalId))}Input`
: `${_.upperFirst(toSingular(globalId))}Input${
required ? '!' : ''
}`;
: `${_.upperFirst(toSingular(globalId))}Input${required ? '!' : ''}`;
}

if (repeatable === true) {
Expand All @@ -104,9 +101,7 @@ module.exports = {
if (attribute.type === 'dynamiczone') {
const { required } = attribute;

const unionName = `${modelName}${_.upperFirst(
_.camelCase(attributeName)
)}DynamicZone`;
const unionName = `${modelName}${_.upperFirst(_.camelCase(attributeName))}DynamicZone`;

let typeName = unionName;

Expand Down Expand Up @@ -202,9 +197,7 @@ module.exports = {
addPolymorphicUnionType(definition) {
const types = graphql
.parse(definition)
.definitions.filter(
def => def.kind === 'ObjectTypeDefinition' && def.name.value !== 'Query'
)
.definitions.filter(def => def.kind === 'ObjectTypeDefinition' && def.name.value !== 'Query')
.map(def => def.name.value);

if (types.length > 0) {
Expand Down Expand Up @@ -250,7 +243,7 @@ module.exports = {

const inputs = `
input ${inputName} {

${Object.keys(model.attributes)
.map(attributeName => {
return `${attributeName}: ${this.convertType({
Expand Down Expand Up @@ -278,6 +271,7 @@ module.exports = {
.join('\n')}
}
`;

return inputs;
},

Expand Down
53 changes: 14 additions & 39 deletions packages/strapi-plugin-graphql/services/type-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ const DynamicZoneScalar = require('../types/dynamiczoneScalar');

const { formatModelConnectionsGQL } = require('./build-aggregation');
const types = require('./type-builder');
const {
mergeSchemas,
convertToParams,
convertToQuery,
amountLimiting,
} = require('./utils');
const { mergeSchemas, convertToParams, convertToQuery, amountLimiting } = require('./utils');
const { toSDL, getTypeDescription } = require('./schema-definitions');
const { toSingular, toPlural } = require('./naming');
const { buildQuery, buildMutation } = require('./resolvers-builder');
Expand Down Expand Up @@ -60,10 +55,10 @@ const buildTypeDefObj = model => {
// Change field definition for collection relations
associations
.filter(association => association.type === 'collection')
.filter(association => attributes[association.alias].private !== true)
.forEach(association => {
typeDef[
`${association.alias}(sort: String, limit: Int, start: Int, where: JSON)`
] = typeDef[association.alias];
typeDef[`${association.alias}(sort: String, limit: Int, start: Int, where: JSON)`] =
typeDef[association.alias];

delete typeDef[association.alias];
});
Expand All @@ -90,9 +85,7 @@ const generateDynamicZoneDefinitions = (attributes, globalId, schema) => {
.forEach(attribute => {
const { components } = attributes[attribute];

const typeName = `${globalId}${_.upperFirst(
_.camelCase(attribute)
)}DynamicZone`;
const typeName = `${globalId}${_.upperFirst(_.camelCase(attribute))}DynamicZone`;

if (components.length === 0) {
// Create dummy type because graphql doesn't support empty ones
Expand All @@ -111,9 +104,7 @@ const generateDynamicZoneDefinitions = (attributes, globalId, schema) => {
return compo.globalId;
});

const unionType = `union ${typeName} = ${componentsTypeNames.join(
' | '
)}`;
const unionType = `union ${typeName} = ${componentsTypeNames.join(' | ')}`;

schema.definition += `\n${unionType}\n`;
}
Expand All @@ -137,8 +128,7 @@ const generateDynamicZoneDefinitions = (attributes, globalId, schema) => {
};

const buildAssocResolvers = model => {
const contentManager =
strapi.plugins['content-manager'].services['contentmanager'];
const contentManager = strapi.plugins['content-manager'].services['contentmanager'];

const { primaryKey, associations = [] } = model;

Expand Down Expand Up @@ -194,40 +184,29 @@ const buildAssocResolvers = model => {
};

if (
((association.nature === 'manyToMany' &&
association.dominant) ||
((association.nature === 'manyToMany' && association.dominant) ||
association.nature === 'manyWay') &&
_.has(obj, association.alias) // if populated
) {
_.set(
queryOpts,
['query', targetModel.primaryKey],
obj[association.alias]
? obj[association.alias]
.map(val => val[targetModel.primaryKey] || val)
.sort()
? obj[association.alias].map(val => val[targetModel.primaryKey] || val).sort()
: []
);
} else {
_.set(
queryOpts,
['query', association.via],
obj[targetModel.primaryKey]
);
_.set(queryOpts, ['query', association.via], obj[targetModel.primaryKey]);
}
}

return association.model
? strapi.plugins.graphql.services['data-loaders'].loaders[
targetModel.uid
].load({
? strapi.plugins.graphql.services['data-loaders'].loaders[targetModel.uid].load({
params,
options: queryOpts,
single: true,
})
: strapi.plugins.graphql.services['data-loaders'].loaders[
targetModel.uid
].load({
: strapi.plugins.graphql.services['data-loaders'].loaders[targetModel.uid].load({
options: queryOpts,
association,
});
Expand Down Expand Up @@ -308,9 +287,7 @@ const buildSingleType = model => {

const singularName = toSingular(modelName);

const _schema = _.cloneDeep(
_.get(strapi.plugins, 'graphql.config._schema.graphql', {})
);
const _schema = _.cloneDeep(_.get(strapi.plugins, 'graphql.config._schema.graphql', {}));

const globalType = _.get(_schema, ['type', model.globalId], {});

Expand Down Expand Up @@ -357,9 +334,7 @@ const buildCollectionType = model => {
const singularName = toSingular(modelName);
const pluralName = toPlural(modelName);

const _schema = _.cloneDeep(
_.get(strapi.plugins, 'graphql.config._schema.graphql', {})
);
const _schema = _.cloneDeep(_.get(strapi.plugins, 'graphql.config._schema.graphql', {}));

const globalType = _.get(_schema, ['type', model.globalId], {});

Expand Down