Skip to content

Commit

Permalink
place on fragmentDefinition instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 19, 2024
1 parent 070ce7b commit edd6fbc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
7 changes: 7 additions & 0 deletions examples/example-pokemon-api/src/components/PokemonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export const PokemonItemFragment = graphql(`
}
`);

export const PokemonItemFragmentUnmasked = graphql(`
fragment PokemonItemNoMask on Pokemon @mask_disable {
id
name
}
`);

interface Props {
data: FragmentOf<typeof PokemonItemFragment> | null;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/example-pokemon-api/src/components/PokemonList.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useQuery } from 'urql';
import { graphql } from '../graphql';

import { PokemonItem, PokemonItemFragment } from './PokemonItem';
import { PokemonItem, PokemonItemFragment, PokemonItemFragmentUnmasked } from './PokemonItem';

const PokemonsQuery = graphql(`
query Pokemons ($limit: Int = 10) {
pokemons(limit: $limit) {
id
...PokemonItem @mask_disable
...PokemonItemNoMask
...PokemonItem
}
}
`, [PokemonItemFragment]);
`, [PokemonItemFragment, PokemonItemFragmentUnmasked]);

const PokemonList = () => {
const [result] = useQuery({ query: PokemonsQuery });
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/namespace.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('decorateFragmentDef', () => {
value: 'Todo';
};
};
directives: unknown;
directives: [];
selectionSet: unknown;
},
];
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('getFragmentsOfDocumentsRec', () => {
value: 'Todo';
};
};
selectionSet: {};
masked: true;
readonly [$tada.fragmentId]: unique symbol;
};

Expand Down
5 changes: 5 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ function initGraphQLTada<const Setup extends AbstractSetupSchema>() {
for (const document of fragments || []) {
for (const definition of document.definitions) {
if (definition.kind === Kind.FRAGMENT_DEFINITION && !seen.has(definition)) {
if (definition.directives && definition.directives.length) {
(definition as any).directives = definition.directives.filter(
(directive) => directive.name !== 'mask_disable'
);
}
definitions.push(definition);
seen.add(definition);
}
Expand Down
15 changes: 14 additions & 1 deletion src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,38 @@ interface FragmentDefDecorationLike {
name: any;
typeCondition: any;
selectionSet: any;
masked: boolean;
}

interface DocumentDefDecorationLike extends DocumentNode {
[$tada.fragmentDef]?: FragmentDefDecorationLike;
}

type isUnmaskedFragmentRec<Directives extends readonly unknown[] | undefined> =
Directives extends readonly [infer Directive, ...infer Rest]
? Directive extends { kind: Kind.DIRECTIVE; name: any }
? Directive['name']['value'] extends 'mask_disable'
? true
: isUnmaskedFragmentRec<Rest>
: isUnmaskedFragmentRec<Rest>
: false;

type decorateFragmentDef<Document extends DocumentNodeLike> = Document['definitions'][0] extends {
kind: Kind.FRAGMENT_DEFINITION;
name: any;
typeCondition: any;
selectionSet: any;
directives: any[];
}
? {
// NOTE: This is a shortened definition for readability in LSP hovers
kind: Kind.FRAGMENT_DEFINITION;
name: Document['definitions'][0]['name'];
typeCondition: Document['definitions'][0]['typeCondition'];
selectionSet: Document['definitions'][0]['selectionSet'];
masked: isUnmaskedFragmentRec<Document['definitions'][0]['directives']> extends true
? false
: true;
readonly [$tada.fragmentId]: unique symbol;
}
: never;
Expand All @@ -58,7 +72,6 @@ type getFragmentsOfDocumentsRec<Documents> = Documents extends readonly [
kind: Kind.FRAGMENT_DEFINITION;
name: any;
typeCondition: any;
selectionSet: any;
}
? { [Name in FragmentDef['name']['value']]: FragmentDef }
: {}
Expand Down
15 changes: 3 additions & 12 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ type isOptionalRec<Directives extends readonly unknown[] | undefined> =
: isOptionalRec<Rest>
: false;

type isUnmaskedFragmentRec<Directives extends readonly unknown[] | undefined> =
Directives extends readonly [infer Directive, ...infer Rest]
? Directive extends { kind: Kind.DIRECTIVE; name: any }
? Directive['name']['value'] extends 'mask_disable'
? true
: isUnmaskedFragmentRec<Rest>
: isUnmaskedFragmentRec<Rest>
: false;

type getFieldAlias<Node extends FieldNode> = Node['alias'] extends undefined
? Node['name']['value']
: Node['alias'] extends NameNode
Expand All @@ -94,14 +85,14 @@ type getFragmentSelection<
: Node extends { kind: Kind.FRAGMENT_SPREAD; name: any; directives: any[] }
? Node['name']['value'] extends keyof Fragments
? Fragments[Node['name']['value']] extends FragmentDefDecorationLike
? isUnmaskedFragmentRec<Node['directives']> extends true
? getSelection<
? Fragments[Node['name']['value']]['masked'] extends true
? makeFragmentRef<Fragments[Node['name']['value']]>
: getSelection<
Fragments[Node['name']['value']]['selectionSet']['selections'],
Type,
Introspection,
Fragments
>
: makeFragmentRef<Fragments[Node['name']['value']]>
: getSelection<
Fragments[Node['name']['value']]['selectionSet']['selections'],
Type,
Expand Down

0 comments on commit edd6fbc

Please sign in to comment.