Skip to content

Commit ae0ddc4

Browse files
committed
Use valueFromASTUntyped and handle default vs defaultValue for arg
1 parent f814ec0 commit ae0ddc4

2 files changed

Lines changed: 15 additions & 29 deletions

File tree

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Kind, type GraphQLArgument, type GraphQLInputField, type ValueNode } from 'graphql';
1+
import {
2+
valueFromASTUntyped,
3+
type GraphQLArgument,
4+
type GraphQLInputField,
5+
type ValueNode,
6+
} from 'graphql';
27
import { astFromValue } from './astFromValue.js';
38
import { astFromValueUntyped } from './astFromValueUntyped.js';
49

@@ -23,7 +28,7 @@ export const defaultValueAstFromType = (
2328
return (astFromValueUntyped((arg.default as any).value) as any) ?? undefined;
2429
}
2530

26-
const value = convertConstValueNode((arg.default as any).literal);
31+
const value = valueFromASTUntyped((arg.default as any).literal);
2732
return (astFromValue(value, arg.type) as any) ?? undefined;
2833
}
2934

@@ -32,26 +37,3 @@ export const defaultValueAstFromType = (
3237
? ((astFromValue((arg as any).defaultValue, (arg as any).type) as any) ?? undefined)
3338
: undefined;
3439
};
35-
36-
/**
37-
* `convertConstValueNode` exhaustively traverses an literal node (a node with constant value)
38-
* and constructs a JavaScript representation of the node values
39-
*
40-
* Note: `node` is supposed to be `ConstValueNode` for graphql@17 but
41-
* it is not available in graphql@15 so we cannot import it from `graphql`
42-
*/
43-
const convertConstValueNode = (node: any) => {
44-
if (node.kind === Kind.NULL) {
45-
return null;
46-
} else if (node.kind === Kind.LIST) {
47-
return node.values.map(convertConstValueNode);
48-
} else if (node.kind === Kind.OBJECT) {
49-
const result = {};
50-
for (const field of node.fields) {
51-
result[field.name.value] = convertConstValueNode(field.value);
52-
}
53-
return result;
54-
}
55-
56-
return node.value;
57-
};

packages/utils/src/getArgumentValues.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
Kind,
1010
print,
1111
valueFromAST,
12+
valueFromASTUntyped,
1213
} from 'graphql';
13-
import { defaultValueAstFromType } from './defaultValueAstFromType.js';
1414
import { createGraphQLError } from './errors.js';
1515
import { hasOwnProperty } from './jsutils.js';
1616

@@ -42,9 +42,13 @@ export function getArgumentValues(
4242
const argumentNode = argNodeMap[arg.name];
4343

4444
if (!argumentNode) {
45-
const defaultValue = defaultValueAstFromType(arg) as any;
46-
if (defaultValue) {
47-
coercedValues[arg.name] = defaultValue.value;
45+
if ('default' in (arg as any) && arg.default) {
46+
// graphql v17
47+
coercedValues[arg.name] =
48+
'value' in arg.default ? arg.default.value : valueFromASTUntyped(arg.default.literal);
49+
} else if ('defaultValue' in (arg as any)) {
50+
// graphql < v17
51+
coercedValues[arg.name] = arg.defaultValue;
4852
} else if (isNonNullType(arg.type)) {
4953
throw createGraphQLError(
5054
`Argument "${arg.name}" of required type "${inspect(arg.type)}" ` + 'was not provided.',

0 commit comments

Comments
 (0)