|
| 1 | +import type { Maybe } from '../jsutils/Maybe.js'; |
| 2 | +import type { ObjMap } from '../jsutils/ObjMap.js'; |
| 3 | + |
| 4 | +import type { |
| 5 | + ArgumentNode, |
| 6 | + FragmentArgumentDefinitionNode, |
| 7 | + FragmentDefinitionNode, |
| 8 | + FragmentSpreadNode, |
| 9 | + SelectionSetNode, |
| 10 | + ValueNode, |
| 11 | +} from '../language/ast.js'; |
| 12 | +import { Kind } from '../language/kinds.js'; |
| 13 | +import { visit } from '../language/visitor.js'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Replaces all fragment argument values with non-fragment-scoped values. |
| 17 | + * |
| 18 | + * NOTE: fragment arguments are scoped to the fragment they're defined on. |
| 19 | + * Therefore, after we apply the passed-in arguments, all remaining variables |
| 20 | + * must be either operation defined variables or explicitly unset. |
| 21 | + */ |
| 22 | +export function substituteFragmentArguments( |
| 23 | + def: FragmentDefinitionNode, |
| 24 | + fragmentSpread: FragmentSpreadNode, |
| 25 | +): SelectionSetNode { |
| 26 | + const argumentDefinitions = def.arguments; |
| 27 | + if (argumentDefinitions == null || argumentDefinitions.length === 0) { |
| 28 | + return def.selectionSet; |
| 29 | + } |
| 30 | + const argumentValues = fragmentArgumentSubstitutions( |
| 31 | + argumentDefinitions, |
| 32 | + fragmentSpread.arguments, |
| 33 | + ); |
| 34 | + return visit(def.selectionSet, { |
| 35 | + Variable(node) { |
| 36 | + return argumentValues[node.name.value]; |
| 37 | + }, |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +export function fragmentArgumentSubstitutions( |
| 42 | + argumentDefinitions: ReadonlyArray<FragmentArgumentDefinitionNode>, |
| 43 | + argumentValues: Maybe<ReadonlyArray<ArgumentNode>>, |
| 44 | +): ObjMap<ValueNode> { |
| 45 | + const substitutions: ObjMap<ValueNode> = {}; |
| 46 | + if (argumentValues) { |
| 47 | + for (const argument of argumentValues) { |
| 48 | + substitutions[argument.name.value] = argument.value; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + for (const argumentDefinition of argumentDefinitions) { |
| 53 | + const argumentName = argumentDefinition.variable.name.value; |
| 54 | + if (substitutions[argumentName]) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + const defaultValue = argumentDefinition.defaultValue; |
| 59 | + if (defaultValue) { |
| 60 | + substitutions[argumentName] = defaultValue; |
| 61 | + } else { |
| 62 | + // We need a way to allow unset arguments without accidentally |
| 63 | + // replacing an unset fragment argument with an operation |
| 64 | + // variable value. Fragment arguments must always have LOCAL scope. |
| 65 | + // |
| 66 | + // To remove this hack, we need to either: |
| 67 | + // - include fragment argument scope when evaluating fields |
| 68 | + // - make unset fragment arguments invalid |
| 69 | + // Requiring the spread to pass all non-default-defined arguments is nice, |
| 70 | + // but makes field argument default values impossible to use. |
| 71 | + substitutions[argumentName] = { |
| 72 | + kind: Kind.VARIABLE, |
| 73 | + name: { kind: Kind.NAME, value: '__UNSET' }, |
| 74 | + }; |
| 75 | + } |
| 76 | + } |
| 77 | + return substitutions; |
| 78 | +} |
0 commit comments