From 81ec5c0f0829931af6ca945bd83648e5a264b115 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sat, 19 Jun 2021 13:36:07 -0400 Subject: [PATCH] fix: Allow meta types to be wrapped with null/list (#935) --- src/definitions/list.ts | 8 +++++++- src/definitions/nonNull.ts | 8 +++++++- src/definitions/nullable.ts | 8 +++++++- tests/definitions/nexusMeta.spec.ts | 24 ++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/definitions/list.ts b/src/definitions/list.ts index c9ab2756..fe9939c4 100644 --- a/src/definitions/list.ts +++ b/src/definitions/list.ts @@ -1,4 +1,5 @@ import { isType } from 'graphql' +import { isNexusMeta } from './nexusMeta' import { AllNamedTypeDefs, isNexusStruct, NexusListableTypes } from './wrapping' import { NexusTypes, withNexusSymbol } from './_types' /** List() */ @@ -13,7 +14,12 @@ export class NexusListDef { constructor(readonly ofNexusType: TypeName) { /* istanbul ignore if */ - if (typeof ofNexusType !== 'string' && !isNexusStruct(ofNexusType) && !isType(ofNexusType)) { + if ( + typeof ofNexusType !== 'string' && + !isNexusStruct(ofNexusType) && + !isNexusMeta(ofNexusType) && + !isType(ofNexusType) + ) { throw new Error('Cannot wrap unknown types in list(). Saw ' + ofNexusType) } } diff --git a/src/definitions/nonNull.ts b/src/definitions/nonNull.ts index 514ac77e..74ee4821 100644 --- a/src/definitions/nonNull.ts +++ b/src/definitions/nonNull.ts @@ -1,4 +1,5 @@ import { isNonNullType, isType } from 'graphql' +import { isNexusMeta } from './nexusMeta' import { isNexusNonNullTypeDef, isNexusNullTypeDef, isNexusStruct, NexusNonNullableTypes } from './wrapping' import { NexusTypes, withNexusSymbol } from './_types' @@ -8,7 +9,12 @@ export class NexusNonNullDef { private _isNexusNonNullDef: boolean = true constructor(readonly ofNexusType: TypeName) { - if (typeof ofNexusType !== 'string' && !isNexusStruct(ofNexusType) && !isType(ofNexusType)) { + if ( + typeof ofNexusType !== 'string' && + !isNexusStruct(ofNexusType) && + !isNexusMeta(ofNexusType) && + !isType(ofNexusType) + ) { throw new Error('Cannot wrap unknown types in a nonNull(). Saw ' + ofNexusType) } } diff --git a/src/definitions/nullable.ts b/src/definitions/nullable.ts index d9e971c1..bb976fd8 100644 --- a/src/definitions/nullable.ts +++ b/src/definitions/nullable.ts @@ -1,4 +1,5 @@ import { isType } from 'graphql' +import { isNexusMeta } from './nexusMeta' import { isNexusNonNullTypeDef, isNexusNullTypeDef, isNexusStruct, NexusNullableTypes } from './wrapping' import { NexusTypes, withNexusSymbol } from './_types' @@ -8,7 +9,12 @@ export class NexusNullDef { private _isNexusNullDef: boolean = true constructor(readonly ofNexusType: TypeName) { - if (typeof ofNexusType !== 'string' && !isNexusStruct(ofNexusType) && !isType(ofNexusType)) { + if ( + typeof ofNexusType !== 'string' && + !isNexusStruct(ofNexusType) && + !isNexusMeta(ofNexusType) && + !isType(ofNexusType) + ) { throw new Error('Cannot wrap unknown types in nullable(). Saw ' + ofNexusType) } } diff --git a/tests/definitions/nexusMeta.spec.ts b/tests/definitions/nexusMeta.spec.ts index ded58a98..5ef1c7fa 100644 --- a/tests/definitions/nexusMeta.spec.ts +++ b/tests/definitions/nexusMeta.spec.ts @@ -1,12 +1,26 @@ import { graphql } from 'graphql' import { makeSchema, objectType, queryField } from '../../src' -import { NEXUS_BUILD, NEXUS_TYPE } from '../../src/core' +import { list, NEXUS_BUILD, NEXUS_TYPE, nonNull, nullable } from '../../src/core' interface UserData { id?: string ok?: boolean } +class Post { + static [NEXUS_TYPE]() { + return objectType({ + name: 'Post', + definition(t) { + t.string('content') + t.field('author', { + type: 'User', + }) + }, + }) + } +} + class User { constructor(readonly data: UserData = {}) {} @@ -28,11 +42,17 @@ class User { definition(t) { t.id('id') t.boolean('ok') - t.field('info', { type: UserInfo }) + t.field('info', { type: nonNull(UserInfo) }) t.field('friend', { type: User, resolve: () => new User({ id: 'User:2', ok: false }), }) + t.field('posts', { + type: list(Post), + }) + t.field('topPost', { + type: nullable(Post), + }) }, }) }