Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Add Filtering #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Congratulations, the server is running. 🚀
The most important parts are implemented and working properly but we have room for improvements.

- [ ] Ordering
- [ ] Filtering
- [X] Filtering
- [ ] Custom arguments example
- [ ] Error handling
- [ ] DataLoader
Expand Down Expand Up @@ -205,3 +205,28 @@ Backward with cursor
}
}
```

With Filter

```graphql
{
products(last: 2, name: "Something") {
edges {
node {
id
name
description
createdAt
updatedAt
}
cursor
}
pageInfo {
startCursor
endCursor
hasPreviousPage
hasNextPage
}
}
}
```
10 changes: 8 additions & 2 deletions src/modules/product/product.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ export class ProductResolver {
private service!: ProductService

@Query(() => ProductConnection)
async products(@Args() args: ConnectionArgs): Promise<ProductConnection> {
return this.service.paginate(args)
async products(
@Args() args: ConnectionArgs,
@Arg('name', () => String, { nullable: true }) product_name: string
): Promise<ProductConnection> {
return this.service.paginate(
args,
product_name ? { name: product_name } : undefined
)
}

@Mutation(() => AddProductPayload)
Expand Down
9 changes: 6 additions & 3 deletions src/modules/product/product.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Service } from 'typedi'
import { Repository } from 'typeorm'
import { FindConditions, Repository } from 'typeorm'
import { InjectRepository } from 'typeorm-typedi-extensions'

import { ProductEntity } from './product.entity'
Expand All @@ -13,8 +13,11 @@ export class ProductService {
@InjectRepository(ProductEntity)
private readonly repository!: Repository<ProductEntity>

async paginate(args: ConnectionArguments): Promise<ProductConnection> {
return connectionFromRepository(args, this.repository)
async paginate(
args: ConnectionArguments,
filter?: FindConditions<ProductEntity>
): Promise<ProductConnection> {
return connectionFromRepository(args, this.repository, filter)
}

async findById(id: string): Promise<ProductEntity | undefined> {
Expand Down
11 changes: 7 additions & 4 deletions src/relay/connection.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import {
getOffsetWithDefault,
offsetToCursor
} from 'graphql-relay'
import { Repository } from 'typeorm'
import { FindConditions, Repository } from 'typeorm'

export async function connectionFromRepository<T>(
args: ConnectionArguments,
repository: Repository<T>
repository: Repository<T>,
filterArg?: FindConditions<T>
): Promise<Connection<T>> {
const { before, after, first, last } = args

const total = await repository.count()
const where = filterArg ? { where: filterArg } : undefined

const total = await repository.count(where)

// offsets
const beforeOffset = getOffsetWithDefault(before, total)
Expand All @@ -34,7 +37,7 @@ export async function connectionFromRepository<T>(
const take = Math.max(endOffset - startOffset, 1) // sql limit

// records
const entities = await repository.find({ skip, take })
const entities = await repository.find({ skip, take, where })

const edges = entities.map((entity, index) => ({
cursor: offsetToCursor(startOffset + index),
Expand Down