Skip to content

Commit

Permalink
fix: only publish non-null list + list members (#452)
Browse files Browse the repository at this point in the history
* fix: only publish non-null list + list members

closes #437

* fix comment

* refactor: utility function

* feedback: fix for model side

* feedback: better comment
  • Loading branch information
Jason Kuhrt authored Oct 8, 2019
1 parent ba368dc commit 63277cc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 22 additions & 4 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ interface FieldPublisherConfig {
type FieldPublisher = (opts?: FieldPublisherConfig) => PublisherMethods // Fluent API
type PublisherMethods = Record<string, FieldPublisher>

/**
* 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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -309,8 +328,7 @@ export class SchemaBuilder {
const type = resolvedConfig.type || field.outputType.type
const fieldOpts: Nexus.core.NexusOutputFieldConfig<any, string> = {
type: this.publisher.outputType(type, field),
list: field.outputType.isList || undefined,
nullable: !field.outputType.isRequired,
...dmmfListFieldTypeToNexus(field.outputType),
args: this.buildArgsFromField(
typeName,
null,
Expand Down
8 changes: 4 additions & 4 deletions tests/__app/generated/nexus-typegen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!]!
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/__app/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -89,7 +89,7 @@ enum OrderByArg {
}

type Post {
authors: [User!]
authors: [User!]!
status: PostStatus!
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions tests/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -92,7 +92,7 @@ enum OrderByArg {
}

type Post {
authors: [User!]
authors: [User!]!
status: PostStatus!
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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!]!
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/enums.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 63277cc

Please sign in to comment.