[Object: null prototype] { skip: 1, take: 2 }
这个问题出现在 nestjs + grapqhl 项目,主要是在 resolver 端未正确的对应 grapqhl 的查询类型定义造成的
如:
类型定义
export type PostListOptions = {
skip?: Maybe<Scalars['Int']>,
take?: Maybe<Scalars['Int']>,
sort?: Maybe<PostSortParameter>,
filter?: Maybe<PostFilterParameter>,
};
**resolver**
@Query()
async posts(
@Ctx() ctx: RequestContext,
@Args() args: QueryPostsArgs,
): Promise<PaginatedList<Post>> {
return this.postService.findAll(ctx, args.options || undefined);
}
**service**
async findAll(
ctx: RequestContext,
options?: ListQueryOptions<Post>,
): Promise<PaginatedList<Post>> {
const query = {
limit: 10,
offset: 0
};
if (options !== undefined && options !== null) {
const skip = options.skip;
let take = options.take;
if (options.skip !== undefined && options.take === undefined) {
take = Number.MAX_SAFE_INTEGER;
}
if (take !== undefined && take !== null) {
query.limit = take;
}
if (skip !== undefined && skip !== null) {
query.offset = skip;
}
}
const [ items, totalItems ] = await this.em.findAndCount(
Post,
{},
{
...query
});
return {
items,
totalItems
};
}