Skip to content

Commit

Permalink
Accept ReadonlyArray<T> for enum members
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed May 24, 2021
1 parent 571fa4d commit 23c77ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/builder.ts
Expand Up @@ -139,6 +139,7 @@ import {
graphql15InterfaceConfig,
graphql15InterfaceType,
invariantGuard,
isArray,
isObject,
mapValues,
objValues,
Expand Down Expand Up @@ -952,7 +953,7 @@ export class SchemaBuilder {
private buildEnumType(config: NexusEnumTypeConfig<any>) {
const { members } = config
const values: GraphQLEnumValueConfigMap = {}
if (Array.isArray(members)) {
if (isArray(members)) {
members.forEach((m) => {
if (typeof m === 'string') {
values[m] = { value: m }
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/enumType.ts
Expand Up @@ -34,7 +34,7 @@ export interface NexusEnumTypeConfig<TypeName extends string> {
sourceType?: SourceTypingDef
/** All members of the enum, either as an array of strings/definition objects, as an object, or as a TypeScript enum */
members:
| Array<string | EnumMemberInfo>
| ReadonlyArray<string | EnumMemberInfo>
| Record<string, string | number | object | boolean>
| TypeScriptEnumLike
/**
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Expand Up @@ -600,3 +600,11 @@ export function graphql15InterfaceType<T extends GraphQLInterfaceType>(
}
return type as T & { getInterfaces(): GraphQLInterfaceType[] }
}

// A function that is correctly typed to get arround a TypeScript issue.
// See https://github.com/microsoft/TypeScript/issues/17002
export function isArray<T>(
arg: T | {}
): arg is T extends readonly any[] ? (unknown extends T ? never : readonly any[]) : any[] {
return Array.isArray(arg)
}

0 comments on commit 23c77ab

Please sign in to comment.