Skip to content

Commit

Permalink
fix(cli-utils): Strip unmask directive during persisted-ops generation (
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Sep 4, 2024
1 parent 7fb938d commit d6ca923
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-dolls-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/cli-utils": patch
---

Strip our internal `@_unmask` directive from fragment-definitions during persisted-operations generation
19 changes: 17 additions & 2 deletions packages/cli-utils/src/commands/generate-persisted/thread.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts from 'typescript';
import { parse, print } from '@0no-co/graphql.web';
import { Kind, parse, print } from '@0no-co/graphql.web';

import type { FragmentDefinitionNode } from '@0no-co/graphql.web';
import type { GraphQLSPConfig } from '@gql.tada/internal';
Expand Down Expand Up @@ -156,7 +156,14 @@ async function* _runPersisted(params: PersistedParams): AsyncIterableIterator<Pe
document = operation;
} else {
try {
document = print(parse(operation));
const parsed = parse(operation);
const seen = new Set<unknown>();
for (const definition of parsed.definitions) {
if (definition.kind === Kind.FRAGMENT_DEFINITION && !seen.has(definition)) {
stripUnmaskDirectivesFromDefinition(definition);
}
}
document = print(parsed);
} catch (_error) {
warnings.push({
message:
Expand All @@ -172,6 +179,7 @@ async function* _runPersisted(params: PersistedParams): AsyncIterableIterator<Pe

// NOTE: Update graphqlsp not to pre-parse fragments, which also swallows errors
for (const fragmentDef of fragmentDefs) {
stripUnmaskDirectivesFromDefinition(fragmentDef);
const printedFragmentDef = print(fragmentDef);
if (!seen.has(printedFragmentDef)) {
document += '\n\n' + print(fragmentDef);
Expand All @@ -196,3 +204,10 @@ async function* _runPersisted(params: PersistedParams): AsyncIterableIterator<Pe
}

export const runPersisted = expose(_runPersisted);
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 d6ca923

Please sign in to comment.