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

Agg greater than #2703

Open
wants to merge 3 commits 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
3 changes: 3 additions & 0 deletions packages/query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- When using an aggregate query as a query condition, the query results may be incorrect.

## [2.21.0] - 2025-02-19
### Changed
- Update node version to LTS (#2655)
Expand Down
53 changes: 44 additions & 9 deletions packages/query/src/graphql/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,26 @@ describe('GraphqlModule', () => {
"updatedAt" timestamp with time zone NOT NULL,
CONSTRAINT _metadata_pkey PRIMARY KEY (key)
)`);
await pool.query(`CREATE TABLE "${dbSchema}"."pools" (
"id" text COLLATE "pg_catalog"."default" NOT NULL,
CONSTRAINT "pool_pkey" PRIMARY KEY ("id")
)`);

await pool.query(`CREATE TABLE "${dbSchema}"."pool_snapshots" (
"id" text COLLATE "pg_catalog"."default" NOT NULL,
"pool_id" text COLLATE "pg_catalog"."default" NOT NULL,
"block_number" int4 NOT NULL,
"total_reserve" numeric,
CONSTRAINT "pool_snapshots_pkey" PRIMARY KEY ("id")
CONSTRAINT "pool_snapshots_pkey" PRIMARY KEY ("id"),
CONSTRAINT "fk_pool"
FOREIGN KEY(pool_id)
REFERENCES "${dbSchema}"."pools"(id)
)`);
await pool.query(`INSERT INTO "${dbSchema}"."pools" ("id") VALUES ('1'),('2'),('3'),('4')`);
});

afterEach(async () => {
await pool.query(`DROP TABLE "${dbSchema}"."pool_snapshots"`);
await pool.query(`DROP TABLE subquery_1._metadata`);
await pool.query(`DROP SCHEMA "${dbSchema}" CASCADE`);
});

afterAll(async () => {
Expand Down Expand Up @@ -190,13 +197,10 @@ describe('GraphqlModule', () => {
expect(fetchedMeta).toMatchObject(mock);
});

// sum(price_amount)
it('AggregateSpecsPlugin support big number', async () => {
await pool.query(
`INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '1')`
);
// sum(price_amount) TODO Wait for resolution
it.skip('AggregateSpecsPlugin support big number', async () => {
await pool.query(
`INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('2', '1', 1, '20000000000000000000000')`
`INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '1'),('2', '1', 1, '20000000000000000000000')`
);

const server = await createApolloServer();
Expand Down Expand Up @@ -244,6 +248,37 @@ describe('GraphqlModule', () => {
expect(aggregate.max.totalReserve).toEqual('20000000000000000000000');
});

it('AggregateSpecsPlugin sum greaterThan', async () => {
await pool.query(
`INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '6'),('2', '1', 1, '1111')`
);
const server = await createApolloServer();
const GET_META = gql`
query {
pools(filter: {poolSnapshots: {aggregates: {sum: {totalReserve: {greaterThan: "3"}}}}}) {
nodes {
poolSnapshots {
nodes {
totalReserve
blockNumber
}
groupedAggregates(groupBy: []) {
sum {
totalReserve
}
}
}
}
}
}
`;

const results = await server.executeOperation({query: GET_META});

expect(results.data).toBeDefined();
expect(results.data!.pools?.nodes[0].poolSnapshots?.groupedAggregates[0]?.sum?.totalReserve).toEqual('1117');
});

// github issue #2387 : orderBy with orderByNull
it('PgOrderByUnique plugin correctly orders NULL values using orderByNull param', async () => {
await pool.query(`
Expand Down
6 changes: 5 additions & 1 deletion packages/query/src/graphql/plugins/PgAggregationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import AddConnectionGroupedAggregatesPlugin from '@graphile/pg-aggregates/dist/A
import AddGroupByAggregateEnumsPlugin from '@graphile/pg-aggregates/dist/AddGroupByAggregateEnumsPlugin';
import AddGroupByAggregateEnumValuesForColumnsPlugin from '@graphile/pg-aggregates/dist/AddGroupByAggregateEnumValuesForColumnsPlugin';
import AddHavingAggregateTypesPlugin from '@graphile/pg-aggregates/dist/AddHavingAggregateTypesPlugin';
// TODO: This method can ensure that the retrieved bigfloat data is correct, but when there is a WHERE condition, it may retrieve incorrect data.
// Refer to unit test `AggregateSpecsPlugin support big number`.
// The same issues: https://github.com/graphile/pg-aggregates/issues/39
// import AggregateSpecsPlugin from './PgAggregateSpecsPlugin';
import AggregateSpecsPlugin from '@graphile/pg-aggregates/dist/AggregateSpecsPlugin';
import FilterRelationalAggregatesPlugin from '@graphile/pg-aggregates/dist/FilterRelationalAggregatesPlugin';
import InflectionPlugin from '@graphile/pg-aggregates/dist/InflectionPlugin';
import {AggregateSpec, AggregateGroupBySpec} from '@graphile/pg-aggregates/dist/interfaces';

import type {Plugin} from 'graphile-build';
import {makePluginByCombiningPlugins} from 'graphile-utils';
import {argv} from '../../yargs';
import AggregateSpecsPlugin from './PgAggregateSpecsPlugin';
import OrderByAggregatesPlugin from './PgOrderByAggregatesPlugin';

const aggregate = argv('aggregate') as boolean;
Expand Down
Loading