Skip to content

Commit

Permalink
feat: allow UnionTypeComposer, ScalarTypeComposer, EnumTypeComposer t…
Browse files Browse the repository at this point in the history
…o be as a source TC (#48)

* init

* chore: fix typescript definitions

* feat: allow ScalarTypeComposer & EnumTypeComposer to be as source TC

Co-authored-by: nodkz <pavel.chertorogov@gmail.com>
  • Loading branch information
theasmoth and nodkz authored Aug 6, 2021
1 parent 85dc8c7 commit 58939d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import { Resolver, inspect } from 'graphql-compose';
import type {
ObjectTypeComposer,
InterfaceTypeComposer,
UnionTypeComposer,
ScalarTypeComposer,
EnumTypeComposer,
ResolverResolveParams,
ObjectTypeComposerArgumentConfigMap,
} from 'graphql-compose';
import { preparePaginationTC } from './types';

export const DEFAULT_RESOLVER_NAME = 'pagination';
export const DEFAULT_PER_PAGE = 20;
const ALLOWED_TYPE_COMPOSERS = [
'ObjectTypeComposer',
'InterfaceTypeComposer',
'UnionTypeComposer',
'ScalarTypeComposer',
'EnumTypeComposer',
];

export type PaginationResolverOpts = {
findManyResolver: Resolver;
Expand Down Expand Up @@ -40,12 +50,19 @@ export interface PaginationTArgs {
}

export function preparePaginationResolver<TSource, TContext>(
tc: ObjectTypeComposer<TSource, TContext> | InterfaceTypeComposer<TSource, TContext>,
tc:
| ObjectTypeComposer<TSource, TContext>
| InterfaceTypeComposer<TSource, TContext>
| UnionTypeComposer<TSource, TContext>
| ScalarTypeComposer<TContext>
| EnumTypeComposer<TContext>,
opts: PaginationResolverOpts
): Resolver<TSource, TContext, PaginationTArgs> {
if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !ALLOWED_TYPE_COMPOSERS.includes(tc.constructor.name)) {
throw new Error(
'First arg for prepareConnectionResolver() should be instance of ObjectTypeComposer'
`First arg for preparePaginationResolver() should be instance of ${ALLOWED_TYPE_COMPOSERS.join(
' or '
)}`
);
}

Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
ObjectTypeComposer,
SchemaComposer,
InterfaceTypeComposer,
UnionTypeComposer,
ScalarTypeComposer,
EnumTypeComposer,
} from 'graphql-compose';

// PaginationInfo should be global
Expand Down Expand Up @@ -41,7 +44,12 @@ export function preparePaginationInfoTC<TContext>(
}

export function preparePaginationTC<TSource, TContext>(
tc: ObjectTypeComposer<TSource, TContext> | InterfaceTypeComposer<TSource, TContext>,
tc:
| ObjectTypeComposer<TSource, TContext>
| InterfaceTypeComposer<TSource, TContext>
| UnionTypeComposer<TSource, TContext>
| ScalarTypeComposer<TContext>
| EnumTypeComposer<TContext>,
resolverName?: string
): ObjectTypeComposer<TSource, TContext> {
const schemaComposer = tc.schemaComposer;
Expand Down

0 comments on commit 58939d4

Please sign in to comment.