diff --git a/src/utilities/replaceVariables.ts b/src/utilities/replaceVariables.ts index e491416ed7..747931fab7 100644 --- a/src/utilities/replaceVariables.ts +++ b/src/utilities/replaceVariables.ts @@ -24,7 +24,7 @@ export function replaceVariables( return visit(valueNode, { Variable(node) { const varName = node.name.value; - const scopedVariableValues = fragmentVariableValues?.signatures[varName] + const scopedVariableValues = fragmentVariableValues?.sources[varName] ? fragmentVariableValues : variableValues; @@ -32,29 +32,39 @@ export function replaceVariables( return { kind: Kind.NULL }; } - if (scopedVariableValues.sources[varName] === undefined) { - const defaultValue = - scopedVariableValues.signatures[varName]?.defaultValue; + const scopedVariableSource = scopedVariableValues.sources[varName]; + if (scopedVariableSource.value === undefined) { + const defaultValue = scopedVariableSource.signature.defaultValue; if (defaultValue !== undefined) { return defaultValue.literal; } } return valueToLiteral( - scopedVariableValues.sources[varName], - scopedVariableValues?.signature.type, + scopedVariableSource.value, + scopedVariableSource.signature.type, ); }, ObjectValue(node) { return { ...node, // Filter out any fields with a missing variable. - fields: node.fields.filter( - (field) => - (field.value.kind !== Kind.VARIABLE || - fragmentVariableValues?.sources[field.value.name.value]) ?? - variableValues?.sources[field.value.name.value], - ), + fields: node.fields.filter((field) => { + if (field.value.kind !== Kind.VARIABLE) { + return true; + } + const scopedVariableSource = + fragmentVariableValues?.sources[field.value.name.value] ?? + variableValues?.sources[field.value.name.value]; + + if ( + scopedVariableSource?.value === undefined && + scopedVariableSource?.signature.defaultValue === undefined + ) { + return false; + } + return true; + }), }; }, }) as ConstValueNode;