Skip to content

Commit

Permalink
feat(build): add getTypeAndIdentifiersFromNodeId(nodeId) (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
wieseljonas authored and benjie committed Jul 23, 2018
1 parent a65da8e commit 447239d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 14 additions & 0 deletions packages/graphile-build/src/plugins/NodePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { GraphQLType, GraphQLInterfaceType } from "graphql";
import type { BuildExtensionQuery } from "./QueryPlugin";

const base64 = str => new Buffer(String(str)).toString("base64");
const base64Decode = str => new Buffer(String(str), "base64").toString("utf8");

export type NodeFetcher = (
data: mixed,
Expand All @@ -30,6 +31,12 @@ export type BuildExtensionNode = {|
Type: GraphQLType,
...identifiers: Array<mixed>
): string,
getTypeAndIdentifiersFromNodeId(
nodeId: string
): {
Type: GraphQLType,
identifiers: Array<mixed>,
},
addNodeFetcherForTypeName(typeName: string, fetcher: NodeFetcher): void,
getNodeAlias(typeName: string): string,
getNodeType(alias: string): GraphQLType,
Expand Down Expand Up @@ -60,6 +67,13 @@ export default (function NodePlugin(
JSON.stringify([this.getNodeAlias(Type), ...identifiers])
);
},
getTypeAndIdentifiersFromNodeId(nodeId) {
const [alias, ...identifiers] = JSON.parse(base64Decode(nodeId));
return {
Type: this.getTypeByName(nodeTypeNameByAlias[alias] || alias),
identifiers,
};
},
addNodeFetcherForTypeName(typeName, fetcher) {
if (nodeFetcherByTypeName[typeName]) {
throw new Error("There's already a fetcher for this type");
Expand Down
7 changes: 2 additions & 5 deletions packages/graphile-build/src/resolveNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const base64Decode = str => new Buffer(String(str), "base64").toString("utf8");

export default async function resolveNode(
nodeId,
build,
Expand All @@ -13,15 +11,14 @@ export default async function resolveNode(
$$nodeType,
parseResolveInfo,
nodeFetcherByTypeName,
getNodeType,
getTypeAndIdentifiersFromNodeId,
graphql: { getNamedType },
} = build;
if (nodeId === "query") {
return $$isQuery;
}
try {
const [alias, ...identifiers] = JSON.parse(base64Decode(nodeId));
const Type = getNodeType(alias);
const { Type, identifiers } = getTypeAndIdentifiersFromNodeId(nodeId);
if (!Type) {
throw new Error("Type not found");
}
Expand Down

0 comments on commit 447239d

Please sign in to comment.