-
Notifications
You must be signed in to change notification settings - Fork 275
/
enumType.ts
71 lines (66 loc) · 2.35 KB
/
enumType.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { assertValidName, GraphQLEnumTypeConfig, GraphQLEnumValueConfig } from 'graphql'
import { arg, NexusArgDef, NexusAsArgConfig } from './args'
import { NexusTypes, SourceTypingDef, withNexusSymbol } from './_types'
type TypeScriptEnumLike = {
[key: number]: string
}
export interface EnumMemberInfo {
/** The external "value" of the enum as displayed in the SDL */
name: string
/** The internal representation of the enum */
value?: string | number | object | boolean
/** The description to annotate the GraphQL SDL */
description?: string
/**
* Info about a field deprecation. Formatted as a string and provided with the deprecated directive on
* field/enum types and as a comment on input fields.
*/
deprecation?: string // | DeprecationInfo;
/**
* Custom extensions, as supported in graphql-js
*
* @see https://github.com/graphql/graphql-js/issues/1527
*/
extensions?: GraphQLEnumValueConfig['extensions']
}
export interface NexusEnumTypeConfig<TypeName extends string> {
name: TypeName
/** The description to annotate the GraphQL SDL */
description?: string | null
/** Source type information for this type */
sourceType?: SourceTypingDef
/** All members of the enum, either as an array of strings/definition objects, as an object, or as a TypeScript enum */
members:
| ReadonlyArray<string | EnumMemberInfo>
| Record<string, string | number | object | boolean>
| TypeScriptEnumLike
/**
* Custom extensions, as supported in graphql-js
*
* @see https://github.com/graphql/graphql-js/issues/1527
*/
extensions?: GraphQLEnumTypeConfig['extensions']
}
export class NexusEnumTypeDef<TypeName extends string> {
constructor(readonly name: TypeName, protected config: NexusEnumTypeConfig<string>) {
assertValidName(name)
}
get value() {
return this.config
}
/**
* Wraps the current enum as an argument, useful if you're defining the enumType inline for an individual field.
*
* @example
* args: {
* sort: enumType(config).asArg({ default: 'someValue' })
* }
*/
asArg(cfg?: NexusAsArgConfig<TypeName>): NexusArgDef<any> {
return arg({ ...cfg, type: this })
}
}
withNexusSymbol(NexusEnumTypeDef, NexusTypes.Enum)
export function enumType<TypeName extends string>(config: NexusEnumTypeConfig<TypeName>) {
return new NexusEnumTypeDef(config.name, config)
}