@@ -52,6 +52,7 @@ import { Alert } from '@/model/Alert.model'
52
52
import { store } from '@/store/index'
53
53
import { Tokens } from '@/utils/uid'
54
54
import { WorkflowState , WorkflowStateNames } from '@/model/WorkflowState.model'
55
+ import { isBoolean } from 'lodash-es'
55
56
56
57
/** @typedef {import('@apollo/client').ApolloClient } ApolloClient */
57
58
/** @typedef {import('graphql').IntrospectionInputType } IntrospectionInputType */
@@ -911,10 +912,16 @@ async function _mutateError (mutationName, err, response) {
911
912
console . error ( 'mutation response' , response )
912
913
}
913
914
915
+ let detail = ''
916
+ for ( const e of err . cause ?. result ?. errors ?? [ ] ) {
917
+ console . error ( e )
918
+ detail += `${ e . message } \n`
919
+ }
920
+
914
921
// open a user alert
915
922
await store . dispatch (
916
923
'setAlert' ,
917
- new Alert ( err , 'error' , `Command failed: ${ mutationName } - ${ err } ` )
924
+ new Alert ( err , 'error' , `Command failed: ${ mutationName } - ${ err } ` , detail )
918
925
)
919
926
920
927
// format a response
@@ -936,14 +943,10 @@ async function _mutateError (mutationName, err, response) {
936
943
*/
937
944
export async function mutate ( mutation , variables , apolloClient , cylcID ) {
938
945
const mutationStr = constructMutation ( mutation )
939
- let response = null
940
946
// eslint-disable-next-line no-console
941
- console . debug ( [
942
- `mutation(${ mutation . name } )` ,
943
- mutationStr ,
944
- variables
945
- ] )
947
+ console . debug ( mutationStr , variables )
946
948
949
+ let response
947
950
try {
948
951
// call the mutation
949
952
response = await apolloClient . mutate ( {
@@ -961,23 +964,36 @@ export async function mutate (mutation, variables, apolloClient, cylcID) {
961
964
}
962
965
963
966
try {
964
- const { result } = response . data [ mutation . name ]
965
- if ( Array . isArray ( result ) && result . length === 2 ) {
966
- // regular [commandSucceeded, message] format
967
- if ( result [ 0 ] === true ) {
968
- // success
969
- return _mutateSuccess ( result [ 1 ] )
970
- }
971
- // failure (Cylc error, e.g. could not find workflow <x>)
972
- return _mutateError ( mutation . name , result [ 1 ] , response )
973
- }
974
- // command in a different format (e.g. info command)
975
- return _mutateSuccess ( result )
967
+ return handleMutationResponse ( mutation , response )
976
968
} catch ( error ) {
977
969
return _mutateError ( mutation . name , 'invalid response' , response )
978
970
}
979
971
}
980
972
973
+ export function handleMutationResponse ( mutation , response ) {
974
+ const { result } = response . data [ mutation . name ]
975
+ if ( Array . isArray ( result ) ) {
976
+ if ( result . length === 2 && isBoolean ( result [ 0 ] ) ) {
977
+ // Expected response format
978
+ const [ success , msg ] = result
979
+ if ( success ) return _mutateSuccess ( msg )
980
+ // failure (Cylc error, e.g. could not find workflow <x>)
981
+ return _mutateError ( mutation . name , msg , response )
982
+ }
983
+ // Handle possible nested response formats (https://github.com/cylc/cylc-ui/issues/1851):
984
+ const results = result . map (
985
+ ( r ) => r [ mutation . name ] ?. result ?. [ 0 ] ?. response ?? r . response
986
+ )
987
+ for ( const [ success , msg ] of results ) {
988
+ if ( ! success ) return _mutateError ( mutation . name , msg , response )
989
+ }
990
+ return _mutateSuccess ( 'Command(s) submitted' )
991
+ }
992
+ console . warn ( 'Unexpected format for mutation response:' , result )
993
+ // But assume success
994
+ return _mutateSuccess ( result )
995
+ }
996
+
981
997
/**
982
998
* Send a GraphQL query.
983
999
*
0 commit comments