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

resolver helper #1867

Merged
merged 3 commits into from
Dec 29, 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
4 changes: 2 additions & 2 deletions ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ts/src/graphql/node_resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ class SearchResult {
class SearchResultResolver implements NodeResolver {
encode(result: SearchResult) {
const str = `searchResult:${result.data.context}:${result.id}`;
return Buffer.from(str, "ascii").toString("base64");
return btoa(str);
}

async decodeObj(viewer: Viewer, id: string) {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3) {
return null;
Expand Down
16 changes: 9 additions & 7 deletions ts/src/graphql/node_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ interface loadEnt {
(v: Viewer, nodeType: string, id: ID): Promise<Ent | null>;
}

function encodeHelper(nodeType: string, id: ID): string {
return btoa(`node:${nodeType}:${id}`);
}

export class EntNodeResolver implements NodeResolver {
constructor(private loader: loadEnt) {}

encode(node: Ent): string {
// let's do 3 parts. we take the "node" prefix
const str = `node:${node.nodeType}:${node.id}`;
return Buffer.from(str, "ascii").toString("base64");
return encodeHelper(node.nodeType, node.id);
}

static decode(id: string): ID | null {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3) {
return null;
Expand All @@ -34,7 +37,7 @@ export class EntNodeResolver implements NodeResolver {
}

mustDecode(id: string): [ID, string] {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3) {
throw new Error(`invalid id ${id} passed to EntNodeResolver`);
Expand All @@ -43,7 +46,7 @@ export class EntNodeResolver implements NodeResolver {
}

async decodeObj(viewer: Viewer, id: string): Promise<Node | null> {
const decoded = Buffer.from(id, "base64").toString("ascii");
const decoded = atob(id);
let parts = decoded.split(":");
if (parts.length != 3 || parts[0] != "node") {
return null;
Expand Down Expand Up @@ -117,6 +120,5 @@ export function mustDecodeNullableIDFromGQLID(
// This takes an ent and returns the graphql id
export function encodeGQLID(node: Ent): string {
// let's do 3 parts. we take the "node" prefix
const str = `node:${node.nodeType}:${node.id}`;
return Buffer.from(str, "ascii").toString("base64");
return btoa(`node:${node.nodeType}:${node.id}`);
}
Loading