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

Add getNodeIdFromTypeAndIdentifiers(nodeId) to build #262

Merged
merged 11 commits into from
Jul 23, 2018
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
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