Skip to content

Commit 8e1c778

Browse files
authored
AOTF: Do not send non-required mutation args when they have the default value (#2230)
* Tidy * AOTF: Do not send non-required mutation args when they have the default value This workaround helps avoid back-compat issues when we make changes to the schema
1 parent 4e5569e commit 8e1c778

File tree

2 files changed

+74
-40
lines changed

2 files changed

+74
-40
lines changed

src/utils/aotf.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -517,20 +517,12 @@ export function getMutationExtendedDesc (text) {
517517
* @param {IntrospectionInputType[]} types - Types as returned by introspection query.
518518
*/
519519
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
525520
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'
534526
while (pointer) {
535527
// walk down the nested type tree
536528
if (pointer.kind === 'LIST') {
@@ -770,10 +762,11 @@ export function argumentSignature (arg) {
770762
/** Construct a mutation string from a mutation introspection.
771763
*
772764
* @param {Mutation} mutation - A mutation as returned by an introspection query.
765+
* @param {Record<string,any>} variables
773766
*
774767
* @returns {string} A mutation string for a client to send to the server.
775768
*/
776-
export function constructMutation (mutation) {
769+
export function constructMutation (mutation, variables) {
777770
// the scan mutation has no arguments
778771
if (!mutation.args.length) {
779772
return dedent`
@@ -788,6 +781,12 @@ export function constructMutation (mutation) {
788781
const argNames = []
789782
const argTypes = []
790783
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+
}
791790
argNames.push(`${arg.name}: $${arg.name}`)
792791
argTypes.push(`$${arg.name}: ${argumentSignature(arg)}`)
793792
}
@@ -858,30 +857,32 @@ export function getMutationArgsFromTokens (mutation, tokens) {
858857
const argspec = {}
859858
let value
860859
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
882885
}
883-
argspec[arg.name] = value
884-
break
885886
}
886887
}
887888
if (!argspec[arg.name]) {
@@ -940,14 +941,14 @@ async function _mutateError (mutationName, err, response) {
940941
* Call a mutation.
941942
*
942943
* @param {Mutation} mutation
943-
* @param {Object} variables
944+
* @param {Record<string,any>} variables
944945
* @param {ApolloClient} apolloClient
945946
* @param {string=} cylcID
946947
*
947948
* @returns {(MutationResponse | Promise<MutationResponse>)} {status, msg}
948949
*/
949950
export async function mutate (mutation, variables, apolloClient, cylcID) {
950-
const mutationStr = constructMutation(mutation)
951+
const mutationStr = constructMutation(mutation, variables)
951952
// eslint-disable-next-line no-console
952953
console.debug(mutationStr, variables)
953954

tests/unit/utils/aotf.spec.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,12 @@ describe('aotf (Api On The Fly)', () => {
566566
}
567567
]
568568
}
569-
expect(aotf.constructMutation(mutation)).to.equal(dedent`
569+
aotf.processMutations([mutation])
570+
const variables = {
571+
foo: 'defined',
572+
bar: 'defined', // N.B. type irrelevant for this test
573+
}
574+
expect(aotf.constructMutation(mutation, variables)).to.equal(dedent`
570575
mutation MyMutation($foo: String, $bar: Int) {
571576
MyMutation(foo: $foo, bar: $bar) {
572577
result
@@ -575,6 +580,33 @@ describe('aotf (Api On The Fly)', () => {
575580
`.trim())
576581
})
577582

583+
it("doesn't include non-required args with default value", () => {
584+
const mutation = {
585+
name: 'MyMutation',
586+
args: [
587+
{
588+
name: 'foo',
589+
type: { name: 'String', kind: 'SCALAR' },
590+
defaultValue: 'default',
591+
},
592+
{
593+
name: 'bar',
594+
type: { name: 'Int', kind: 'SCALAR' },
595+
defaultValue: 42,
596+
}
597+
]
598+
}
599+
aotf.processMutations([mutation])
600+
const variables = { foo: 'default', bar: 42 }
601+
expect(aotf.constructMutation(mutation, variables)).to.equal(dedent`
602+
mutation MyMutation() {
603+
MyMutation() {
604+
result
605+
}
606+
}
607+
`.trim())
608+
})
609+
578610
it('handles nested types', () => {
579611
const mutation = {
580612
name: 'MyMutation',
@@ -597,6 +629,7 @@ describe('aotf (Api On The Fly)', () => {
597629
}
598630
]
599631
}
632+
aotf.processMutations([mutation])
600633
expect(aotf.constructMutation(mutation)).to.equal(dedent`
601634
mutation MyMutation($myArg: [String]!) {
602635
MyMutation(myArg: $myArg) {

0 commit comments

Comments
 (0)