Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
198 lines
6.8 KiB
Plaintext
198 lines
6.8 KiB
Plaintext
import { GraphQLBoolean, GraphQLNonNull, GraphQLObjectType } from 'graphql';
|
|
import { toWords } from 'payload';
|
|
import { GraphQLJSONObject } from '../packages/graphql-type-json/index.js';
|
|
import { formatName } from '../utilities/formatName.js';
|
|
const buildFields = (label, fieldsToBuild)=>fieldsToBuild.reduce((builtFields, field)=>{
|
|
const includeField = !field.hidden && field.type !== 'ui';
|
|
if (includeField) {
|
|
if (field.name) {
|
|
const fieldName = formatName(field.name);
|
|
const objectTypeFields = [
|
|
'create',
|
|
'read',
|
|
'update',
|
|
'delete'
|
|
].reduce((operations, operation)=>{
|
|
const capitalizedOperation = operation.charAt(0).toUpperCase() + operation.slice(1);
|
|
return {
|
|
...operations,
|
|
[operation]: {
|
|
type: new GraphQLObjectType({
|
|
name: `${label}_${fieldName}_${capitalizedOperation}`,
|
|
fields: {
|
|
permission: {
|
|
type: new GraphQLNonNull(GraphQLBoolean)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
};
|
|
}, {});
|
|
if (field.fields) {
|
|
objectTypeFields.fields = {
|
|
type: new GraphQLObjectType({
|
|
name: `${label}_${fieldName}_Fields`,
|
|
fields: buildFields(`${label}_${fieldName}`, field.fields)
|
|
})
|
|
};
|
|
}
|
|
return {
|
|
...builtFields,
|
|
[formatName(field.name)]: {
|
|
type: new GraphQLObjectType({
|
|
name: `${label}_${fieldName}`,
|
|
fields: objectTypeFields
|
|
})
|
|
}
|
|
};
|
|
}
|
|
if (!field.name && field.fields && field.fields.length) {
|
|
const subFields = buildFields(label, field.fields);
|
|
return {
|
|
...builtFields,
|
|
...subFields
|
|
};
|
|
}
|
|
if (field.type === 'tabs') {
|
|
return field.tabs.reduce((fieldsWithTabFields, tab)=>{
|
|
if ('name' in tab) {
|
|
if (tab.fields.length) {
|
|
const tabName = formatName(tab.name);
|
|
fieldsWithTabFields[tabName] = {
|
|
type: new GraphQLObjectType({
|
|
name: `${label}_${tabName}`,
|
|
fields: buildFields(`${label}_${tabName}`, tab.fields)
|
|
})
|
|
};
|
|
}
|
|
return fieldsWithTabFields;
|
|
}
|
|
return {
|
|
...fieldsWithTabFields,
|
|
...buildFields(label, tab.fields)
|
|
};
|
|
}, {
|
|
...builtFields
|
|
});
|
|
}
|
|
}
|
|
return builtFields;
|
|
}, {});
|
|
export const buildEntityPolicy = (args)=>{
|
|
const { name, entityFields, operations, scope } = args;
|
|
const fieldsTypeName = toWords(`${name}-${scope || ''}-Fields`, true);
|
|
const fields = {
|
|
fields: {
|
|
type: new GraphQLObjectType({
|
|
name: fieldsTypeName,
|
|
fields: buildFields(fieldsTypeName, entityFields)
|
|
})
|
|
}
|
|
};
|
|
operations.forEach((operation)=>{
|
|
const operationTypeName = toWords(`${name}-${operation}-${scope || 'Access'}`, true);
|
|
fields[operation] = {
|
|
type: new GraphQLObjectType({
|
|
name: operationTypeName,
|
|
fields: {
|
|
permission: {
|
|
type: new GraphQLNonNull(GraphQLBoolean)
|
|
},
|
|
where: {
|
|
type: GraphQLJSONObject
|
|
}
|
|
}
|
|
})
|
|
};
|
|
});
|
|
return fields;
|
|
};
|
|
export function buildPolicyType(args) {
|
|
const { type, entity, scope, typeSuffix } = args;
|
|
const { slug, fields, graphQL, versions } = entity;
|
|
let operations = [];
|
|
if (graphQL === false) {
|
|
return null;
|
|
}
|
|
if (type === 'collection') {
|
|
operations = [
|
|
'create',
|
|
'read',
|
|
'update',
|
|
'delete'
|
|
];
|
|
if (entity.auth && typeof entity.auth === 'object' && typeof entity.auth.maxLoginAttempts !== 'undefined' && entity.auth.maxLoginAttempts !== 0) {
|
|
operations.push('unlock');
|
|
}
|
|
if (versions) {
|
|
operations.push('readVersions');
|
|
}
|
|
const collectionTypeName = formatName(`${slug}${typeSuffix || ''}`);
|
|
return new GraphQLObjectType({
|
|
name: collectionTypeName,
|
|
fields: buildEntityPolicy({
|
|
name: slug,
|
|
entityFields: fields,
|
|
operations,
|
|
scope
|
|
})
|
|
});
|
|
}
|
|
// else create global type
|
|
operations = [
|
|
'read',
|
|
'update'
|
|
];
|
|
if (entity.versions) {
|
|
operations.push('readVersions');
|
|
}
|
|
const globalTypeName = formatName(`${global?.graphQL?.name || slug}${typeSuffix || ''}`);
|
|
return new GraphQLObjectType({
|
|
name: globalTypeName,
|
|
fields: buildEntityPolicy({
|
|
name: entity.graphQL ? entity?.graphQL?.name || slug : slug,
|
|
entityFields: entity.fields,
|
|
operations,
|
|
scope
|
|
})
|
|
});
|
|
}
|
|
export function buildPoliciesType(config) {
|
|
const fields = {
|
|
canAccessAdmin: {
|
|
type: new GraphQLNonNull(GraphQLBoolean)
|
|
}
|
|
};
|
|
Object.values(config.collections).forEach((collection)=>{
|
|
if (collection.graphQL === false) {
|
|
return;
|
|
}
|
|
const collectionPolicyType = buildPolicyType({
|
|
type: 'collection',
|
|
entity: collection,
|
|
typeSuffix: 'Access'
|
|
});
|
|
fields[formatName(collection.slug)] = {
|
|
type: collectionPolicyType
|
|
};
|
|
});
|
|
Object.values(config.globals).forEach((global1)=>{
|
|
if (global1.graphQL === false) {
|
|
return;
|
|
}
|
|
const globalPolicyType = buildPolicyType({
|
|
type: 'global',
|
|
entity: global1,
|
|
typeSuffix: 'Access'
|
|
});
|
|
fields[formatName(global1.slug)] = {
|
|
type: globalPolicyType
|
|
};
|
|
});
|
|
return new GraphQLObjectType({
|
|
name: 'Access',
|
|
fields
|
|
});
|
|
}
|
|
|
|
//# sourceMappingURL=buildPoliciesType.js.map |