graphql 查询 [Object: null prototype] { skip: 1, take: 2 } 问题

[Object: null prototype] { skip: 1, take: 2 }

这个问题出现在 nestjs + grapqhl 项目,主要是在 resolver 端未正确的对应 grapqhl 的查询类型定义造成的

如:

类型定义

export type PostListOptions = {
  skip?: Maybe,
  take?: Maybe,
  sort?: Maybe,
  filter?: Maybe,
};

**resolver**

@Query()
async posts(
@Ctx() ctx: RequestContext,
@Args() args: QueryPostsArgs,
): Promise> {
return this.postService.findAll(ctx, args.options || undefined);
}


**service**

async findAll(
ctx: RequestContext,
options?: ListQueryOptions,
): Promise> {
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
};
}

你可能感兴趣的:(graphql 查询 [Object: null prototype] { skip: 1, take: 2 } 问题)