Improve performance on findMany queries (#4334)

* Improve performance on findMany queries

* Fix
This commit is contained in:
Charles Bochet 2024-03-06 13:59:42 +01:00 committed by GitHub
parent b2210bd418
commit e7857d7fa3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 64 additions and 77 deletions

View File

@ -69,6 +69,7 @@ export const useActivities = ({
useFindManyRecords<Activity>({
skip: skipActivities,
objectNameSingular: CoreObjectNameSingular.Activity,
depth: 3,
filter,
orderBy: activitiesOrderByVariables,
onCompleted: useRecoilCallback(

View File

@ -10,17 +10,11 @@ export const useMapFieldMetadataToGraphQLQuery = () => {
const mapFieldMetadataToGraphQLQuery = ({
field,
maxDepthForRelations = 2,
onlyTypenameAndIdOnDeepestRelationFields = false,
depth = 2,
}: {
field: FieldMetadataItem;
maxDepthForRelations?: number;
onlyTypenameAndIdOnDeepestRelationFields?: boolean;
depth?: number;
}): any => {
if (maxDepthForRelations <= 0) {
return '';
}
// TODO: parse
const fieldType = field.type as FieldType;
@ -50,26 +44,23 @@ export const useMapFieldMetadataToGraphQLQuery = () => {
(field.toRelationMetadata as any)?.fromObjectMetadata?.id,
);
let subfieldQuery = '';
if (maxDepthForRelations > 0) {
subfieldQuery = `${(relationMetadataItem?.fields ?? [])
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: maxDepthForRelations - 1,
onlyTypenameAndIdOnDeepestRelationFields,
}),
)
.join('\n')}`;
if (depth > 1) {
return `${field.name}
{
__typename
id
${(relationMetadataItem?.fields ?? [])
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
depth: depth - 1,
}),
)
.join('\n')}
}`;
} else {
return '';
}
return `${field.name}
{
__typename
id
${subfieldQuery}
}`;
} else if (
fieldType === 'RELATION' &&
field.toRelationMetadata?.relationType === 'ONE_TO_ONE'
@ -80,26 +71,23 @@ export const useMapFieldMetadataToGraphQLQuery = () => {
(field.toRelationMetadata as any)?.fromObjectMetadata?.id,
);
let subfieldQuery = '';
if (maxDepthForRelations > 0) {
subfieldQuery = `${(relationMetadataItem?.fields ?? [])
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: maxDepthForRelations - 1,
onlyTypenameAndIdOnDeepestRelationFields,
}),
)
.join('\n')}`;
if (depth > 1) {
return `${field.name}
{
__typename
id
${(relationMetadataItem?.fields ?? [])
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
depth: depth - 1,
}),
)
.join('\n')}
}`;
} else {
return '';
}
return `${field.name}
{
__typename
id
${subfieldQuery}
}`;
} else if (
fieldType === 'RELATION' &&
field.fromRelationMetadata?.relationType === 'ONE_TO_MANY'
@ -110,30 +98,27 @@ export const useMapFieldMetadataToGraphQLQuery = () => {
(field.fromRelationMetadata as any)?.toObjectMetadata?.id,
);
let subfieldQuery = '';
if (maxDepthForRelations > 0) {
subfieldQuery = `${(relationMetadataItem?.fields ?? [])
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: maxDepthForRelations - 1,
onlyTypenameAndIdOnDeepestRelationFields,
}),
)
.join('\n')}`;
}
return `${field.name}
{
edges {
node {
__typename
id
${subfieldQuery}
if (depth > 1) {
return `${field.name}
{
edges {
node {
__typename
id
${(relationMetadataItem?.fields ?? [])
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
depth: depth - 1,
}),
)
.join('\n')}
}
}
}
}`;
}`;
} else {
return '';
}
} else if (fieldType === 'LINK') {
return `
${field.name}

View File

@ -36,7 +36,7 @@ export const useAddRecordInCache = ({
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: MAX_QUERY_DEPTH_FOR_CACHE_INJECTION,
depth: MAX_QUERY_DEPTH_FOR_CACHE_INJECTION,
}),
)
.join('\n')}

View File

@ -29,7 +29,7 @@ export const useGenerateFindDuplicateRecordsQuery = () => {
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: depth,
depth,
}),
)
.join('\n')}

View File

@ -74,7 +74,7 @@ export const useGenerateFindManyRecordsForMultipleMetadataItemsQuery = ({
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: depth,
depth,
}),
)
.join('\n')}

View File

@ -31,7 +31,7 @@ export const useGenerateFindManyRecordsQuery = () => {
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: depth,
depth,
}),
)
.join('\n')}

View File

@ -28,7 +28,7 @@ export const useGenerateFindOneRecordQuery = () => {
.map((field) =>
mapFieldMetadataToGraphQLQuery({
field,
maxDepthForRelations: depth,
depth,
}),
)
.join('\n')}

View File

@ -9,7 +9,8 @@
"**/*.spec.tsx",
"**/*.test.tsx",
"jest.config.ts",
"tsup.config.ts"
"tsup.config.ts",
"tsup.ui.index.tsx"
],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}

View File

@ -28,7 +28,7 @@ export * from './src/modules/ui/input/button/components/FloatingButtonGroup'
export * from './src/modules/ui/input/button/components/FloatingIconButton'
export * from './src/modules/ui/input/button/components/FloatingIconButtonGroup'
export * from './src/modules/ui/input/button/components/LightButton'
export * from '@/ui/navigation/link/components/ActionLink.tsx'
export * from './src/modules/ui/navigation/link/components/ActionLink.tsx'
export * from './src/modules/ui/input/button/components/LightIconButton'
export * from './src/modules/ui/input/button/components/MainButton'
export * from './src/modules/ui/input/button/components/RoundedIconButton'