From ec442785e719298f1dca0ccac0debc39963d1ac1 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Fri, 26 Jul 2024 16:55:14 +0100 Subject: [PATCH 1/8] enums queries and example with maintenance --- backend/dataall/base/utils/enum_utils.py | 26 ++++++++++++++++ .../modules/maintenance/api/queries.py | 6 ++++ .../components/MaintenanceViewer.js | 31 +++++++++++++++---- .../services/graphql/Shared/getEnumByName.js | 15 +++++++++ frontend/src/services/graphql/Shared/index.js | 1 + frontend/src/services/graphql/index.js | 1 + 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 backend/dataall/base/utils/enum_utils.py create mode 100644 frontend/src/services/graphql/Shared/getEnumByName.js create mode 100644 frontend/src/services/graphql/Shared/index.js diff --git a/backend/dataall/base/utils/enum_utils.py b/backend/dataall/base/utils/enum_utils.py new file mode 100644 index 000000000..1cfbb144f --- /dev/null +++ b/backend/dataall/base/utils/enum_utils.py @@ -0,0 +1,26 @@ +from dataall.base.api import gql + + +def generate_enum_resolver(enum_class): + def resolver(context, source): + return [{'name': item.name, 'value': item.value} for item in enum_class] + + return resolver + + +EnumItem = gql.ObjectType( + name='EnumItem', + fields=[ + gql.Field(name='name', type=gql.String), + gql.Field(name='value', type=gql.String), + ], +) + + +def generate_enum_query(enum_class, test_scope): + return gql.QueryField( + name=enum_class.__name__, + type=gql.ArrayType(EnumItem), + resolver=generate_enum_resolver(enum_class), + test_scope=test_scope, + ) diff --git a/backend/dataall/modules/maintenance/api/queries.py b/backend/dataall/modules/maintenance/api/queries.py index 11c623cf0..097a0cd0d 100644 --- a/backend/dataall/modules/maintenance/api/queries.py +++ b/backend/dataall/modules/maintenance/api/queries.py @@ -1,9 +1,15 @@ """The module defines GraphQL queries for the Maintenance Activity>""" from dataall.base.api import gql +from dataall.base.utils.enum_utils import generate_enum_query +from dataall.modules.maintenance.api.enums import MaintenanceModes, MaintenanceStatus from dataall.modules.maintenance.api.resolvers import get_maintenance_window_status getMaintenanceWindowStatus = gql.QueryField( name='getMaintenanceWindowStatus', type=gql.Ref('Maintenance'), resolver=get_maintenance_window_status ) + +queryMaintenanceModes = generate_enum_query(MaintenanceModes, 'Maintenance') + +queryMaintenanceStatus = generate_enum_query(MaintenanceStatus, 'Maintenance') diff --git a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js index 8fcf252f0..1904f4423 100644 --- a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js +++ b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js @@ -27,16 +27,11 @@ import { startMaintenanceWindow, startReindexCatalog } from '../services'; -import { useClient } from 'services'; +import { getEnumByName, useClient } from 'services'; import { SET_ERROR, useDispatch } from 'globalErrors'; import { useSnackbar } from 'notistack'; import { ModuleNames, isModuleEnabled } from 'utils'; -const maintenanceModes = [ - { value: 'READ-ONLY', label: 'Read-Only' }, - { value: 'NO-ACCESS', label: 'No-Access' } -]; - const START_MAINTENANCE = 'Start Maintenance'; const END_MAINTENANCE = 'End Maintenance'; export const PENDING_STATUS = 'PENDING'; @@ -314,11 +309,32 @@ export const MaintenanceViewer = () => { useState(START_MAINTENANCE); const [maintenanceWindowStatus, setMaintenanceWindowStatus] = useState(INACTIVE_STATUS); + const [maintenanceModes, setMaintenanceModes] = useState([]); const [dropDownStatus, setDropDownStatus] = useState(false); const [refreshingTimer, setRefreshingTimer] = useState(''); const { enqueueSnackbar, closeSnackbar } = useSnackbar(); const dispatch = useDispatch(); + const fetchMaintenanceModes = useCallback(async () => { + const response = await client.query( + getEnumByName({ enum_name: 'MaintenanceModes' }) + ); + if (!response.errors && response.data.MaintenanceModes != null) { + const modes = response.data.MaintenanceModes; + if (modes.length > 0) { + setMaintenanceModes( + modes.map((m) => { + return { value: m.name, label: m.value }; + }) + ); + } + } else { + const error = response.errors + ? response.errors[0].message + : 'Could not fetch maintenance modes'; + dispatch({ type: SET_ERROR, error }); + } + }, [client]); const refreshMaintenanceView = async () => { setUpdating(true); setRefreshing(true); @@ -468,6 +484,9 @@ export const MaintenanceViewer = () => { initializeMaintenanceView().catch((e) => dispatch({ type: SET_ERROR, e }) ); + fetchMaintenanceModes().catch((e) => + dispatch({ type: SET_ERROR, error: e.message }) + ); const setTimer = setInterval(() => { refreshStatus().catch((e) => dispatch({ type: SET_ERROR, error: e.message }) diff --git a/frontend/src/services/graphql/Shared/getEnumByName.js b/frontend/src/services/graphql/Shared/getEnumByName.js new file mode 100644 index 000000000..b20d4ebd3 --- /dev/null +++ b/frontend/src/services/graphql/Shared/getEnumByName.js @@ -0,0 +1,15 @@ +import { gql } from 'apollo-boost'; + +export const getEnumByName = ({ enum_name }) => ({ + variables: { + enum_name + }, + query: gql` + query ${enum_name}{ + ${enum_name}{ + name + value + } + } + ` +}); diff --git a/frontend/src/services/graphql/Shared/index.js b/frontend/src/services/graphql/Shared/index.js new file mode 100644 index 000000000..af6921f17 --- /dev/null +++ b/frontend/src/services/graphql/Shared/index.js @@ -0,0 +1 @@ +export * from './getEnumByName'; diff --git a/frontend/src/services/graphql/index.js b/frontend/src/services/graphql/index.js index ce1c3fba2..144c217fe 100644 --- a/frontend/src/services/graphql/index.js +++ b/frontend/src/services/graphql/index.js @@ -13,6 +13,7 @@ export * from './Notification'; export * from './Organization'; export * from './Principal'; export * from './SavedQuery'; +export * from './Shared'; export * from './Search'; export * from './ShareObject'; export * from './Stack'; From 4b0790df47697963555a4be6b21887ec99d7e97b Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Mon, 29 Jul 2024 15:58:06 +0100 Subject: [PATCH 2/8] shared enumFetcher in services --- .../components/MaintenanceViewer.js | 31 +++++++------------ .../services/graphql/Shared/getEnumByName.js | 11 ++++++- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js index 1904f4423..83426de4e 100644 --- a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js +++ b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js @@ -27,7 +27,7 @@ import { startMaintenanceWindow, startReindexCatalog } from '../services'; -import { getEnumByName, useClient } from 'services'; +import { useClient, fetchEnum } from 'services'; import { SET_ERROR, useDispatch } from 'globalErrors'; import { useSnackbar } from 'notistack'; import { ModuleNames, isModuleEnabled } from 'utils'; @@ -315,26 +315,19 @@ export const MaintenanceViewer = () => { const { enqueueSnackbar, closeSnackbar } = useSnackbar(); const dispatch = useDispatch(); - const fetchMaintenanceModes = useCallback(async () => { - const response = await client.query( - getEnumByName({ enum_name: 'MaintenanceModes' }) - ); - if (!response.errors && response.data.MaintenanceModes != null) { - const modes = response.data.MaintenanceModes; - if (modes.length > 0) { - setMaintenanceModes( - modes.map((m) => { - return { value: m.name, label: m.value }; - }) - ); - } + const fetchMaintenanceModes = async () => { + const maintenanceModesEnum = await fetchEnum(client, 'MaintenanceModes'); + if (maintenanceModesEnum.length > 0) { + setMaintenanceModes( + maintenanceModesEnum.map((elem) => { + return { label: elem.value, value: elem.name }; + }) + ); } else { - const error = response.errors - ? response.errors[0].message - : 'Could not fetch maintenance modes'; - dispatch({ type: SET_ERROR, error }); + dispatch({ type: SET_ERROR, error: 'Could not fetch maintenance modes' }); } - }, [client]); + }; + const refreshMaintenanceView = async () => { setUpdating(true); setRefreshing(true); diff --git a/frontend/src/services/graphql/Shared/getEnumByName.js b/frontend/src/services/graphql/Shared/getEnumByName.js index b20d4ebd3..cd0c60691 100644 --- a/frontend/src/services/graphql/Shared/getEnumByName.js +++ b/frontend/src/services/graphql/Shared/getEnumByName.js @@ -1,6 +1,6 @@ import { gql } from 'apollo-boost'; -export const getEnumByName = ({ enum_name }) => ({ +const getEnumByName = ({ enum_name }) => ({ variables: { enum_name }, @@ -13,3 +13,12 @@ export const getEnumByName = ({ enum_name }) => ({ } ` }); + +export const fetchEnum = async (client, enum_name) => { + const response = await client.query(getEnumByName({ enum_name: enum_name })); + if (!response.errors && response.data[enum_name] != null) { + return response.data[enum_name]; + } else { + return []; + } +}; From c22e481c396f8baeca92b7c7509dae34f5861a24 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Mon, 29 Jul 2024 16:04:45 +0100 Subject: [PATCH 3/8] all enums automatically have queries --- backend/dataall/base/api/__init__.py | 3 +++ backend/dataall/base/utils/enum_utils.py | 2 +- backend/dataall/modules/maintenance/api/queries.py | 5 ----- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/backend/dataall/base/api/__init__.py b/backend/dataall/base/api/__init__.py index 4aef8e332..42a7fe07f 100644 --- a/backend/dataall/base/api/__init__.py +++ b/backend/dataall/base/api/__init__.py @@ -12,6 +12,7 @@ from dataall.base.api import gql from dataall.base.api.constants import GraphQLEnumMapper +from dataall.base.utils.enum_utils import generate_enum_query def bootstrap(): @@ -30,6 +31,8 @@ def bootstrap(): for enumclass in GraphQLEnumMapper.__subclasses__(): enumclass.toGraphQLEnum() + generate_enum_query(enumclass, 'Enums') + for cls in classes.keys(): if not cls.class_instances: # if there are no instances of cls registered in the app diff --git a/backend/dataall/base/utils/enum_utils.py b/backend/dataall/base/utils/enum_utils.py index 1cfbb144f..51ae9ff4c 100644 --- a/backend/dataall/base/utils/enum_utils.py +++ b/backend/dataall/base/utils/enum_utils.py @@ -3,7 +3,7 @@ def generate_enum_resolver(enum_class): def resolver(context, source): - return [{'name': item.name, 'value': item.value} for item in enum_class] + return [{'name': item.name, 'value': str(item.value)} for item in enum_class] return resolver diff --git a/backend/dataall/modules/maintenance/api/queries.py b/backend/dataall/modules/maintenance/api/queries.py index 097a0cd0d..57a5f3dcc 100644 --- a/backend/dataall/modules/maintenance/api/queries.py +++ b/backend/dataall/modules/maintenance/api/queries.py @@ -1,8 +1,6 @@ """The module defines GraphQL queries for the Maintenance Activity>""" from dataall.base.api import gql -from dataall.base.utils.enum_utils import generate_enum_query -from dataall.modules.maintenance.api.enums import MaintenanceModes, MaintenanceStatus from dataall.modules.maintenance.api.resolvers import get_maintenance_window_status @@ -10,6 +8,3 @@ name='getMaintenanceWindowStatus', type=gql.Ref('Maintenance'), resolver=get_maintenance_window_status ) -queryMaintenanceModes = generate_enum_query(MaintenanceModes, 'Maintenance') - -queryMaintenanceStatus = generate_enum_query(MaintenanceStatus, 'Maintenance') From fbcb9472001f0088f4ff1d2096ba57f7f086c9f1 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Mon, 29 Jul 2024 16:09:13 +0100 Subject: [PATCH 4/8] make ruff happy --- backend/dataall/base/api/__init__.py | 1 - backend/dataall/modules/maintenance/api/queries.py | 1 - 2 files changed, 2 deletions(-) diff --git a/backend/dataall/base/api/__init__.py b/backend/dataall/base/api/__init__.py index 42a7fe07f..62cc9131c 100644 --- a/backend/dataall/base/api/__init__.py +++ b/backend/dataall/base/api/__init__.py @@ -33,7 +33,6 @@ def bootstrap(): enumclass.toGraphQLEnum() generate_enum_query(enumclass, 'Enums') - for cls in classes.keys(): if not cls.class_instances: # if there are no instances of cls registered in the app continue diff --git a/backend/dataall/modules/maintenance/api/queries.py b/backend/dataall/modules/maintenance/api/queries.py index 57a5f3dcc..11c623cf0 100644 --- a/backend/dataall/modules/maintenance/api/queries.py +++ b/backend/dataall/modules/maintenance/api/queries.py @@ -7,4 +7,3 @@ getMaintenanceWindowStatus = gql.QueryField( name='getMaintenanceWindowStatus', type=gql.Ref('Maintenance'), resolver=get_maintenance_window_status ) - From f51ae8fa89686f5a52f685b9a29fde633b799f72 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Tue, 30 Jul 2024 11:13:54 +0100 Subject: [PATCH 5/8] one query to rule them all --- backend/dataall/base/api/__init__.py | 2 +- backend/dataall/base/utils/enum_utils.py | 36 +++++++++++++------ .../components/MaintenanceViewer.js | 4 +-- .../services/graphql/Shared/getEnumByName.js | 21 ++++++----- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/backend/dataall/base/api/__init__.py b/backend/dataall/base/api/__init__.py index 62cc9131c..3741a72cf 100644 --- a/backend/dataall/base/api/__init__.py +++ b/backend/dataall/base/api/__init__.py @@ -31,7 +31,7 @@ def bootstrap(): for enumclass in GraphQLEnumMapper.__subclasses__(): enumclass.toGraphQLEnum() - generate_enum_query(enumclass, 'Enums') + generate_enum_query() for cls in classes.keys(): if not cls.class_instances: # if there are no instances of cls registered in the app diff --git a/backend/dataall/base/utils/enum_utils.py b/backend/dataall/base/utils/enum_utils.py index 51ae9ff4c..a1092241d 100644 --- a/backend/dataall/base/utils/enum_utils.py +++ b/backend/dataall/base/utils/enum_utils.py @@ -1,11 +1,18 @@ from dataall.base.api import gql +from dataall.base.api.constants import GraphQLEnumMapper -def generate_enum_resolver(enum_class): - def resolver(context, source): - return [{'name': item.name, 'value': str(item.value)} for item in enum_class] - - return resolver +def enum_resolver(context, source, enums_names): + result = [] + for enum_class in GraphQLEnumMapper.__subclasses__(): + if enum_class.__name__ in enums_names: + result.append( + { + 'name': enum_class.__name__, + 'items': [{'name': item.name, 'value': str(item.value)} for item in enum_class], + } + ) + return result EnumItem = gql.ObjectType( @@ -16,11 +23,20 @@ def resolver(context, source): ], ) +EnumResult = gql.ObjectType( + name='EnumResult', + fields=[ + gql.Field(name='name', type=gql.String), + gql.Field(name='items', type=gql.ArrayType(EnumItem)), + ], +) + -def generate_enum_query(enum_class, test_scope): +def generate_enum_query(): return gql.QueryField( - name=enum_class.__name__, - type=gql.ArrayType(EnumItem), - resolver=generate_enum_resolver(enum_class), - test_scope=test_scope, + name='queryEnum', + args=[gql.Argument(name='enums_names', type=gql.ArrayType(gql.String))], + type=gql.ArrayType(EnumResult), + resolver=enum_resolver, + test_scope='Enums', ) diff --git a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js index 83426de4e..20a94907d 100644 --- a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js +++ b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js @@ -27,7 +27,7 @@ import { startMaintenanceWindow, startReindexCatalog } from '../services'; -import { useClient, fetchEnum } from 'services'; +import { useClient, fetchOneEnum } from 'services'; import { SET_ERROR, useDispatch } from 'globalErrors'; import { useSnackbar } from 'notistack'; import { ModuleNames, isModuleEnabled } from 'utils'; @@ -316,7 +316,7 @@ export const MaintenanceViewer = () => { const dispatch = useDispatch(); const fetchMaintenanceModes = async () => { - const maintenanceModesEnum = await fetchEnum(client, 'MaintenanceModes'); + const maintenanceModesEnum = await fetchOneEnum(client, 'MaintenanceModes'); if (maintenanceModesEnum.length > 0) { setMaintenanceModes( maintenanceModesEnum.map((elem) => { diff --git a/frontend/src/services/graphql/Shared/getEnumByName.js b/frontend/src/services/graphql/Shared/getEnumByName.js index cd0c60691..49005918f 100644 --- a/frontend/src/services/graphql/Shared/getEnumByName.js +++ b/frontend/src/services/graphql/Shared/getEnumByName.js @@ -1,23 +1,28 @@ import { gql } from 'apollo-boost'; -const getEnumByName = ({ enum_name }) => ({ +const getEnumByName = ({ enum_names }) => ({ variables: { - enum_name + enum_names: enum_names }, query: gql` - query ${enum_name}{ - ${enum_name}{ + query queryEnum($enum_names: [String]) { + queryEnum(enums_names: $enum_names) { + name + items { name value + } } } ` }); -export const fetchEnum = async (client, enum_name) => { - const response = await client.query(getEnumByName({ enum_name: enum_name })); - if (!response.errors && response.data[enum_name] != null) { - return response.data[enum_name]; +export const fetchOneEnum = async (client, enum_name) => { + const response = await client.query( + getEnumByName({ enum_names: [enum_name] }) + ); + if (!response.errors && response.data.queryEnum != null) { + return response.data.queryEnum[0].items; } else { return []; } From ac03c01cb6e079f3408c06004219f165926c3d61 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Tue, 30 Jul 2024 11:17:36 +0100 Subject: [PATCH 6/8] multiple enums in one request --- .../src/services/graphql/Shared/getEnumByName.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/frontend/src/services/graphql/Shared/getEnumByName.js b/frontend/src/services/graphql/Shared/getEnumByName.js index 49005918f..37e51fda2 100644 --- a/frontend/src/services/graphql/Shared/getEnumByName.js +++ b/frontend/src/services/graphql/Shared/getEnumByName.js @@ -27,3 +27,18 @@ export const fetchOneEnum = async (client, enum_name) => { return []; } }; + +export const fetchEnums = async (client, enum_names) => { + const response = await client.query( + getEnumByName({ enum_names: enum_names }) + ); + let result = {}; + if (!response.errors && response.data.queryEnum != null) { + for (let i = 0; i < response.data.queryEnum.length; i++) { + result[response.data.queryEnum[i].name] = + response.data.queryEnum[i].items; + } + } else { + return result; + } +}; From 6937f9720e576669a80f250674ad5feabd0f8544 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Tue, 30 Jul 2024 14:13:18 +0100 Subject: [PATCH 7/8] PR changes --- backend/dataall/base/api/__init__.py | 3 +- backend/dataall/base/api/queries.py | 11 +++++ backend/dataall/base/api/resolvers.py | 14 +++++++ backend/dataall/base/api/types.py | 17 ++++++++ backend/dataall/base/utils/enum_utils.py | 42 ------------------- .../components/MaintenanceViewer.js | 8 ++-- .../services/graphql/Shared/getEnumByName.js | 29 ++++--------- 7 files changed, 55 insertions(+), 69 deletions(-) create mode 100644 backend/dataall/base/api/queries.py create mode 100644 backend/dataall/base/api/resolvers.py create mode 100644 backend/dataall/base/api/types.py delete mode 100644 backend/dataall/base/utils/enum_utils.py diff --git a/backend/dataall/base/api/__init__.py b/backend/dataall/base/api/__init__.py index 3741a72cf..36f7a275a 100644 --- a/backend/dataall/base/api/__init__.py +++ b/backend/dataall/base/api/__init__.py @@ -12,7 +12,7 @@ from dataall.base.api import gql from dataall.base.api.constants import GraphQLEnumMapper -from dataall.base.utils.enum_utils import generate_enum_query +from dataall.base.api.queries import enumsQuery def bootstrap(): @@ -31,7 +31,6 @@ def bootstrap(): for enumclass in GraphQLEnumMapper.__subclasses__(): enumclass.toGraphQLEnum() - generate_enum_query() for cls in classes.keys(): if not cls.class_instances: # if there are no instances of cls registered in the app diff --git a/backend/dataall/base/api/queries.py b/backend/dataall/base/api/queries.py new file mode 100644 index 000000000..6401fff0b --- /dev/null +++ b/backend/dataall/base/api/queries.py @@ -0,0 +1,11 @@ +from dataall.base.api import gql +from dataall.base.api.resolvers import enum_resolver +from dataall.base.api.types import EnumResult + +enumsQuery = gql.QueryField( + name='queryEnums', + args=[gql.Argument(name='enums_names', type=gql.ArrayType(gql.String))], + type=gql.ArrayType(EnumResult), + resolver=enum_resolver, + test_scope='Enums', +) diff --git a/backend/dataall/base/api/resolvers.py b/backend/dataall/base/api/resolvers.py new file mode 100644 index 000000000..e59165d46 --- /dev/null +++ b/backend/dataall/base/api/resolvers.py @@ -0,0 +1,14 @@ +from dataall.base.api.constants import GraphQLEnumMapper + + +def enum_resolver(context, source, enums_names): + result = [] + for enum_class in GraphQLEnumMapper.__subclasses__(): + if enum_class.__name__ in enums_names: + result.append( + { + 'name': enum_class.__name__, + 'items': [{'name': item.name, 'value': str(item.value)} for item in enum_class], + } + ) + return result diff --git a/backend/dataall/base/api/types.py b/backend/dataall/base/api/types.py new file mode 100644 index 000000000..8d0236d64 --- /dev/null +++ b/backend/dataall/base/api/types.py @@ -0,0 +1,17 @@ +from dataall.base.api import gql + +EnumItem = gql.ObjectType( + name='EnumItem', + fields=[ + gql.Field(name='name', type=gql.String), + gql.Field(name='value', type=gql.String), + ], +) + +EnumResult = gql.ObjectType( + name='EnumResult', + fields=[ + gql.Field(name='name', type=gql.String), + gql.Field(name='items', type=gql.ArrayType(EnumItem)), + ], +) diff --git a/backend/dataall/base/utils/enum_utils.py b/backend/dataall/base/utils/enum_utils.py deleted file mode 100644 index a1092241d..000000000 --- a/backend/dataall/base/utils/enum_utils.py +++ /dev/null @@ -1,42 +0,0 @@ -from dataall.base.api import gql -from dataall.base.api.constants import GraphQLEnumMapper - - -def enum_resolver(context, source, enums_names): - result = [] - for enum_class in GraphQLEnumMapper.__subclasses__(): - if enum_class.__name__ in enums_names: - result.append( - { - 'name': enum_class.__name__, - 'items': [{'name': item.name, 'value': str(item.value)} for item in enum_class], - } - ) - return result - - -EnumItem = gql.ObjectType( - name='EnumItem', - fields=[ - gql.Field(name='name', type=gql.String), - gql.Field(name='value', type=gql.String), - ], -) - -EnumResult = gql.ObjectType( - name='EnumResult', - fields=[ - gql.Field(name='name', type=gql.String), - gql.Field(name='items', type=gql.ArrayType(EnumItem)), - ], -) - - -def generate_enum_query(): - return gql.QueryField( - name='queryEnum', - args=[gql.Argument(name='enums_names', type=gql.ArrayType(gql.String))], - type=gql.ArrayType(EnumResult), - resolver=enum_resolver, - test_scope='Enums', - ) diff --git a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js index 20a94907d..a4048dba9 100644 --- a/frontend/src/modules/Maintenance/components/MaintenanceViewer.js +++ b/frontend/src/modules/Maintenance/components/MaintenanceViewer.js @@ -27,7 +27,7 @@ import { startMaintenanceWindow, startReindexCatalog } from '../services'; -import { useClient, fetchOneEnum } from 'services'; +import { useClient, fetchEnums } from 'services'; import { SET_ERROR, useDispatch } from 'globalErrors'; import { useSnackbar } from 'notistack'; import { ModuleNames, isModuleEnabled } from 'utils'; @@ -316,10 +316,10 @@ export const MaintenanceViewer = () => { const dispatch = useDispatch(); const fetchMaintenanceModes = async () => { - const maintenanceModesEnum = await fetchOneEnum(client, 'MaintenanceModes'); - if (maintenanceModesEnum.length > 0) { + const maintenanceModesEnum = await fetchEnums(client, ['MaintenanceModes']); + if (maintenanceModesEnum['MaintenanceModes'].length > 0) { setMaintenanceModes( - maintenanceModesEnum.map((elem) => { + maintenanceModesEnum['MaintenanceModes'].map((elem) => { return { label: elem.value, value: elem.name }; }) ); diff --git a/frontend/src/services/graphql/Shared/getEnumByName.js b/frontend/src/services/graphql/Shared/getEnumByName.js index 37e51fda2..506202a0d 100644 --- a/frontend/src/services/graphql/Shared/getEnumByName.js +++ b/frontend/src/services/graphql/Shared/getEnumByName.js @@ -5,8 +5,8 @@ const getEnumByName = ({ enum_names }) => ({ enum_names: enum_names }, query: gql` - query queryEnum($enum_names: [String]) { - queryEnum(enums_names: $enum_names) { + query queryEnums($enum_names: [String]) { + queryEnums(enums_names: $enum_names) { name items { name @@ -17,28 +17,15 @@ const getEnumByName = ({ enum_names }) => ({ ` }); -export const fetchOneEnum = async (client, enum_name) => { - const response = await client.query( - getEnumByName({ enum_names: [enum_name] }) - ); - if (!response.errors && response.data.queryEnum != null) { - return response.data.queryEnum[0].items; - } else { - return []; - } -}; - export const fetchEnums = async (client, enum_names) => { const response = await client.query( getEnumByName({ enum_names: enum_names }) ); - let result = {}; - if (!response.errors && response.data.queryEnum != null) { - for (let i = 0; i < response.data.queryEnum.length; i++) { - result[response.data.queryEnum[i].name] = - response.data.queryEnum[i].items; - } - } else { - return result; + if (!response.errors && response.data.queryEnums != null) { + return Object.assign( + {}, + ...response.data.queryEnums.map((x) => ({ [x.name]: x.items })) + ); } + return {}; }; From 1c14949fbbdefe62754f8f4fd5f63fa2efc60a72 Mon Sep 17 00:00:00 2001 From: Sofia Sazonova Date: Tue, 30 Jul 2024 14:16:46 +0100 Subject: [PATCH 8/8] comments --- frontend/src/services/graphql/Shared/getEnumByName.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/frontend/src/services/graphql/Shared/getEnumByName.js b/frontend/src/services/graphql/Shared/getEnumByName.js index 506202a0d..a24742df4 100644 --- a/frontend/src/services/graphql/Shared/getEnumByName.js +++ b/frontend/src/services/graphql/Shared/getEnumByName.js @@ -17,6 +17,11 @@ const getEnumByName = ({ enum_names }) => ({ ` }); +/// function to fetch multiple enums +/// output -- dictionary +// { +// 'enumName': [{name: '...', value: '..'}] +// } export const fetchEnums = async (client, enum_names) => { const response = await client.query( getEnumByName({ enum_names: enum_names })