Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 itemListByCollectionId burned filter #55

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
8 changes: 5 additions & 3 deletions src/clients/SquidClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
genericCountQuery,
getFields,
includeBurned,
includeBurnedOption,
optionToQuery,
} from './defaults'

Expand Down Expand Up @@ -362,10 +363,11 @@ class SquidClient implements AbstractClient<SquidCollection, SquidNFT> {
): GraphQuery {
const toQuery = getFields(options?.fields)
const optionList = optionToQuery(options, true)
const burned = includeBurnedOption(options)
return build(
`items: nftEntities(where: {collection: {id_eq: "${id}"}} ${optionList})`,
toQuery,
)
`items: nftEntities(where: {collection: {id_eq: "${id}"} ${burned}} ${optionList})`,
toQuery
);
}

itemListForSale(options?: QueryProps<SquidNFT>): GraphQuery {
Expand Down
10 changes: 9 additions & 1 deletion src/clients/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function ensureOrderBy(orderBy?: OrderBy | OrderBy[]): string {
return Array.isArray(orderBy) ? stringFromArray(orderBy) : orderBy ?? '[]' // this is a trick to handle undefined
}

type Burned = '' | `burned_eq: ${false}`
type Burned = '' | `burned_eq: ${false}` | `burned_eq: ${true}`
export function includeBurned<T = unknown>(options?: QueryProps<T>): Burned {
if (options && options.burned) {
return ''
Expand All @@ -113,6 +113,14 @@ export function includeBurned<T = unknown>(options?: QueryProps<T>): Burned {
return 'burned_eq: false'
}

export function includeBurnedOption<T = unknown>(options?: QueryProps<T>): Burned {
if (options && String(options.burned)) {
return `burned_eq: ${options.burned}`
}

return ''
}

export function strOf<T>(value: T): string {
return `"${value}"`
}
Expand Down
33 changes: 32 additions & 1 deletion test/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it, describe } from 'vitest'
import { parsePath, pathToRequest } from '../src/rest/path'
import { getClient } from '../src'
import { extendFields, getClient } from '../src'

describe.only('Path utils', () => {
describe('parse path should', () => {
Expand Down Expand Up @@ -47,6 +47,37 @@ describe.only('Path utils', () => {
})
})

describe('ahp filter burned items', () => {
const collectionId = '244'
const client = getClient('ahp')

it('should return only burned items from collection', async () => {
const query = client.itemListByCollectionId(collectionId, { burned: true, orderBy: 'blockNumber_ASC', fields: extendFields(['burned']) })
const result = await client.fetch(query)

expect(result).not.toBeUndefined()

if (result.data) {
result.data.items.forEach(element => {
expect(element).toHaveProperty('burned', true)
})
}
})

it('should return all non-burned items from collection', async () => {
const query = client.itemListByCollectionId(collectionId, { burned: false, orderBy: 'blockNumber_ASC', fields: extendFields(['burned']) })
const result = await client.fetch(query)

expect(result).not.toBeUndefined()

if (result.data) {
result.data.items.forEach(element => {
expect(element).toHaveProperty('burned', false)
})
}
})
})

describe('ahk path to request', () => {
const tests = [
{ input: 'collection/2305670031' },
Expand Down