diff --git a/package.json b/package.json index 535826fbf..8aac4afb5 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "clean": "rm -rf dist", "build": "yarn -s clean && tsc", "dev": "tsc --watch", + "dev:test": "jest --watch", "prepublishOnly": "yarn -s build", "style": "prettier --write 'src/**/*.ts' 'test/**/*.ts' 'example/**/*.ts'", "test": "jest" diff --git a/src/builder.ts b/src/builder.ts index 54785089f..f24cec334 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -25,6 +25,26 @@ interface FieldPublisherConfig { type FieldPublisher = (opts?: FieldPublisherConfig) => PublisherMethods // Fluent API type PublisherMethods = Record +/** + * When dealing with list types we rely on the list type zero value (empty-list) + * to represet the idea of null. + * + * For Photon's part, it will never return null for list type fields nor will it + * ever return null value list members. + */ +const dmmfListFieldTypeToNexus = ( + fieldType: DMMF.Data.SchemaField['outputType'], +) => { + return fieldType.isList + ? { + list: [true], + nullable: false, + } + : { + nullable: !fieldType.isRequired, + } +} + export interface Options { types: any photon?: (ctx: Nexus.core.GetGen<'context'>) => any @@ -204,8 +224,7 @@ export class SchemaBuilder { resolvedConfig.type!, mappedField.field, ), - list: mappedField.field.outputType.isList || undefined, - nullable: !mappedField.field.outputType.isRequired, + ...dmmfListFieldTypeToNexus(mappedField.field.outputType), args: this.buildArgsFromField( typeName, mappedField.operation, @@ -309,8 +328,7 @@ export class SchemaBuilder { const type = resolvedConfig.type || field.outputType.type const fieldOpts: Nexus.core.NexusOutputFieldConfig = { type: this.publisher.outputType(type, field), - list: field.outputType.isList || undefined, - nullable: !field.outputType.isRequired, + ...dmmfListFieldTypeToNexus(field.outputType), args: this.buildArgsFromField( typeName, null, diff --git a/tests/__app/generated/nexus-typegen.d.ts b/tests/__app/generated/nexus-typegen.d.ts index 8d62404ba..de581d7ba 100644 --- a/tests/__app/generated/nexus-typegen.d.ts +++ b/tests/__app/generated/nexus-typegen.d.ts @@ -223,24 +223,24 @@ export interface NexusGenFieldTypes { Bubble: { // field return type createdAt: any; // DateTime! id: any; // UUID! - members: NexusGenRootTypes['User'][] | null; // [User!] + members: NexusGenRootTypes['User'][]; // [User!]! } Mutation: { // field return type createOnePost: NexusGenRootTypes['Post']; // Post! updateManyPost: NexusGenRootTypes['BatchPayload']; // BatchPayload! } Post: { // field return type - authors: NexusGenRootTypes['User'][] | null; // [User!] + authors: NexusGenRootTypes['User'][]; // [User!]! status: NexusGenEnums['PostStatus']; // PostStatus! } Query: { // field return type user: NexusGenRootTypes['User'] | null; // User - users: NexusGenRootTypes['User'][] | null; // [User!] + users: NexusGenRootTypes['User'][]; // [User!]! } User: { // field return type firstName: string; // String! id: string; // ID! - posts: NexusGenRootTypes['Post'][] | null; // [Post!] + posts: NexusGenRootTypes['Post'][]; // [Post!]! } } diff --git a/tests/__app/generated/schema.graphql b/tests/__app/generated/schema.graphql index 27a92e832..b992a5957 100644 --- a/tests/__app/generated/schema.graphql +++ b/tests/__app/generated/schema.graphql @@ -9,7 +9,7 @@ type BatchPayload { type Bubble { createdAt: DateTime! id: UUID! - members(first: Int, orderBy: BubbleMembersOrderByInput, where: BubbleMembersWhereInput): [User!] + members(first: Int, orderBy: BubbleMembersOrderByInput, where: BubbleMembersWhereInput): [User!]! } input BubbleCreateOneWithoutBubbleInput { @@ -89,7 +89,7 @@ enum OrderByArg { } type Post { - authors: [User!] + authors: [User!]! status: PostStatus! } @@ -134,7 +134,7 @@ input PostWhereInput { type Query { user(where: UserWhereUniqueInput!): User - users(after: String, before: String, first: Int, last: Int, skip: Int): [User!] + users(after: String, before: String, first: Int, last: Int, skip: Int): [User!]! } input StringFilter { @@ -154,7 +154,7 @@ input StringFilter { type User { firstName: String! id: ID! - posts(after: String, before: String, first: Int, last: Int, orderBy: PostOrderByInput, skip: Int, where: PostWhereInput): [Post!] + posts(after: String, before: String, first: Int, last: Int, orderBy: PostOrderByInput, skip: Int, where: PostWhereInput): [Post!]! } input UserCreateManyWithoutAuthorsInput { diff --git a/tests/__snapshots__/app.test.ts.snap b/tests/__snapshots__/app.test.ts.snap index f730ac2b0..d32cf7c20 100644 --- a/tests/__snapshots__/app.test.ts.snap +++ b/tests/__snapshots__/app.test.ts.snap @@ -12,7 +12,7 @@ type BatchPayload { type Bubble { createdAt: DateTime! id: UUID! - members(first: Int, orderBy: BubbleMembersOrderByInput, where: BubbleMembersWhereInput): [User!] + members(first: Int, orderBy: BubbleMembersOrderByInput, where: BubbleMembersWhereInput): [User!]! } input BubbleCreateOneWithoutBubbleInput { @@ -92,7 +92,7 @@ enum OrderByArg { } type Post { - authors: [User!] + authors: [User!]! status: PostStatus! } @@ -137,7 +137,7 @@ input PostWhereInput { type Query { user(where: UserWhereUniqueInput!): User - users(after: String, before: String, first: Int, last: Int, skip: Int): [User!] + users(after: String, before: String, first: Int, last: Int, skip: Int): [User!]! } input StringFilter { @@ -157,7 +157,7 @@ input StringFilter { type User { firstName: String! id: ID! - posts(after: String, before: String, first: Int, last: Int, orderBy: PostOrderByInput, skip: Int, where: PostWhereInput): [Post!] + posts(after: String, before: String, first: Int, last: Int, orderBy: PostOrderByInput, skip: Int, where: PostWhereInput): [Post!]! } input UserCreateManyWithoutAuthorsInput { @@ -437,24 +437,24 @@ export interface NexusGenFieldTypes { Bubble: { // field return type createdAt: any; // DateTime! id: any; // UUID! - members: NexusGenRootTypes['User'][] | null; // [User!] + members: NexusGenRootTypes['User'][]; // [User!]! } Mutation: { // field return type createOnePost: NexusGenRootTypes['Post']; // Post! updateManyPost: NexusGenRootTypes['BatchPayload']; // BatchPayload! } Post: { // field return type - authors: NexusGenRootTypes['User'][] | null; // [User!] + authors: NexusGenRootTypes['User'][]; // [User!]! status: NexusGenEnums['PostStatus']; // PostStatus! } Query: { // field return type user: NexusGenRootTypes['User'] | null; // User - users: NexusGenRootTypes['User'][] | null; // [User!] + users: NexusGenRootTypes['User'][]; // [User!]! } User: { // field return type firstName: string; // String! id: string; // ID! - posts: NexusGenRootTypes['Post'][] | null; // [Post!] + posts: NexusGenRootTypes['Post'][]; // [Post!]! } } diff --git a/tests/__snapshots__/enums.test.ts.snap b/tests/__snapshots__/enums.test.ts.snap index 94bc2fa8d..449c090db 100644 --- a/tests/__snapshots__/enums.test.ts.snap +++ b/tests/__snapshots__/enums.test.ts.snap @@ -7,7 +7,7 @@ exports[`does not automatically publish input/enum type if already created by us } type Query { - users(where: UserWhereInput, skip: Int, after: String, before: String, first: Int, last: Int): [User!] + users(where: UserWhereInput, skip: Int, after: String, before: String, first: Int, last: Int): [User!]! } type User { @@ -305,7 +305,7 @@ input IntFilter { } type Query { - users(where: UserWhereInput, skip: Int, after: String, before: String, first: Int, last: Int): [User!] + users(where: UserWhereInput, skip: Int, after: String, before: String, first: Int, last: Int): [User!]! } type User {