UNPKG

graphql-compose-relay

Version:

Plugin for `graphql-compose` which wraps graphql types with Relay specific logic.

40 lines (33 loc) 932 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.base64 = base64; exports.unbase64 = unbase64; exports.toGlobalId = toGlobalId; exports.fromGlobalId = fromGlobalId; function base64(i) { return Buffer.from(i, 'ascii').toString('base64'); } function unbase64(i) { return Buffer.from(i, 'base64').toString('ascii'); } /** * Takes a type name and an ID specific to that type name, and returns a * "global ID" that is unique among all types. */ function toGlobalId(type, id) { return base64([type, id].join(':')); } /** * Takes the "global ID" created by toGlobalID, and returns the type name and ID * used to create it. */ function fromGlobalId(globalId) { const unbasedGlobalId = unbase64(globalId); const delimiterPos = unbasedGlobalId.indexOf(':'); return { type: unbasedGlobalId.substring(0, delimiterPos), id: unbasedGlobalId.substring(delimiterPos + 1) }; }