-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): configurable pagination, prisma option (#709)
Co-authored-by: Jason Kuhrt <jasonkuhrt@me.com>
- Loading branch information
1 parent
81a53b7
commit 18e017d
Showing
9 changed files
with
948 additions
and
45 deletions.
There are no files selected for viewing
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,35 @@ | ||
import { DMMF } from '@prisma/client/runtime' | ||
import { DmmfTypes } from '../dmmf' | ||
import { prismaStraegy } from './prisma' | ||
import { relayStrategy } from './relay' | ||
|
||
interface NormalizedPaginationArgs { | ||
cursor?: object | ||
skip?: string | number | ||
take?: string | number | ||
orderBy?: { [x: string]: 'asc' | 'desc' } | ||
[x: string]: any | ||
} | ||
|
||
export interface PaginationStrategy<GraphQLPaginationArgs extends object = object> { | ||
paginationArgNames: (keyof GraphQLPaginationArgs)[] | ||
transformDmmfArgs(params: { | ||
args: DmmfTypes.SchemaArg[] | ||
paginationArgNames: string[] | ||
field: DMMF.SchemaField | ||
}): DmmfTypes.SchemaArg[] | ||
resolve(args: GraphQLPaginationArgs): NormalizedPaginationArgs | ||
} | ||
|
||
// todo remove, but TS error if we do, dont' understand the error message | ||
interface PaginationStrategies { | ||
relay: typeof relayStrategy | ||
prisma: typeof prismaStraegy | ||
} | ||
|
||
export const paginationStrategies: PaginationStrategies = { | ||
relay: relayStrategy, | ||
prisma: prismaStraegy, | ||
} | ||
|
||
export type PaginationStrategyTypes = typeof relayStrategy | typeof prismaStraegy |
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,67 @@ | ||
import { PaginationStrategy } from '.' | ||
import { DmmfTypes, getReturnTypeName } from '../dmmf' | ||
import { keys } from '../utils' | ||
|
||
interface PrismaPaginationArgs { | ||
cursor?: object | ||
take?: number | ||
skip?: number | ||
} | ||
|
||
const prismaPaginationArgsToDmmfArgs: Record< | ||
'take' | 'skip' | 'cursor', | ||
(typeName: string) => DmmfTypes.SchemaArg | ||
> = { | ||
take: () => ({ | ||
name: 'take', | ||
inputType: { | ||
type: 'Int', | ||
kind: 'scalar', | ||
isRequired: false, | ||
isList: false, | ||
isNullable: false, | ||
}, | ||
}), | ||
skip: () => ({ | ||
name: 'skip', | ||
inputType: { | ||
type: 'Int', | ||
kind: 'scalar', | ||
isRequired: false, | ||
isList: false, | ||
isNullable: false, | ||
}, | ||
}), | ||
cursor: (typeName: string) => ({ | ||
name: 'cursor', | ||
inputType: { | ||
type: `${typeName}WhereUniqueInput`, | ||
kind: 'object', | ||
isRequired: false, | ||
isList: false, | ||
isNullable: false, | ||
}, | ||
}), | ||
} | ||
|
||
export const prismaStraegy: PaginationStrategy<PrismaPaginationArgs> = { | ||
paginationArgNames: keys(prismaPaginationArgsToDmmfArgs), | ||
transformDmmfArgs({ paginationArgNames, args, field }) { | ||
const fieldOutputTypeName = getReturnTypeName(field.outputType.type) | ||
|
||
// Remove old pagination args | ||
args = args.filter((dmmfArg) => !paginationArgNames.includes(dmmfArg.name)) | ||
|
||
// Push new pagination args | ||
args.push( | ||
prismaPaginationArgsToDmmfArgs.take(fieldOutputTypeName), | ||
prismaPaginationArgsToDmmfArgs.skip(fieldOutputTypeName), | ||
prismaPaginationArgsToDmmfArgs.cursor(fieldOutputTypeName) | ||
) | ||
|
||
return args | ||
}, | ||
resolve(args) { | ||
return args | ||
}, | ||
} |
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
Oops, something went wrong.