@@ -517,20 +517,12 @@ export function getMutationExtendedDesc (text) {
517
517
* @param {IntrospectionInputType[] } types - Types as returned by introspection query.
518
518
*/
519
519
export function processArguments ( mutation , types ) {
520
- let pointer = null
521
- let multiple = null
522
- let required = null
523
- let cylcObject = null
524
- let cylcType = null
525
520
for ( const arg of mutation . args ) {
526
- pointer = arg . type
527
- multiple = false
528
- required = false
529
- cylcObject = null
530
- cylcType = null
531
- if ( pointer ?. kind === 'NON_NULL' ) {
532
- required = true
533
- }
521
+ let pointer = arg . type
522
+ let multiple = false
523
+ let cylcObject = null
524
+ let cylcType = null
525
+ const required = arg . type ?. kind === 'NON_NULL'
534
526
while ( pointer ) {
535
527
// walk down the nested type tree
536
528
if ( pointer . kind === 'LIST' ) {
@@ -770,10 +762,11 @@ export function argumentSignature (arg) {
770
762
/** Construct a mutation string from a mutation introspection.
771
763
*
772
764
* @param {Mutation } mutation - A mutation as returned by an introspection query.
765
+ * @param {Record<string,any> } variables
773
766
*
774
767
* @returns {string } A mutation string for a client to send to the server.
775
768
*/
776
- export function constructMutation ( mutation ) {
769
+ export function constructMutation ( mutation , variables ) {
777
770
// the scan mutation has no arguments
778
771
if ( ! mutation . args . length ) {
779
772
return dedent `
@@ -788,6 +781,12 @@ export function constructMutation (mutation) {
788
781
const argNames = [ ]
789
782
const argTypes = [ ]
790
783
for ( const arg of mutation . args ) {
784
+ if ( ! arg . _required && variables ?. [ arg . name ] === arg . _default ) {
785
+ // Skip optional arguments that are set to their default value -
786
+ // this helps avoid back-compat issues when we add new args to the schema
787
+ // TODO: remove this workaround when addressing https://github.com/cylc/cylc-ui/issues/1225
788
+ continue
789
+ }
791
790
argNames . push ( `${ arg . name } : $${ arg . name } ` )
792
791
argTypes . push ( `$${ arg . name } : ${ argumentSignature ( arg ) } ` )
793
792
}
@@ -858,30 +857,32 @@ export function getMutationArgsFromTokens (mutation, tokens) {
858
857
const argspec = { }
859
858
let value
860
859
for ( const arg of mutation . args ) {
861
- const alternate = alternateFields [ arg . _cylcType ]
862
- for ( let token in tokens ) {
863
- if ( arg . _cylcObject && [ token , alternate ] . includes ( arg . _cylcObject ) ) {
864
- if ( arg . name === 'cutoff' ) {
865
- // Work around for a field we don't want filled in, see:
866
- // * https://github.com/cylc/cylc-ui/issues/1222
867
- // * https://github.com/cylc/cylc-ui/issues/1225
868
- // TODO: Once #1225 is done the field type can be safely changed in
869
- // the schema without creating a compatibility issue with the UIS.
870
- continue
871
- }
872
- if ( arg . _cylcObject === alternate ) {
873
- token = alternate
874
- }
875
- if ( arg . _cylcType in compoundFields ) {
876
- value = compoundFields [ arg . _cylcType ] ( tokens )
877
- } else {
878
- value = tokens [ token ]
879
- }
880
- if ( arg . _multiple ) {
881
- value = [ value ]
860
+ if ( arg . _cylcObject ) {
861
+ const alternate = alternateFields [ arg . _cylcType ]
862
+ for ( let token in tokens ) {
863
+ if ( [ token , alternate ] . includes ( arg . _cylcObject ) ) {
864
+ if ( arg . name === 'cutoff' ) {
865
+ // Work around for a field we don't want filled in, see:
866
+ // * https://github.com/cylc/cylc-ui/issues/1222
867
+ // * https://github.com/cylc/cylc-ui/issues/1225
868
+ // TODO: Once #1225 is done the field type can be safely changed in
869
+ // the schema without creating a compatibility issue with the UIS.
870
+ continue
871
+ }
872
+ if ( arg . _cylcObject === alternate ) {
873
+ token = alternate
874
+ }
875
+ if ( arg . _cylcType in compoundFields ) {
876
+ value = compoundFields [ arg . _cylcType ] ( tokens )
877
+ } else {
878
+ value = tokens [ token ]
879
+ }
880
+ if ( arg . _multiple ) {
881
+ value = [ value ]
882
+ }
883
+ argspec [ arg . name ] = value
884
+ break
882
885
}
883
- argspec [ arg . name ] = value
884
- break
885
886
}
886
887
}
887
888
if ( ! argspec [ arg . name ] ) {
@@ -940,14 +941,14 @@ async function _mutateError (mutationName, err, response) {
940
941
* Call a mutation.
941
942
*
942
943
* @param {Mutation } mutation
943
- * @param {Object } variables
944
+ * @param {Record<string,any> } variables
944
945
* @param {ApolloClient } apolloClient
945
946
* @param {string= } cylcID
946
947
*
947
948
* @returns {(MutationResponse | Promise<MutationResponse>) } {status, msg}
948
949
*/
949
950
export async function mutate ( mutation , variables , apolloClient , cylcID ) {
950
- const mutationStr = constructMutation ( mutation )
951
+ const mutationStr = constructMutation ( mutation , variables )
951
952
// eslint-disable-next-line no-console
952
953
console . debug ( mutationStr , variables )
953
954
0 commit comments