Skip to content

Commit 9a4f0b6

Browse files
committed
Fix
1 parent b685cef commit 9a4f0b6

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/utils/src/build-operation-for-field.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
ArgumentNode,
3+
astFromValue,
4+
ConstValueNode,
35
getNamedType,
46
GraphQLArgument,
57
GraphQLField,
@@ -27,6 +29,7 @@ import {
2729
TypeNode,
2830
VariableDefinitionNode,
2931
} from 'graphql';
32+
import { astFromValueUntyped } from './astFromValueUntyped.js';
3033
import { getDefinedRootType, getRootTypeNames } from './rootTypes.js';
3134

3235
let operationVariables: VariableDefinitionNode[] = [];
@@ -385,6 +388,22 @@ function resolveVariable(arg: GraphQLArgument, name?: string): VariableDefinitio
385388
};
386389
}
387390

391+
let defaultValue: ConstValueNode | undefined;
392+
try {
393+
const returnVal = astFromValue(arg.defaultValue, arg.type);
394+
if (returnVal == null) {
395+
defaultValue = undefined;
396+
} else {
397+
defaultValue = returnVal as ConstValueNode;
398+
}
399+
} catch (e) {
400+
const returnVal = astFromValueUntyped(arg.defaultValue);
401+
if (returnVal == null) {
402+
defaultValue = undefined;
403+
} else {
404+
defaultValue = returnVal as ConstValueNode;
405+
}
406+
}
388407
return {
389408
kind: Kind.VARIABLE_DEFINITION,
390409
variable: {
@@ -395,7 +414,7 @@ function resolveVariable(arg: GraphQLArgument, name?: string): VariableDefinitio
395414
},
396415
},
397416
type: resolveVariableType(arg.type),
398-
defaultValue: (arg.astNode || {}).defaultValue,
417+
defaultValue,
399418
};
400419
}
401420

packages/utils/tests/build-operation-node-for-field.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ test('should work with query with default value arguments', async () => {
697697
name
698698
}
699699
}
700-
`)
700+
`),
701701
);
702702
});
703703

@@ -729,6 +729,6 @@ test('should work with mutation with default value arguments', async () => {
729729
active
730730
}
731731
}
732-
`)
732+
`),
733733
);
734734
});

0 commit comments

Comments
 (0)