-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts): Improves return types of QueryResolvers, MutationResolvers …
…and <Model>Resolvers (#6228) Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com> Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
- Loading branch information
1 parent
b0e0b7d
commit 259fd77
Showing
15 changed files
with
347 additions
and
39 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { removeNulls } from '../transforms' | ||
|
||
describe('removeNulls utility', () => { | ||
it('Changes nulls to undefined', () => { | ||
const input = { | ||
a: null, | ||
b: 'b', | ||
c: { | ||
d: null, // nested null | ||
e: 3, | ||
f: { | ||
g: null, // deeply nested null | ||
h: [null, null], // array of nulls is also transformed | ||
i: [1, 2, null, 4], | ||
}, | ||
}, | ||
myDate: new Date('2020-01-01'), | ||
} | ||
|
||
const result = removeNulls(input) | ||
|
||
expect(result).toEqual({ | ||
a: undefined, | ||
b: 'b', | ||
c: { | ||
d: undefined, | ||
e: 3, | ||
f: { | ||
g: undefined, | ||
h: [undefined, undefined], | ||
i: [1, 2, undefined, 4], | ||
}, | ||
}, | ||
myDate: new Date('2020-01-01'), | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Houses utility types commonly used on the api side | ||
*/ | ||
|
||
import { O, A } from 'ts-toolbelt' | ||
|
||
/** | ||
* ---- Prisma SDL Type Merge ---- | ||
* SDL is source of truth for KEYS | ||
* Prisma types is source of truth for VALUES (unless SDL-only field) | ||
*/ | ||
|
||
type AnyObject = Record<string | symbol | number, unknown> | ||
// Pick out unique keys on the SDL type | ||
type SdlOnlyFields<TPrisma, TSdl> = Omit<TSdl, keyof TPrisma> | ||
|
||
// Object with all the optional keys, so that we can make them nullable | ||
type PrismaTypeWithOptionalKeysFromSdl< | ||
TPrisma extends AnyObject, | ||
TSdl extends AnyObject | ||
> = Pick<TPrisma, O.OptionalKeys<TSdl>> | ||
|
||
// Make the optional values nullable | ||
type PrismaTypeWithOptionalKeysAndNullableValues< | ||
TPrisma extends AnyObject, | ||
TSdl extends AnyObject | ||
> = { | ||
[k in keyof PrismaTypeWithOptionalKeysFromSdl<TPrisma, TSdl>]?: | ||
| PrismaTypeWithOptionalKeysFromSdl<TPrisma, TSdl>[k] | ||
| null // Note: if we ever change the type of Maybe in codegen, it might be worth changing this to Maybe<T> | ||
} | ||
|
||
// Object with all the required keys | ||
type PrismaTypeWithRequiredKeysFromSdl< | ||
TPrisma extends AnyObject, | ||
TSdl extends AnyObject | ||
> = Pick<TPrisma, O.RequiredKeys<TSdl>> | ||
|
||
// To replace the unknowns with types from Sdl on SDL-only fields | ||
type OptionalsAndSdlOnly< | ||
TPrisma extends AnyObject, | ||
TSdl extends AnyObject | ||
> = PrismaTypeWithOptionalKeysAndNullableValues<TPrisma, TSdl> & | ||
SdlOnlyFields<TPrisma, TSdl> | ||
|
||
export type MakeRelationsOptional<T, TAllMappedModels> = { | ||
//object with optional relation keys | ||
[key in keyof T as T[key] extends TAllMappedModels | ||
? key | ||
: never]?: MakeRelationsOptional<T[key], TAllMappedModels> | ||
} & { | ||
// object without the relation keys | ||
[key in keyof T as T[key] extends TAllMappedModels ? never : key]: T[key] | ||
} | ||
|
||
// ⚡ All together now | ||
// Note: don't use O.Merge here, because it results in unknowns | ||
export type MergePrismaWithSdlTypes< | ||
TPrisma extends AnyObject, | ||
TSdl extends AnyObject, | ||
TAllMappedModels | ||
> = A.Compute< | ||
OptionalsAndSdlOnly<TPrisma, MakeRelationsOptional<TSdl, TAllMappedModels>> & | ||
PrismaTypeWithRequiredKeysFromSdl< | ||
TPrisma, | ||
MakeRelationsOptional<TSdl, TAllMappedModels> | ||
> | ||
> | ||
// ---- Prisma SDL Type Merge ---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.