|
| 1 | +import type { Maybe } from '../jsutils/Maybe'; |
| 2 | + |
| 3 | +import type { |
| 4 | + DocumentNode, |
| 5 | + FieldNode, |
| 6 | + FragmentDefinitionNode, |
| 7 | + SelectionNode, |
| 8 | +} from '../language/ast'; |
| 9 | +import { Kind } from '../language/kinds'; |
| 10 | +import type { ASTVisitor } from '../language/visitor'; |
| 11 | +import { visit } from '../language/visitor'; |
| 12 | + |
| 13 | +import type { GraphQLOutputType } from '../type/definition'; |
| 14 | +import { getNamedType, isNamedType } from '../type/definition'; |
| 15 | +import type { GraphQLSchema } from '../type/schema'; |
| 16 | + |
| 17 | +import { TypeInfo, visitWithTypeInfo } from './TypeInfo'; |
| 18 | + |
| 19 | +export function mergeAST( |
| 20 | + documentAST: DocumentNode, |
| 21 | + schema?: GraphQLSchema | null, |
| 22 | +): DocumentNode { |
| 23 | + // If we're given the schema, we can simplify even further by resolving object |
| 24 | + // types vs unions/interfaces |
| 25 | + const typeInfo = schema ? new TypeInfo(schema) : null; |
| 26 | + |
| 27 | + const fragmentDefinitions: { |
| 28 | + [key: string]: FragmentDefinitionNode | undefined; |
| 29 | + } = Object.create(null); |
| 30 | + |
| 31 | + for (const definition of documentAST.definitions) { |
| 32 | + if (definition.kind === Kind.FRAGMENT_DEFINITION) { |
| 33 | + fragmentDefinitions[definition.name.value] = |
| 34 | + definition; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + const flattenVisitors: ASTVisitor = { |
| 39 | + SelectionSet(node: any) { |
| 40 | + const selectionSetType: Maybe<GraphQLOutputType> = |
| 41 | + typeInfo?.getParentType(); |
| 42 | + let { selections } = node; |
| 43 | + |
| 44 | + selections = selectionSetType |
| 45 | + ? inlineRelevantFragmentSpreads( |
| 46 | + fragmentDefinitions, |
| 47 | + selections, |
| 48 | + selectionSetType, |
| 49 | + ) |
| 50 | + : inlineRelevantFragmentSpreads( |
| 51 | + fragmentDefinitions, |
| 52 | + selections, |
| 53 | + ); |
| 54 | + |
| 55 | + return { |
| 56 | + ...node, |
| 57 | + selections, |
| 58 | + }; |
| 59 | + }, |
| 60 | + FragmentDefinition() { |
| 61 | + return null; |
| 62 | + }, |
| 63 | + }; |
| 64 | + |
| 65 | + const flattenedAST = visit( |
| 66 | + documentAST, |
| 67 | + typeInfo |
| 68 | + ? visitWithTypeInfo(typeInfo, flattenVisitors) |
| 69 | + : flattenVisitors, |
| 70 | + ); |
| 71 | + |
| 72 | + const deduplicateVisitors: ASTVisitor = { |
| 73 | + SelectionSet(node: any) { |
| 74 | + let { selections } = node; |
| 75 | + |
| 76 | + selections = uniqueBy(selections, (selection) => |
| 77 | + selection.alias |
| 78 | + ? selection.alias.value |
| 79 | + : selection.name.value, |
| 80 | + ); |
| 81 | + |
| 82 | + return { |
| 83 | + ...node, |
| 84 | + selections, |
| 85 | + }; |
| 86 | + }, |
| 87 | + FragmentDefinition() { |
| 88 | + return null; |
| 89 | + }, |
| 90 | + }; |
| 91 | + |
| 92 | + return visit(flattenedAST, deduplicateVisitors); |
| 93 | +} |
| 94 | + |
| 95 | +function inlineRelevantFragmentSpreads( |
| 96 | + fragmentDefinitions: { |
| 97 | + [key: string]: FragmentDefinitionNode | undefined; |
| 98 | + }, |
| 99 | + selections: ReadonlyArray<SelectionNode>, |
| 100 | + selectionSetType?: GraphQLOutputType, |
| 101 | +): ReadonlyArray<SelectionNode> { |
| 102 | + // const selectionSetTypeName = selectionSetType |
| 103 | + // ? getNamedType(selectionSetType).name |
| 104 | + // : null; |
| 105 | + |
| 106 | + let selectionSetTypeName = null; |
| 107 | + if (selectionSetType) { |
| 108 | + const typ = getNamedType(selectionSetType); |
| 109 | + if (isNamedType(typ)) { |
| 110 | + selectionSetTypeName = typ.name; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + const outputSelections = []; |
| 115 | + const seenSpreads: Array<string> = []; |
| 116 | + for (let selection of selections) { |
| 117 | + if (selection.kind === 'FragmentSpread') { |
| 118 | + const fragmentName = selection.name.value; |
| 119 | + if ( |
| 120 | + !selection.directives || |
| 121 | + selection.directives.length === 0 |
| 122 | + ) { |
| 123 | + if (seenSpreads.includes(fragmentName)) { |
| 124 | + /* It's a duplicate - skip it! */ |
| 125 | + continue; |
| 126 | + } else { |
| 127 | + seenSpreads.push(fragmentName); |
| 128 | + } |
| 129 | + } |
| 130 | + const fragmentDefinition = |
| 131 | + fragmentDefinitions[selection.name.value]; |
| 132 | + if (fragmentDefinition) { |
| 133 | + const { typeCondition, directives, selectionSet } = |
| 134 | + fragmentDefinition; |
| 135 | + selection = { |
| 136 | + kind: Kind.INLINE_FRAGMENT, |
| 137 | + typeCondition, |
| 138 | + directives, |
| 139 | + selectionSet, |
| 140 | + }; |
| 141 | + } |
| 142 | + } |
| 143 | + if ( |
| 144 | + selection.kind === Kind.INLINE_FRAGMENT && |
| 145 | + // Cannot inline if there are directives |
| 146 | + (!selection.directives || |
| 147 | + selection.directives?.length === 0) |
| 148 | + ) { |
| 149 | + const fragmentTypeName = selection.typeCondition |
| 150 | + ? selection.typeCondition.name.value |
| 151 | + : null; |
| 152 | + if ( |
| 153 | + !fragmentTypeName || |
| 154 | + fragmentTypeName === selectionSetTypeName |
| 155 | + ) { |
| 156 | + outputSelections.push( |
| 157 | + ...inlineRelevantFragmentSpreads( |
| 158 | + fragmentDefinitions, |
| 159 | + selection.selectionSet.selections, |
| 160 | + selectionSetType, |
| 161 | + ), |
| 162 | + ); |
| 163 | + continue; |
| 164 | + } |
| 165 | + } |
| 166 | + outputSelections.push(selection); |
| 167 | + } |
| 168 | + return outputSelections; |
| 169 | +} |
| 170 | + |
| 171 | +function uniqueBy<T>( |
| 172 | + array: ReadonlyArray<SelectionNode>, |
| 173 | + iteratee: (item: FieldNode) => T, |
| 174 | +) { |
| 175 | + const FilteredMap = new Map<T, FieldNode>(); |
| 176 | + const result: Array<SelectionNode> = []; |
| 177 | + for (const item of array) { |
| 178 | + if (item.kind === 'Field') { |
| 179 | + const uniqueValue = iteratee(item); |
| 180 | + const existing = FilteredMap.get(uniqueValue); |
| 181 | + if (item.directives?.length) { |
| 182 | + // Cannot inline fields with directives (yet) |
| 183 | + const itemClone = { ...item }; |
| 184 | + result.push(itemClone); |
| 185 | + } else if ( |
| 186 | + existing?.selectionSet && |
| 187 | + item.selectionSet |
| 188 | + ) { |
| 189 | + // Merge the selection sets |
| 190 | + existing.selectionSet.selections = [ |
| 191 | + ...existing.selectionSet.selections, |
| 192 | + ...item.selectionSet.selections, |
| 193 | + ]; |
| 194 | + } else if (!existing) { |
| 195 | + const itemClone = { ...item }; |
| 196 | + FilteredMap.set(uniqueValue, itemClone); |
| 197 | + result.push(itemClone); |
| 198 | + } |
| 199 | + } else { |
| 200 | + result.push(item); |
| 201 | + } |
| 202 | + } |
| 203 | + return result; |
| 204 | +} |
0 commit comments