Skip to content

Commit 7783bff

Browse files
committed
fix: dashboard queries would take too much time if context
1 parent 70217f5 commit 7783bff

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

src/schema/query/dashboard.query.ts

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
import { GraphQLNonNull, GraphQLID, GraphQLError } from 'graphql';
22
import { DashboardType } from '../types';
3-
import {
4-
ApiConfiguration,
5-
Dashboard,
6-
Page,
7-
Record,
8-
ReferenceData,
9-
} from '@models';
3+
import { Dashboard } from '@models';
104
import extendAbilityForContent from '@security/extendAbilityForContent';
11-
import { CustomAPI } from '@server/apollo/dataSources';
12-
import { Types } from 'mongoose';
135
import { logger } from '@services/logger.service';
146

157
/**
@@ -39,55 +31,58 @@ export default {
3931
context.i18next.t('common.errors.permissionNotGranted')
4032
);
4133
}
34+
// todo: reactivate if we want to inject context in dashboard
4235
// Check if dashboard has context linked to it
43-
const page = await Page.findOne({
44-
contentWithContext: { $elemMatch: { content: args.id } },
45-
});
36+
// const page = await Page.findOne({
37+
// contentWithContext: { $elemMatch: { content: args.id } },
38+
// });
4639

4740
// If a page was found, means the dashboard has context
48-
if (page && page.context) {
49-
// get the id of the resource or refData
50-
const contentWithContext = page.contentWithContext.find((c) =>
51-
(c.content as Types.ObjectId).equals(args.id)
52-
);
53-
const id =
54-
'element' in contentWithContext && contentWithContext.element
55-
? contentWithContext.element
56-
: 'record' in contentWithContext && contentWithContext.record
57-
? contentWithContext.record
58-
: null;
41+
// if (page && page.context) {
42+
// // get the id of the resource or refData
43+
// const contentWithContext = page.contentWithContext.find((c) =>
44+
// (c.content as Types.ObjectId).equals(args.id)
45+
// );
46+
// const id =
47+
// 'element' in contentWithContext && contentWithContext.element
48+
// ? contentWithContext.element
49+
// : 'record' in contentWithContext && contentWithContext.record
50+
// ? contentWithContext.record
51+
// : null;
5952

60-
const ctx = page.context;
61-
let data: any;
53+
// const ctx = page.context;
54+
// let data: any;
6255

63-
if ('resource' in ctx && ctx.resource) {
64-
const record = await Record.findById(id);
65-
data = record.data;
66-
} else if ('refData' in ctx && ctx.refData) {
67-
// get refData from page
68-
const referenceData = await ReferenceData.findById(ctx.refData);
69-
const apiConfiguration = await ApiConfiguration.findById(
70-
referenceData.apiConfiguration
71-
);
72-
const items = apiConfiguration
73-
? await (
74-
context.dataSources[apiConfiguration.name] as CustomAPI
75-
).getReferenceDataItems(referenceData, apiConfiguration)
76-
: referenceData.data;
77-
data = items.find((x) => x[referenceData.valueField] === id);
78-
}
56+
// if ('resource' in ctx && ctx.resource) {
57+
// const record = await Record.findById(id);
58+
// data = record.data;
59+
// } else if ('refData' in ctx && ctx.refData) {
60+
// // get refData from page
61+
// const referenceData = await ReferenceData.findById(ctx.refData);
62+
// const apiConfiguration = await ApiConfiguration.findById(
63+
// referenceData.apiConfiguration
64+
// );
65+
// const items = apiConfiguration
66+
// ? await (
67+
// context.dataSources[apiConfiguration.name] as CustomAPI
68+
// ).getReferenceDataItems(referenceData, apiConfiguration)
69+
// : referenceData.data;
70+
// data = items.find((x) => x[referenceData.valueField] === id);
71+
// }
7972

80-
const stringifiedStructure = JSON.stringify(dashboard.structure);
81-
const regex = /{{context\.(.*?)}}/g;
73+
// const stringifiedStructure = JSON.stringify(dashboard.structure);
74+
// const regex = /{{context\.(.*?)}}/g;
8275

83-
// replace all {{context.<field>}} with the value from the data
84-
dashboard.structure = JSON.parse(
85-
stringifiedStructure.replace(regex, (match) => {
86-
const field = match.replace('{{context.', '').replace('}}', '');
87-
return data[field] || match;
88-
})
89-
);
90-
}
76+
// console.log(stringifiedStructure);
77+
78+
// // replace all {{context.<field>}} with the value from the data
79+
// dashboard.structure = JSON.parse(
80+
// stringifiedStructure.replace(regex, (match) => {
81+
// const field = match.replace('{{context.', '').replace('}}', '');
82+
// return data[field] || match;
83+
// })
84+
// );
85+
// }
9186
return dashboard;
9287
} catch (err) {
9388
logger.error(err.message, { stack: err.stack });

0 commit comments

Comments
 (0)