Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli-utils): Strip unmask directive during persisted-ops generation #388

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
@@ -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:
@@ -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);
@@ -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'
);
};