Skip to content

Commit

Permalink
feat(package/gqty): reduce bundle size with ohash over object-hash (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary authored Feb 8, 2025
1 parent 8d9a312 commit d379a9f
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 209 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-vans-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gqty': minor
---

reduce bundle size with ohash over object-hash
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@graphql-tools/wrap": "^10.0.29",
"@inquirer/prompts": "^5.5.0",
"chalk": "^4.1.2",
"commander": "^12.1.0",
"commander": "^13.1.0",
"cosmiconfig": "^9.0.0",
"cross-fetch": "^4.1.0",
"fast-glob": "^3.3.3",
Expand Down
3 changes: 1 addition & 2 deletions packages/gqty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@
"just-safe-get": "^4.2.0",
"just-safe-set": "^4.2.1",
"multidict": "^1.0.9",
"object-hash": "^3.0.0",
"p-defer": "^3.0.0"
},
"devDependencies": {
"@types/node": "^22.13.1",
"@types/object-hash": "^3.0.6",
"@types/ws": "^8.5.14",
"@typescript-eslint/eslint-plugin": "^8.23.0",
"@typescript-eslint/parser": "^8.23.0",
Expand All @@ -97,6 +95,7 @@
"graphql-ws": "^5.16.2",
"jest": "^30.0.0-alpha.7",
"just-memoize": "^2.2.0",
"ohash": "^1.1.4",
"p-lazy": "^3.1.0",
"test-utils": "workspace:^",
"tsc-watch": "^6.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/gqty/src/Client/compat/resolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const createLegacyResolved = <
{
fetchOptions,
noCache = false, // prevent cache writes after fetch
//nonSerializableVariables, // Ignored, object-hash can handle files
//nonSerializableVariables, // Ignored since our hasher can handle files
onCacheData,
onEmptyResolve,
onNoCacheFound,
Expand Down
4 changes: 2 additions & 2 deletions packages/gqty/src/Utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import memoize from 'just-memoize';
import objectHash from 'object-hash';
import { objectHash, sha256 } from 'ohash';

/**
* Memoized hash function, with a prefix to avoid starting with a number.
*/
export const hash = memoize((...args: unknown[]) =>
objectHash(args, { unorderedObjects: false }).replace(/^(\d)/, 'a$1')
sha256(objectHash(args, { unorderedObjects: false })).replace(/^(\d)/, 'a$1')
);
8 changes: 4 additions & 4 deletions packages/gqty/test/buildQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ describe('buildQuery()', () => {
);

expect(query).toMatchInlineSnapshot(
`"query($a03b9b:Int!$ad2f8d:String!){d gqtyAlias_1:a(a:$a03b9b b:$ad2f8d){a_b a_c}}"`
`"query($a1968d:String!$a93e62:Int!){d gqtyAlias_1:a(a:$a93e62 b:$a1968d){a_b a_c}}"`
);
expect(() => parse(query)).not.toThrow();
expect(variables).toEqual({ a03b9b: 1, ad2f8d: 1 });
expect(variables).toEqual({ a1968d: 1, a93e62: 1 });
expect(officialStripIgnoredCharacters(query)).toBe(query);
});

Expand All @@ -104,14 +104,14 @@ describe('buildQuery()', () => {
const [{ query, variables }] = buildQuery(new Set([selectionA]));

expect(query).toMatchInlineSnapshot(
`"mutation($a03b9b:Int!$ad2f8d:String!){gqtyAlias_1:a(a:$a03b9b b:$ad2f8d)}"`
`"mutation($a1968d:String!$a93e62:Int!){gqtyAlias_1:a(a:$a93e62 b:$a1968d)}"`
);

expect(() => {
parse(query);
}).not.toThrow();

expect(variables).toEqual({ a03b9b: 1, ad2f8d: 1 });
expect(variables).toEqual({ a1968d: 1, a93e62: 1 });

expect(officialStripIgnoredCharacters(query)).toBe(query);
});
Expand Down
224 changes: 112 additions & 112 deletions packages/gqty/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,38 +180,38 @@ describe('core#resolve', () => {
// 2. Ensure selections made
expect([...selections].map((v) => v.cacheKeys.join('.')))
.toMatchInlineSnapshot(`
[
"query.dogs.__typename",
"query.dogs.id",
"query.dogs.e61ad2",
]
`);
[
"query.dogs.__typename",
"query.dogs.id",
"query.dogs.a10168",
]
`);

// 3. resolve()
await expect(resolve()).resolves.toMatchInlineSnapshot(`
[
{
"data": {
"dogs": [
{
"__typename": "Dog",
"e61ad2": "arf!",
"id": "1",
},
{
"__typename": "Dog",
"e61ad2": "arf!",
"id": "2",
},
],
},
"extensions": {
"hash": "a242b05e35ff15857d32ed1a1eeb07500b5138f16",
"type": "query",
},
},
]
`);
[
{
"data": {
"dogs": [
{
"__typename": "Dog",
"a10168": "arf!",
"id": "1",
},
{
"__typename": "Dog",
"a10168": "arf!",
"id": "2",
},
],
},
"extensions": {
"hash": "a4049161dd3fbeed6f5c5658ae5aeb067002147bed201698119cf6fa3c93f6f8d",
"type": "query",
},
},
]
`);

// 4. Make selections again
query.dogs = [];
Expand All @@ -220,12 +220,12 @@ describe('core#resolve', () => {
// 5. Expect previous sub-selections
expect([...selections].map((v) => v.cacheKeys.join('.')))
.toMatchInlineSnapshot(`
[
"query.dogs.__typename",
"query.dogs.id",
"query.dogs.e61ad2",
]
`);
[
"query.dogs.__typename",
"query.dogs.id",
"query.dogs.a10168",
]
`);

await expect(resolve()).resolves.toMatchInlineSnapshot(`undefined`);

Expand All @@ -237,37 +237,37 @@ describe('core#resolve', () => {

expect([...selections].map((v) => v.cacheKeys.join('.')))
.toMatchInlineSnapshot(`
[
"query.dogs.__typename",
"query.dogs.id",
"query.dogs.a27c8c",
]
`);
[
"query.dogs.__typename",
"query.dogs.id",
"query.dogs.a516ab",
]
`);

await expect(resolve()).resolves.toMatchInlineSnapshot(`
[
{
"data": {
"dogs": [
{
"__typename": "Dog",
"a27c8c": "arf!arf!arf!",
"id": "1",
},
{
"__typename": "Dog",
"a27c8c": "arf!arf!arf!",
"id": "2",
},
],
},
"extensions": {
"hash": "ec6389bde813c1b5cb4b5b294e573082ec4270e1",
"type": "query",
},
},
]
`);
[
{
"data": {
"dogs": [
{
"__typename": "Dog",
"a516ab": "arf!arf!arf!",
"id": "1",
},
{
"__typename": "Dog",
"a516ab": "arf!arf!arf!",
"id": "2",
},
],
},
"extensions": {
"hash": "a559a7f6482e7959e433f04f7274247f7cf5661540c3af47424ee1446027a816",
"type": "query",
},
},
]
`);
});

it('should retain previous sub-selections on caches with null objects', async () => {
Expand All @@ -291,9 +291,9 @@ describe('core#resolve', () => {
expect([...selections].map((v) => v.cacheKeys.join('.')))
.toMatchInlineSnapshot(`
[
"query.a7f6f9.__typename",
"query.a7f6f9.id",
"query.a7f6f9.a7a17c",
"query.a6c916.__typename",
"query.a6c916.id",
"query.a6c916.e61179",
]
`);

Expand All @@ -302,14 +302,14 @@ describe('core#resolve', () => {
[
{
"data": {
"a7f6f9": {
"a6c916": {
"__typename": "Human",
"a7a17c": "Now you see me...",
"e61179": "Now you see me...",
"id": "1",
},
},
"extensions": {
"hash": "be79c4bae3a49b23d4beb0bbdbf9023ba0ca4898",
"hash": "a10b43bf9c52ee5bbc996198f56cc4f89f203199bfb6ad6f13b1082a5bcc1eae7",
"type": "query",
},
},
Expand All @@ -322,26 +322,26 @@ describe('core#resolve', () => {
// 5. Expect sub-selections
expect([...selections].map((v) => v.cacheKeys.join('.')))
.toMatchInlineSnapshot(`
[
"query.a3f697.__typename",
"query.a3f697.id",
"query.a3f697.bad514",
]
`);
[
"query.a1cda9.__typename",
"query.a1cda9.id",
"query.a1cda9.a2ce8c",
]
`);

await expect(resolve()).resolves.toMatchInlineSnapshot(`
[
{
"data": {
"a3f697": null,
},
"extensions": {
"hash": "fc1b66218d65d51732ed66b85bb5450d0332ba44",
"type": "query",
},
},
]
`);
[
{
"data": {
"a1cda9": null,
},
"extensions": {
"hash": "b9ae5a3dfdf2c9949d51bdf555c8eecc4c4c091d5d11cf0734853c4ba10bdd34",
"type": "query",
},
},
]
`);

// 6. Ensure previous selections of no more than the last 1 fetch is reused.
context.cache.clear();
Expand All @@ -350,30 +350,30 @@ describe('core#resolve', () => {

expect([...selections].map((v) => v.cacheKeys.join('.')))
.toMatchInlineSnapshot(`
[
"query.a4fd2c.__typename",
"query.a4fd2c.id",
"query.a4fd2c.eb7f1d",
]
`);
[
"query.a7816d.__typename",
"query.a7816d.id",
"query.a7816d.a0564a",
]
`);

await expect(resolve()).resolves.toMatchInlineSnapshot(`
[
{
"data": {
"a4fd2c": {
"__typename": "Human",
"eb7f1d": "I am Jane.",
"id": "2",
},
},
"extensions": {
"hash": "a1fac2bb2a1052821915362495c678811cb296e09",
"type": "query",
},
},
]
`);
[
{
"data": {
"a7816d": {
"__typename": "Human",
"a0564a": "I am Jane.",
"id": "2",
},
},
"extensions": {
"hash": "a9df6d7b05fcef74f68db84388ccac8b33b9b3766e859e764b0e626741628bcd2",
"type": "query",
},
},
]
`);
});
});

Expand Down Expand Up @@ -549,8 +549,8 @@ describe('compat', () => {

expect(queries.map(({ query }) => query)).toMatchInlineSnapshot(`
[
"query TestQueryA($e8a374:String){a5b434:human(name:$e8a374){__typename}}",
"mutation TestMutation($ef3ee3:String!){a133ff:humanMutation(nameArg:$ef3ee3){__typename}}",
"query TestQueryA($a860ed:String){a1c0de:human(name:$a860ed){__typename}}",
"mutation TestMutation($de937b:String!){eed53f:humanMutation(nameArg:$de937b){__typename}}",
"query TestQueryB{hello}",
]
`);
Expand Down
Loading

0 comments on commit d379a9f

Please sign in to comment.