Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.

fix: Strip the unmask directive (#354)

authored by

Jovi De Croock and committed by
GitHub
c149f35e 1ef3b51b

+44 -1
+5
.changeset/fluffy-rabbits-shake.md
··· 1 + --- 2 + '@0no-co/graphqlsp': patch 3 + --- 4 + 5 + Strip our internal `@_unmask` directive from fragment-definitions when creating hashes for persisted-operations
+39 -1
packages/graphqlsp/src/persisted.ts
··· 12 12 import { resolveTemplate } from './ast/resolve'; 13 13 import { 14 14 FragmentDefinitionNode, 15 + Kind, 15 16 parse, 16 17 print, 17 18 visit, ··· 183 184 foundFilename, 184 185 info 185 186 ).combinedText; 187 + const parsed = parse(text); 188 + const seen = new Set<unknown>(); 189 + for (const definition of parsed.definitions) { 190 + if ( 191 + definition.kind === Kind.FRAGMENT_DEFINITION && 192 + !seen.has(definition) 193 + ) { 194 + stripUnmaskDirectivesFromDefinition(definition); 195 + } 196 + } 197 + 186 198 const deduplicatedFragments = fragments 187 - .map(fragment => print(fragment)) 199 + .map(fragment => { 200 + stripUnmaskDirectivesFromDefinition(fragment); 201 + return print(fragment) 202 + }) 188 203 .filter((fragment, index, array) => array.indexOf(fragment) === index); 189 204 190 205 deduplicatedFragments.forEach(fragmentDefinition => { ··· 203 218 ).combinedText; 204 219 205 220 const parsed = parse(text); 221 + const seen = new Set<unknown>(); 222 + for (const definition of parsed.definitions) { 223 + if ( 224 + definition.kind === Kind.FRAGMENT_DEFINITION && 225 + !seen.has(definition) 226 + ) { 227 + stripUnmaskDirectivesFromDefinition(definition); 228 + } 229 + } 230 + 206 231 const spreads = new Set<string>(); 207 232 visit(parsed, { 208 233 FragmentSpread: node => { ··· 226 251 ); 227 252 return; 228 253 } 254 + 255 + stripUnmaskDirectivesFromDefinition(fragmentDefinition); 229 256 230 257 visit(fragmentDefinition, { 231 258 FragmentSpread: node => { ··· 322 349 return { node: documentNodeArgument, filename }; 323 350 } 324 351 }; 352 + 353 + type writable<T> = { -readonly [K in keyof T]: T[K] }; 354 + 355 + const stripUnmaskDirectivesFromDefinition = ( 356 + definition: FragmentDefinitionNode 357 + ) => { 358 + (definition as writable<FragmentDefinitionNode>).directives = 359 + definition.directives?.filter( 360 + directive => directive.name.value !== '_unmask' 361 + ); 362 + };