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

Enable stabilization of objects/lists going through loadMany/loadOne multistep #2193

Merged
merged 8 commits into from
Oct 2, 2024
Merged
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
7 changes: 7 additions & 0 deletions .changeset/famous-birds-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@dataplan/pg": patch
"grafast": patch
---

Add support for stable deduplication of object/list arguments to
loadOne/loadMany, reducing redundant fetches.
247 changes: 239 additions & 8 deletions grafast/grafast/__tests__/loadOne-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,71 @@ import { expect } from "chai";
import type { ExecutionResult } from "graphql";
import { it } from "mocha";

import type { ExecutableStep, LoadOneCallback } from "../dist/index.js";
import {
grafast,
list,
loadOne,
makeGrafastSchema,
object,
import type {
ExecutableStep,
LoadedRecordStep,
LoadOneCallback,
} from "../dist/index.js";
import { context, grafast, loadOne, makeGrafastSchema } from "../dist/index.js";

interface Thing {
id: number;
orgId: number;
orgRegNo: number;
name: string;
reallyLongBio: string;
}
const THINGS: Thing[] = [
{
id: 1,
orgId: 27,
orgRegNo: 93,
name: "Eyedee Won",
reallyLongBio: "Really long bio. ".repeat(1000),
},
{
id: 2,
orgId: 42,
orgRegNo: 120,
name: "Idee Too",
reallyLongBio: "Super long bio. ".repeat(1000),
},
{
id: 2003,
orgId: 27,
orgRegNo: 987,
name: "Eye D. Tree",
reallyLongBio: "Somewhat long bio. ".repeat(1000),
},
{
id: 2004,
orgId: 42,
orgRegNo: 987,
name: "I.D. Phwoar",
reallyLongBio: "Quite long bio. ".repeat(1000),
},
];

interface Org {
id: number;
}
const ORGS: Org[] = [
{
id: 27,
},
{
id: 42,
},
];

declare global {
namespace Grafast {
interface Context {
orgId: number;
}
}
}

function pick<T extends object, K extends keyof T>(
obj: T,
keys: readonly K[],
Expand All @@ -39,7 +77,13 @@ function pick<T extends object, K extends keyof T>(
}

let CALLS: {
specs: readonly (number | { identifier: number } | readonly number[])[];
specs: ReadonlyArray<
| number
| { identifier: number }
| readonly [identifier: number]
| { orgId: number; regNo: number }
| readonly [orgId: number, regNo: number]
>;
result: object;
attributes: readonly (keyof Thing)[] | null;
params: object;
Expand Down Expand Up @@ -80,18 +124,66 @@ const loadThingByIdentifierLists: LoadOneCallback<
return result;
};

const loadThingByOrgIdRegNoObjs: LoadOneCallback<
{ orgId: number; regNo: number },
Thing,
Record<string, never>
> = (specs, { attributes, params }) => {
const result = specs
.map((spec) =>
THINGS.find((t) => t.orgId === spec.orgId && t.orgRegNo === spec.regNo),
)
.map((t) => (t && attributes ? pick(t, attributes) : t));
CALLS.push({ specs, result, attributes, params });
return result;
};

const loadThingByOrgIdRegNoTuples: LoadOneCallback<
readonly [orgId: number, regNo: number],
Thing,
Record<string, never>
> = (specs, { attributes, params }) => {
const result = specs
.map((spec) =>
THINGS.find((t) => t.orgId === spec[0] && t.orgRegNo === spec[1]),
)
.map((t) => (t && attributes ? pick(t, attributes) : t));
CALLS.push({ specs, result, attributes, params });
return result;
};

const loadOrgByIds: LoadOneCallback<number, Org, Record<string, never>> = (
specs,
{ attributes, params },
) => {
const result = specs
.map((id) => ORGS.find((t) => t.id === id))
.map((t) => (t && attributes ? pick(t, attributes) : t));
// CALLS.push({ specs, result, attributes, params });
return result;
};

const makeSchema = (useStreamableStep = false) => {
return makeGrafastSchema({
typeDefs: /* GraphQL */ `
type Thing {
id: Int!
name: String!
reallyLongBio: String!
org: Org!
orgRegNo: Int!
}
type Org {
id: Int!
thingByTuple(regNo: Int!): Thing
thingByObj(regNo: Int!): Thing
}
type Query {
thingById(id: Int!): Thing
thingByIdObj(id: Int!): Thing
thingByIdList(id: Int!): Thing
thingByOrgIdRegNoTuple(regNo: Int!): Thing
thingByOrgIdRegNoObj(regNo: Int!): Thing
}
`,
plans: {
Expand All @@ -113,6 +205,47 @@ const makeSchema = (useStreamableStep = false) => {
loadThingByIdentifierLists,
);
},
thingByOrgIdRegNoTuple(_, { $regNo }) {
const $orgId = context().get("orgId");
return loadOne(
[$orgId, $regNo],
// Deliberately not using ioEquivalence here to test stable object/tuple creation
//["orgId", "orgRegNo"],
loadThingByOrgIdRegNoTuples,
);
},
thingByOrgIdRegNoObj(_, { $regNo }) {
const $orgId = context().get("orgId");
return loadOne(
{ orgId: $orgId, regNo: $regNo },
// Deliberately not using ioEquivalence here to test stable object/tuple creation
//{ orgId: "orgId", regNo: "orgRegNo" },
loadThingByOrgIdRegNoObjs,
);
},
},
Thing: {
org($thing: LoadedRecordStep<Thing>) {
return loadOne($thing.get("orgId"), "id", loadOrgByIds);
},
},
Org: {
thingByTuple($org: LoadedRecordStep<Org>, { $regNo }) {
const $orgId = $org.get("id");
return loadOne(
[$orgId, $regNo],
["orgId", "orgRegNo"],
loadThingByOrgIdRegNoTuples,
);
},
thingByObj($org: LoadedRecordStep<Org>, { $regNo }) {
const $orgId = $org.get("id");
return loadOne(
{ orgId: $orgId, regNo: $regNo },
{ orgId: "orgId", regNo: "orgRegNo" },
loadThingByOrgIdRegNoObjs,
);
},
},
},
enableDeferStream: true,
Expand Down Expand Up @@ -366,3 +499,101 @@ it("supports no ioEquivalence", async () => {
expect(CALLS[2].specs).to.deep.equal([[1]]);
expect(CALLS[2].attributes).to.deep.equal(["name"]);
});

it("uses stable identifiers to avoid the need for double-fetches (tuple)", async () => {
const source = /* GraphQL */ `
{
t1: thingByOrgIdRegNoTuple(regNo: 987) {
id
name
org {
id
t1: thingByTuple(regNo: 987) {
id
name
}
}
}
}
`;
const schema = makeSchema(false);

CALLS = [];
const result = (await grafast(
{
schema,
source,
contextValue: {
orgId: 27,
},
},
{},
{},
)) as ExecutionResult;
expect(result).to.deep.equal({
data: {
t1: {
id: 2003,
name: "Eye D. Tree",
org: {
id: 27,
t1: {
id: 2003,
name: "Eye D. Tree",
},
},
},
},
});
expect(CALLS).to.have.length(1);
expect(CALLS[0].attributes).to.deep.equal(["id", "name", "orgId"]);
});

it("uses stable identifiers to avoid the need for double-fetches (obj)", async () => {
const source = /* GraphQL */ `
{
t1: thingByOrgIdRegNoObj(regNo: 987) {
id
name
org {
id
t1: thingByObj(regNo: 987) {
id
name
}
}
}
}
`;
const schema = makeSchema(false);

CALLS = [];
const result = (await grafast(
{
schema,
source,
contextValue: {
orgId: 27,
},
},
{},
{},
)) as ExecutionResult;
expect(result).to.deep.equal({
data: {
t1: {
id: 2003,
name: "Eye D. Tree",
org: {
id: 27,
t1: {
id: 2003,
name: "Eye D. Tree",
},
},
},
},
});
expect(CALLS).to.have.length(1);
expect(CALLS[0].attributes).to.deep.equal(["id", "name", "orgId"]);
});
24 changes: 22 additions & 2 deletions grafast/grafast/src/multistep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,37 @@ export type UnwrapMultistep<TMultistepSpec extends Multistep> =
: never;
};

interface MultistepCacheConfig {
identifier: string;
cacheSize: number;
}

export function multistep<const TMultistepSpec extends Multistep>(
spec: TMultistepSpec,
stable?: string | true | MultistepCacheConfig,
): ExecutableStep<UnwrapMultistep<TMultistepSpec>> {
if (spec == null) {
return constant(spec) as any;
} else if (spec instanceof ExecutableStep) {
return spec;
} else if (isTuple(spec)) {
return list(spec) as any;
const config =
stable === true
? { identifier: `multistep` }
: typeof stable === "string"
? { identifier: stable }
: stable;
const $step = list(spec, config);
return $step as any;
} else {
return object(spec) as any;
const config =
stable === true
? { identifier: `multistep` }
: typeof stable === "string"
? { identifier: stable }
: stable;
const $step = object(spec, config);
return $step as any;
}
}

Expand Down
Loading
Loading