Skip to content

Commit

Permalink
fix (#1517)
Browse files Browse the repository at this point in the history
Co-authored-by: typedarray <[email protected]>
  • Loading branch information
typedarray and typedarray authored Feb 13, 2025
1 parent 12315e6 commit 452c699
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-fans-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ponder": patch
---

Fixed a bug where hex value arguments to singular query fields in GraphQL did not respect case-insensitive comparison.
51 changes: 51 additions & 0 deletions packages/core/src/graphql/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2220,3 +2220,54 @@ test("snake case table and column names with where clause", async (context) => {
},
});
});

test("singular with hex primary key uses case insensitive where", async (context) => {
const account = onchainTable("account", (t) => ({
address: t.hex().primaryKey(),
}));

const schema = { account };

const { database, indexingStore } = await setupDatabaseServices(context, {
schemaBuild: { schema },
});
const contextValue = buildContextValue(database);
const query = (source: string) =>
execute({ schema: graphqlSchema, contextValue, document: parse(source) });

const CHECKSUM_ADDRESS = "0x67BD7c89B54Fa52826186A57363A9303DB3E7626";
const LOWERCASE_ADDRESS = "0x67bd7c89b54fa52826186a57363a9303db3e7626";

await indexingStore
.insert(schema.account)
.values({ address: CHECKSUM_ADDRESS });

const graphqlSchema = buildGraphQLSchema({ schema });

const result = await query(`
query {
account(address: "${CHECKSUM_ADDRESS}") {
address
}
accounts(where: { address: "${CHECKSUM_ADDRESS}" }) {
items {
address
}
}
}
`);

expect(result.errors?.[0]?.message).toBeUndefined();
expect(result.data).toMatchObject({
account: {
address: LOWERCASE_ADDRESS,
},
accounts: {
items: [
{
address: LOWERCASE_ADDRESS,
},
],
},
});
});
41 changes: 33 additions & 8 deletions packages/core/src/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Database } from "@/database/index.js";
import type { OnchainTable } from "@/drizzle/onchain.js";
import { normalizeColumn } from "@/indexing-store/cache.js";
import type { Schema } from "@/internal/types.js";
import type { Drizzle, ReadonlyDrizzle } from "@/types/db.js";
import { never } from "@/utils/never.js";
Expand Down Expand Up @@ -743,7 +744,7 @@ const conditionSuffixesByLengthDesc = Object.values(conditionSuffixes)
function buildWhereConditions(
where: Record<string, any> | undefined,
columns: Record<string, Column>,
): (SQL | undefined)[] {
) {
const conditions: (SQL | undefined)[] = [];

if (where === undefined) return conditions;
Expand Down Expand Up @@ -991,13 +992,37 @@ export function buildDataLoaderCache({
limit: encodedIds.length,
});

return decodedRowFragments.map((decodedRowFragment) => {
return rows.find((row) =>
Object.entries(decodedRowFragment).every(
([col, val]) => row[col] === val,
),
);
});
// Now, we need to order the rows coming out of the database to match
// the order of the IDs passed in. To accomplish this, we need to do
// a comparison of the decoded row PK fragments with the database rows.
// This is tricky because the decoded row PK fragments are not normalized,
// so some comparisons will fail (eg for our PgHex column type).
// To fix this, we need to normalize the values before doing the comparison.
return (
decodedRowFragments
// Normalize the decoded row fragments
.map((fragment) =>
Object.fromEntries(
Object.entries(fragment).map(([col, val]) => {
const column = table.columns[col];
if (column === undefined) {
throw new Error(
`Unknown column '${table.tsName}.${col}' used in dataloader row ID fragment`,
);
}
return [col, normalizeColumn(column, val, false)];
}),
),
)
// Find the database row corresponding to each normalized row fragment
.map((fragment) =>
rows.find((row) =>
Object.entries(fragment).every(
([col, val]) => row[col] === val,
),
),
)
);
},
{ maxBatchSize: 1_000 },
);
Expand Down

0 comments on commit 452c699

Please sign in to comment.