UNPKG

1.96 kBPlain TextView Raw
1import type { Values } from '../../lib/prelude.js'
2import type { TSError } from '../../lib/TSError.js'
3import type { Schema } from '../1_Schema/__.js'
4
5declare global {
6 export namespace GraphQLRequestTypes {
7 interface Schemas {}
8 // Use this is for manual internal type testing.
9 // interface SchemasAlwaysEmpty {}
10 }
11}
12
13type SomeSchema = {
14 index: Schema.Index
15 customScalars: Record<string, Schema.Scalar.Scalar>
16 featureOptions: {
17 schemaErrors: boolean
18 }
19}
20
21type ZeroSchema = {
22 index: { name: never }
23 featureOptions: {
24 schemaErrors: false
25 }
26}
27
28export type GlobalRegistry = Record<string, SomeSchema>
29
30export namespace GlobalRegistry {
31 export type Schemas = GraphQLRequestTypes.Schemas
32
33 export type IsEmpty = keyof Schemas extends never ? true : false
34
35 export type SchemaList = IsEmpty extends true ? ZeroSchema : Values<Schemas>
36
37 export type DefaultSchemaName = 'default'
38
39 export type SchemaNames = keyof GraphQLRequestTypes.Schemas extends never
40 ? TSError<'SchemaNames', 'No schemas have been registered. Did you run graphql-request generate?'>
41 : keyof GraphQLRequestTypes.Schemas
42
43 export type HasSchemaErrors<$Schema extends SchemaList> = $Schema['featureOptions']['schemaErrors']
44
45 export type HasSchemaErrorsViaName<$Name extends SchemaNames> =
46 // todo use conditional types?
47 // eslint-disable-next-line
48 // @ts-ignore passes after generation
49 GraphQLRequestTypes.Schemas[$Name]['featureOptions']['schemaErrors']
50
51 // eslint-disable-next-line
52 // @ts-ignore passes after generation
53 export type GetSchemaIndex<$Name extends SchemaNames> = GraphQLRequestTypes.Schemas[$Name]['index']
54
55 // eslint-disable-next-line
56 // @ts-ignore passes after generation
57 export type SchemaIndexDefault = GetSchemaIndex<DefaultSchemaName>
58
59 export type GetSchemaIndexOrDefault<$Name extends SchemaNames | undefined> = $Name extends SchemaNames
60 ? GetSchemaIndex<$Name>
61 : SchemaIndexDefault
62}