Skip to content

Commit

Permalink
fix(@nftx/subgraph): rework and/or operators to work together nicely
Browse files Browse the repository at this point in the history
we recently added support for OR operators, however if you use an AND operator in the same clause, it would not work.
This fix does some additional work to massage the output into nested and/or clauses in order to create a valid query.

As part of this fix the createQuery method has been refactored to be easier to understand and maintain.
  • Loading branch information
jackmellis committed May 13, 2024
1 parent da2caec commit 143e76f
Show file tree
Hide file tree
Showing 14 changed files with 575 additions and 487 deletions.
129 changes: 90 additions & 39 deletions packages/subgraph/src/__tests__/createQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,41 @@ it('allows for simple OR queries', () => {
expect(ignoreWs(actual)).toBe(ignoreWs(expected));
});

it('wraps OR queries in an AND query if necessary', () => {
const g = createQuery<Query>();

const query = g.positions
.where((w) => [w.liquidity('0'), w.or(w.token0('0x0'), w.token1('0x0'))])
.select((s) => [s.id]);

const actual = query.toString();
const expected = `{
positions(
where: {
and: [
{
liquidity: "0"
},
{
or: [
{
token0: "0x0"
},
{
token1: "0x0"
}
]
}
]
}
) {
id
}
}`;

expect(ignoreWs(actual)).toBe(ignoreWs(expected));
});

it('allows for complex OR/AND queries', () => {
const g = createQuery<Query>();

Expand Down Expand Up @@ -410,48 +445,58 @@ it('allows for complex OR/AND queries', () => {
const expected = `{
positions(
where: {
liquidity_gt: "0",
or: [
{
tickLower_gt: 0
},
and: [
{
tickUpper_lt: 999999999999
liquidity_gt: "0"
},
{
and: [
or: [
{
tickLower_gt: 0
},
{
token0: "0x000",
token1_: {
id: "0x001"
},
or: [
tickUpper_lt: 999999999999
},
{
and: [
{
owner: "0x000"
token0: "0x000",
token1_: {
id: "0x001"
}
},
{
or: [
{
owner: "0x000"
}
]
}
]
}
]
},
{
pool_: {
and: [
{
name: "foo",
or: [
},
{
pool_: {
and: [
{
name: "foo"
},
{
token0: "0x000"
name: "bar"
},
{
token1: "0x001"
or: [
{
token0: "0x000"
},
{
token1: "0x001"
}
]
}
]
},
{
name: "bar"
}
]
}
}
]
}
]
}
Expand Down Expand Up @@ -897,20 +942,26 @@ it('prettifies the result', () => {
orderBy: liquidity
orderDirection: desc
where: {
createdAtTimestamp_gt: "0",
positions_: {
tickLower_gt: 0,
tickUpper_lt: 100000
},
or: [
and: [
{
liquidity_gt: "0"
createdAtTimestamp_gt: "0",
positions_: {
tickLower_gt: 0,
tickUpper_lt: 100000
}
},
{
and: [
or: [
{
liquidity_gt: "100",
liquidity_lt: "200"
liquidity_gt: "0"
},
{
and: [
{
liquidity_gt: "100",
liquidity_lt: "200"
}
]
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions packages/subgraph/src/createQuery/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type WithoutNull<T> = {
[K in keyof T]: Exclude<T[K], null>;
};

export type Defined<T> = WithoutNull<Required<T>>;
Loading

0 comments on commit 143e76f

Please sign in to comment.