Skip to content

Commit c149f35

Browse files
authored
fix: Strip the unmask directive (#354)
1 parent 1ef3b51 commit c149f35

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

.changeset/fluffy-rabbits-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@0no-co/graphqlsp': patch
3+
---
4+
5+
Strip our internal `@_unmask` directive from fragment-definitions when creating hashes for persisted-operations

packages/graphqlsp/src/persisted.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { resolveTemplate } from './ast/resolve';
1313
import {
1414
FragmentDefinitionNode,
15+
Kind,
1516
parse,
1617
print,
1718
visit,
@@ -183,8 +184,22 @@ export const generateHashForDocument = (
183184
foundFilename,
184185
info
185186
).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+
186198
const deduplicatedFragments = fragments
187-
.map(fragment => print(fragment))
199+
.map(fragment => {
200+
stripUnmaskDirectivesFromDefinition(fragment);
201+
return print(fragment)
202+
})
188203
.filter((fragment, index, array) => array.indexOf(fragment) === index);
189204

190205
deduplicatedFragments.forEach(fragmentDefinition => {
@@ -203,6 +218,16 @@ export const generateHashForDocument = (
203218
).combinedText;
204219

205220
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+
206231
const spreads = new Set<string>();
207232
visit(parsed, {
208233
FragmentSpread: node => {
@@ -227,6 +252,8 @@ export const generateHashForDocument = (
227252
return;
228253
}
229254

255+
stripUnmaskDirectivesFromDefinition(fragmentDefinition);
256+
230257
visit(fragmentDefinition, {
231258
FragmentSpread: node => {
232259
if (!visited.has(node.name.value))
@@ -322,3 +349,14 @@ export const getDocumentReferenceFromDocumentNode = (
322349
return { node: documentNodeArgument, filename };
323350
}
324351
};
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+
};

0 commit comments

Comments
 (0)