Skip to content

Commit

Permalink
allow for unmasking a fragment by means of a directive
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 19, 2024
1 parent e714a8f commit a499abf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/example-pokemon-api/src/components/PokemonList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PokemonsQuery = graphql(`
query Pokemons ($limit: Int = 10) {
pokemons(limit: $limit) {
id
...PokemonItem @mask_disable
...PokemonItem
}
}
Expand All @@ -27,7 +28,7 @@ const PokemonList = () => {
} else if (fetching || !data) {
return <h3>Loading...</h3>
}

return (
<div>
{data.pokemons ? (
Expand Down
4 changes: 4 additions & 0 deletions src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface FragmentDefDecorationLike {
kind: Kind.FRAGMENT_DEFINITION;
name: any;
typeCondition: any;
selectionSet: any;
}

interface DocumentDefDecorationLike extends DocumentNode {
Expand All @@ -36,12 +37,14 @@ type decorateFragmentDef<Document extends DocumentNodeLike> = Document['definiti
kind: Kind.FRAGMENT_DEFINITION;
name: any;
typeCondition: any;
selectionSet: 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'];
readonly [$tada.fragmentId]: unique symbol;
}
: never;
Expand All @@ -55,6 +58,7 @@ type getFragmentsOfDocumentsRec<Documents> = Documents extends readonly [
kind: Kind.FRAGMENT_DEFINITION;
name: any;
typeCondition: any;
selectionSet: any;
}
? { [Name in FragmentDef['name']['value']]: FragmentDef }
: {}
Expand Down
23 changes: 20 additions & 3 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import type { DocumentNodeLike } from './parser';

import type {
FragmentDefDecorationLike,
makeUndefinedFragmentRef,
//makeUndefinedFragmentRef,
makeFragmentRef,
makeUndefinedFragmentRef,
} from './namespace';

import type {
Expand Down Expand Up @@ -69,6 +70,15 @@ 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 @@ -82,10 +92,17 @@ type getFragmentSelection<
Fragments extends { [name: string]: any },
> = Node extends { kind: Kind.INLINE_FRAGMENT; selectionSet: any }
? getSelection<Node['selectionSet']['selections'], Type, Introspection, Fragments>
: Node extends { kind: Kind.FRAGMENT_SPREAD; name: any }
: Node extends { kind: Kind.FRAGMENT_SPREAD; name: any; directives: any[] }
? Node['name']['value'] extends keyof Fragments
? Fragments[Node['name']['value']] extends FragmentDefDecorationLike
? makeFragmentRef<Fragments[Node['name']['value']]>
? isUnmaskedFragmentRec<Node['directives']> extends true
? 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 a499abf

Please sign in to comment.