Skip to content

Commit

Permalink
fix: graphql where types on rows and collapsible's (#2758)
Browse files Browse the repository at this point in the history
Co-authored-by: Kári Yngvason <kari@hugsmidjan.is>
Co-authored-by: NikolaGanchev <62907292+NikolaGanchev@users.noreply.github.com>
Co-authored-by: James <james@trbl.design>
  • Loading branch information
4 people committed Jun 2, 2023
1 parent dbb0137 commit f978299
Show file tree
Hide file tree
Showing 16 changed files with 3,651 additions and 390 deletions.
18 changes: 6 additions & 12 deletions src/auth/operations/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@ async function accessOperation(args: Arguments): Promise<Permissions> {
adminInitTelemetry(req);

const results = {} as Permissions;
const promises = [];

const isLoggedIn = !!(user);
const userCollectionConfig = (user && user.collection) ? config.collections.find((collection) => collection.slug === user.collection) : null;


if (userCollectionConfig) {
results.canAccessAdmin = userCollectionConfig.access.admin ? await userCollectionConfig.access.admin(args) : isLoggedIn;
} else {
results.canAccessAdmin = false;
}

config.collections.forEach((collection) => {
await Promise.all(config.collections.map(async (collection) => {
const collectionOperations = [...allOperations];

if (collection.auth && (typeof collection.auth.maxLoginAttempts !== 'undefined' && collection.auth.maxLoginAttempts !== 0)) {
Expand All @@ -47,7 +45,7 @@ async function accessOperation(args: Arguments): Promise<Permissions> {
collectionOperations.push('readVersions');
}

const [collectionPolicy, collectionPromises] = getEntityPolicies({
const collectionPolicy = await getEntityPolicies({
type: 'collection',
req,
entity: collection,
Expand All @@ -57,17 +55,16 @@ async function accessOperation(args: Arguments): Promise<Permissions> {
...results.collections,
[collection.slug]: collectionPolicy,
};
promises.push(...collectionPromises);
});
}));

config.globals.forEach((global) => {
await Promise.all(config.globals.map(async (global) => {
const globalOperations: AllOperations[] = ['read', 'update'];

if (global.versions) {
globalOperations.push('readVersions');
}

const [globalPolicy, globalPromises] = getEntityPolicies({
const globalPolicy = await getEntityPolicies({
type: 'global',
req,
entity: global,
Expand All @@ -77,10 +74,7 @@ async function accessOperation(args: Arguments): Promise<Permissions> {
...results.globals,
[global.slug]: globalPolicy,
};
promises.push(...globalPromises);
});

await Promise.all(promises);
}));

return results;
}
Expand Down
6 changes: 1 addition & 5 deletions src/collections/operations/docAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ export async function docAccess(args: Arguments): Promise<CollectionPermission>
collectionOperations.push('readVersions');
}

const [policy, promises] = getEntityPolicies({
return getEntityPolicies({
type: 'collection',
req,
entity: config,
operations: collectionOperations,
id,
});

await Promise.all(promises);

return policy;
}
6 changes: 1 addition & 5 deletions src/globals/operations/docAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ export async function docAccess(args: Arguments): Promise<GlobalPermission> {
globalOperations.push('readVersions');
}

const [policy, promises] = getEntityPolicies({
return getEntityPolicies({
type: 'global',
req,
entity: globalConfig,
operations: globalOperations,
});

await Promise.all(promises);

return policy;
}
95 changes: 6 additions & 89 deletions src/graphql/schema/fieldToWhereInputSchemaMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import combineParentName from '../utilities/combineParentName';
import formatName from '../utilities/formatName';
import recursivelyBuildNestedPaths from './recursivelyBuildNestedPaths';

const fieldToSchemaMap = (parentName: string): any => ({
const fieldToSchemaMap = (parentName: string, nestedFieldName?: string): any => ({
number: (field: NumberField) => ({
type: withOperators(
field,
Expand Down Expand Up @@ -130,94 +130,11 @@ const fieldToSchemaMap = (parentName: string): any => ({
parentName,
),
}),
array: (field: ArrayField) => recursivelyBuildNestedPaths(parentName, field),
group: (field: GroupField) => recursivelyBuildNestedPaths(parentName, field),
row: (field: RowField) => field.fields.reduce((rowSchema, subField) => {
const getFieldSchema = fieldToSchemaMap(parentName)[subField.type];

if (getFieldSchema) {
const rowFieldSchema = getFieldSchema(subField);

if (fieldHasSubFields(subField)) {
return [
...rowSchema,
...rowFieldSchema,
];
}

if (fieldAffectsData(subField)) {
return [
...rowSchema,
{
key: subField.name,
type: rowFieldSchema,
},
];
}
}


return rowSchema;
}, []),
collapsible: (field: CollapsibleField) => field.fields.reduce((rowSchema, subField) => {
const getFieldSchema = fieldToSchemaMap(parentName)[subField.type];

if (getFieldSchema) {
const rowFieldSchema = getFieldSchema(subField);

if (fieldHasSubFields(subField)) {
return [
...rowSchema,
...rowFieldSchema,
];
}

if (fieldAffectsData(subField)) {
return [
...rowSchema,
{
key: subField.name,
type: rowFieldSchema,
},
];
}
}


return rowSchema;
}, []),
tabs: (field: TabsField) => field.tabs.reduce((tabSchema, tab) => {
return [
...tabSchema,
...tab.fields.reduce((rowSchema, subField) => {
const getFieldSchema = fieldToSchemaMap(parentName)[subField.type];

if (getFieldSchema) {
const rowFieldSchema = getFieldSchema(subField);

if (fieldHasSubFields(subField)) {
return [
...rowSchema,
...rowFieldSchema,
];
}

if (fieldAffectsData(subField)) {
return [
...rowSchema,
{
key: subField.name,
type: rowFieldSchema,
},
];
}
}


return rowSchema;
}, []),
];
}, []),
array: (field: ArrayField) => recursivelyBuildNestedPaths(parentName, nestedFieldName, field),
group: (field: GroupField) => recursivelyBuildNestedPaths(parentName, nestedFieldName, field),
row: (field: RowField) => recursivelyBuildNestedPaths(parentName, nestedFieldName, field),
collapsible: (field: CollapsibleField) => recursivelyBuildNestedPaths(parentName, nestedFieldName, field),
tabs: (field: TabsField) => recursivelyBuildNestedPaths(parentName, nestedFieldName, field),
});

export default fieldToSchemaMap;
31 changes: 25 additions & 6 deletions src/graphql/schema/recursivelyBuildNestedPaths.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import {
FieldAffectingData,
fieldAffectsData,
fieldIsPresentationalOnly,
FieldWithSubFields,
TabsField,
} from '../../fields/config/types';
import fieldToSchemaMap from './fieldToWhereInputSchemaMap';

const recursivelyBuildNestedPaths = (parentName: string, field: FieldWithSubFields & FieldAffectingData) => {
const recursivelyBuildNestedPaths = (parentName: string, nestedFieldName2: string, field: FieldWithSubFields | TabsField) => {
const fieldName = fieldAffectsData(field) ? field.name : undefined;
const nestedFieldName = fieldName || nestedFieldName2;

if (field.type === 'tabs') {
// if the tab has a name, treat it as a group
// otherwise, treat it as a row
return field.tabs.reduce((tabSchema, tab: any) => {
tabSchema.push(...recursivelyBuildNestedPaths(parentName, nestedFieldName, {
...tab,
type: 'name' in tab ? 'group' : 'row',
}));
return tabSchema;
}, []);
}

const nestedPaths = field.fields.reduce((nestedFields, nestedField) => {
if (!fieldIsPresentationalOnly(nestedField)) {
const getFieldSchema = fieldToSchemaMap(parentName)[nestedField.type];
const nestedFieldName = fieldAffectsData(nestedField) ? `${field.name}__${nestedField.name}` : undefined;
if (!fieldAffectsData(nestedField)) {
return recursivelyBuildNestedPaths(parentName, nestedFieldName, nestedField);
}

const nestedPathName = fieldAffectsData(nestedField) ? `${nestedFieldName ? `${nestedFieldName}__` : ''}${nestedField.name}` : undefined;
const getFieldSchema = fieldToSchemaMap(parentName, nestedFieldName)[nestedField.type];

if (getFieldSchema) {
const fieldSchema = getFieldSchema({
...nestedField,
name: nestedFieldName,
name: nestedPathName,
});

if (Array.isArray(fieldSchema)) {
Expand All @@ -28,7 +47,7 @@ const recursivelyBuildNestedPaths = (parentName: string, field: FieldWithSubFiel
return [
...nestedFields,
{
key: nestedFieldName,
key: nestedPathName,
type: fieldSchema,
},
];
Expand Down
4 changes: 3 additions & 1 deletion src/graphql/schema/withOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ export const withOperators = (field: FieldAffectingData, parentName: string): Gr
const fieldOperators = [...defaults[field.type].operators];
if (!('required' in field) || !field.required) fieldOperators.push('exists');

let gqlType: GraphQLType = typeof defaults[field.type].type === 'function'
const initialGqlType: GraphQLType = typeof defaults[field.type].type === 'function'
? defaults[field.type].type(field, parentName)
: defaults?.[field.type].type;

return new GraphQLInputObjectType({
name,
fields: fieldOperators.reduce((objectTypeFields, operator) => {
let gqlType = initialGqlType;

if (listOperators.includes(operator)) {
gqlType = new GraphQLList(gqlType);
} else if (operator === 'exists') {
Expand Down
6 changes: 2 additions & 4 deletions src/mongoose/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,13 @@ export class ParamParser {
collection.fields = fields;

if (!this.policies.collections[collectionSlug]) {
const [policy, promises] = getEntityPolicies({
const policy = await getEntityPolicies({
req: this.req,
entity: collection,
operations: ['read'],
type: 'collection',
});

await Promise.all(promises);
this.policies.collections[collectionSlug] = policy;
}

Expand All @@ -405,14 +404,13 @@ export class ParamParser {
const global = { ...this.req.payload.globals.config.find(({ slug }) => slug === globalSlug) };
global.fields = fields;

const [policy, promises] = getEntityPolicies({
const policy = await getEntityPolicies({
req: this.req,
entity: global,
operations: ['read'],
type: 'global',
});

await Promise.all(promises);
this.policies.globals[globalSlug] = policy;
}

Expand Down

0 comments on commit f978299

Please sign in to comment.