From 447239d60f48a07e284ac26092cddae52cf07c8d Mon Sep 17 00:00:00 2001 From: Jonas Wiesel Date: Mon, 23 Jul 2018 14:26:54 +0200 Subject: [PATCH] feat(build): add getTypeAndIdentifiersFromNodeId(nodeId) (#262) --- packages/graphile-build/src/plugins/NodePlugin.js | 14 ++++++++++++++ packages/graphile-build/src/resolveNode.js | 7 ++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/graphile-build/src/plugins/NodePlugin.js b/packages/graphile-build/src/plugins/NodePlugin.js index a3ec50af8..bc51bdb87 100644 --- a/packages/graphile-build/src/plugins/NodePlugin.js +++ b/packages/graphile-build/src/plugins/NodePlugin.js @@ -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, @@ -30,6 +31,12 @@ export type BuildExtensionNode = {| Type: GraphQLType, ...identifiers: Array ): string, + getTypeAndIdentifiersFromNodeId( + nodeId: string + ): { + Type: GraphQLType, + identifiers: Array, + }, addNodeFetcherForTypeName(typeName: string, fetcher: NodeFetcher): void, getNodeAlias(typeName: string): string, getNodeType(alias: string): GraphQLType, @@ -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"); diff --git a/packages/graphile-build/src/resolveNode.js b/packages/graphile-build/src/resolveNode.js index 27ed35e84..3f78ce216 100644 --- a/packages/graphile-build/src/resolveNode.js +++ b/packages/graphile-build/src/resolveNode.js @@ -1,5 +1,3 @@ -const base64Decode = str => new Buffer(String(str), "base64").toString("utf8"); - export default async function resolveNode( nodeId, build, @@ -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"); }