-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper to derive fragment types (#6)
- Loading branch information
1 parent
7addeb3
commit 6b4c014
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { assertType, test } from 'vitest'; | ||
import { Introspection } from '../introspection'; | ||
import { Document } from '../parser'; | ||
import { FragmentType } from '../typed-document/fragments'; | ||
import { schema } from './introspection.test-d'; | ||
|
||
type Intro = Introspection<typeof schema>; | ||
const any = {} as any; | ||
|
||
test('creates a type for a given fragment', () => { | ||
const unionQuery = ` | ||
fragment Fields on Todo { | ||
id | ||
text | ||
complete | ||
__typename | ||
} | ||
`; | ||
|
||
type doc = Document<typeof unionQuery>; | ||
type typedDoc = FragmentType<doc, Intro>; | ||
|
||
const actual = any as typedDoc; | ||
|
||
assertType<{ | ||
id: string | number; | ||
text: string; | ||
complete: boolean | null; | ||
__typename: 'Todo'; | ||
}>(actual); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { FragmentDefinitionNode, Kind } from '@0no-co/graphql.web'; | ||
import type { Selection, ObjectLikeType, FragmentMap } from './index'; | ||
import type { Introspection as IntrospectionType } from '../introspection'; | ||
|
||
export type FragmentType< | ||
Document extends { kind: Kind.DOCUMENT; definitions: any[] }, | ||
Introspection extends IntrospectionType<any>, | ||
Fragments extends Record<string, FragmentDefinitionNode> = FragmentMap<Document> | ||
> = Document['definitions'][0] extends { | ||
kind: Kind.FRAGMENT_DEFINITION; | ||
typeCondition: { name: { value: infer TypeName } }; | ||
} | ||
? TypeName extends keyof Introspection['types'] | ||
? Introspection['types'][TypeName] extends ObjectLikeType | ||
? Selection< | ||
Document['definitions'][0]['selectionSet']['selections'], | ||
Introspection['types'][TypeName], | ||
Introspection, | ||
Fragments | ||
> | ||
: never | ||
: never | ||
: never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters