Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Accept ReadonlyArray<T> for enum members #916

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
9 changes: 9 additions & 0 deletions src/utils.ts
Expand Up @@ -600,3 +600,12 @@ export function graphql15InterfaceType<T extends GraphQLInterfaceType>(
}
return type as T & { getInterfaces(): GraphQLInterfaceType[] }
}

/**
* A specially typed version of `Array.isArray` to work around [this issue](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)
}