Skip to content

Commit

Permalink
fix: Strip the unmask directive (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Sep 4, 2024
1 parent 1ef3b51 commit c149f35
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-rabbits-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': patch
---

Strip our internal `@_unmask` directive from fragment-definitions when creating hashes for persisted-operations
40 changes: 39 additions & 1 deletion packages/graphqlsp/src/persisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { resolveTemplate } from './ast/resolve';
import {
FragmentDefinitionNode,
Kind,
parse,
print,
visit,
Expand Down Expand Up @@ -183,8 +184,22 @@ export const generateHashForDocument = (
foundFilename,
info
).combinedText;
const parsed = parse(text);
const seen = new Set<unknown>();
for (const definition of parsed.definitions) {
if (
definition.kind === Kind.FRAGMENT_DEFINITION &&
!seen.has(definition)
) {
stripUnmaskDirectivesFromDefinition(definition);
}
}

const deduplicatedFragments = fragments
.map(fragment => print(fragment))
.map(fragment => {
stripUnmaskDirectivesFromDefinition(fragment);
return print(fragment)
})
.filter((fragment, index, array) => array.indexOf(fragment) === index);

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

const parsed = parse(text);
const seen = new Set<unknown>();
for (const definition of parsed.definitions) {
if (
definition.kind === Kind.FRAGMENT_DEFINITION &&
!seen.has(definition)
) {
stripUnmaskDirectivesFromDefinition(definition);
}
}

const spreads = new Set<string>();
visit(parsed, {
FragmentSpread: node => {
Expand All @@ -227,6 +252,8 @@ export const generateHashForDocument = (
return;
}

stripUnmaskDirectivesFromDefinition(fragmentDefinition);

visit(fragmentDefinition, {
FragmentSpread: node => {
if (!visited.has(node.name.value))
Expand Down Expand Up @@ -322,3 +349,14 @@ export const getDocumentReferenceFromDocumentNode = (
return { node: documentNodeArgument, filename };
}
};

type writable<T> = { -readonly [K in keyof T]: T[K] };

const stripUnmaskDirectivesFromDefinition = (
definition: FragmentDefinitionNode
) => {
(definition as writable<FragmentDefinitionNode>).directives =
definition.directives?.filter(
directive => directive.name.value !== '_unmask'
);
};

0 comments on commit c149f35

Please sign in to comment.