1"use strict";(self.webpackChunk_availity_dinosaurdocs=self.webpackChunk_availity_dinosaurdocs||[]).push([[1681],{9134(e,n,l){l.r(n),l.d(n,{assets:()=>r,contentTitle:()=>t,default:()=>h,frontMatter:()=>a,metadata:()=>i,toc:()=>o});const i=JSON.parse('{"id":"resources/relay-id","title":"Relay ID","description":"Small package containing helpers for encoding and decoding IDs according to the Relay GraphQL specification.","source":"@site/docs/resources/relay-id.md","sourceDirName":"resources","slug":"/resources/relay-id","permalink":"/sdk-js/resources/relay-id","draft":false,"unlisted":false,"editUrl":"https://github.com/availity/sdk-js/edit/master/docusaurus/docs/resources/relay-id.md","tags":[],"version":"current","frontMatter":{"title":"Relay ID"},"sidebar":"someSidebar","previous":{"title":"Resolving URLs","permalink":"/sdk-js/resources/resolve-url"},"next":{"title":"User Activity Broadcaster","permalink":"/sdk-js/resources/user-activity-broadcaster"}}');var s=l(4848),d=l(8453);const a={title:"Relay ID"},t=void 0,r={},o=[{value:"When to Use This",id:"when-to-use-this",level:2},{value:"Installation",id:"installation",level:2},{value:"NPM",id:"npm",level:3},{value:"Yarn",id:"yarn",level:3},{value:"Usage",id:"usage",level:2},{value:"API",id:"api",level:2},{value:"<code>toGlobalId(type, id)</code>",id:"toglobalidtype-id",level:3},{value:"<code>fromGlobalId(globalId)</code>",id:"fromglobalidglobalid",level:3},{value:"<code>base64(input)</code>",id:"base64input",level:3},{value:"<code>unbase64(encoded)</code>",id:"unbase64encoded",level:3},{value:"Example: Decoding API Responses",id:"example-decoding-api-responses",level:2},{value:"Example: Building Relay-Compatible IDs",id:"example-building-relay-compatible-ids",level:2}];function c(e){const n={a:"a",code:"code",h2:"h2",h3:"h3",img:"img",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,d.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.p,{children:["Small package containing helpers for encoding and decoding IDs according to the ",(0,s.jsx)(n.a,{href:"https://relay.dev/graphql/objectidentification.htm",children:"Relay GraphQL specification"}),"."]}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.a,{href:"https://www.npmjs.com/package/@availity/relay-id",children:(0,s.jsx)(n.img,{src:"https://img.shields.io/npm/v/@availity/relay-id.svg?style=for-the-badge",alt:"Version"})})}),"\n",(0,s.jsx)(n.h2,{id:"when-to-use-this",children:"When to Use This"}),"\n",(0,s.jsxs)(n.p,{children:["Use ",(0,s.jsx)(n.code,{children:"@availity/relay-id"})," when:"]}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["A backend returns Relay-style global IDs. Some GraphQL or REST APIs encode entity type and ID into a single opaque base64 string (e.g., ",(0,s.jsx)(n.code,{children:"VXNlcjoxMjM="}),"). This package lets you decode them to extract the underlying type and ID."]}),"\n",(0,s.jsx)(n.li,{children:"You need to construct Relay-compatible IDs. When building a BFF (Backend for Frontend) or adapter layer that must produce Relay-compatible global IDs from raw database identifiers."}),"\n",(0,s.jsx)(n.li,{children:"You are debugging API responses. Quickly decode opaque IDs during development to understand what entity and record they reference."}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"installation",children:"Installation"}),"\n",(0,s.jsx)(n.h3,{id:"npm",children:"NPM"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-bash",children:"npm install @availity/relay-id\n"})}),"\n",(0,s.jsx)(n.h3,{id:"yarn",children:"Yarn"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-bash",children:"yarn add @availity/relay-id\n"})}),"\n",(0,s.jsx)(n.h2,{id:"usage",children:"Usage"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",children:"import { toGlobalId, fromGlobalId, base64, unbase64 } from '@availity/relay-id';\n\n// Encode a type and ID into a global Relay ID\nconst globalId = toGlobalId('User', '789');\n// => 'VXNlcjo3ODk='\n\n// Decode a global Relay ID back to its parts\nconst { type, id } = fromGlobalId('VXNlcjo3ODk=');\n// => { type: 'User', id: '789' }\n"})}),"\n",(0,s.jsx)(n.h2,{id:"api",children:"API"}),"\n",(0,s.jsx)(n.h3,{id:"toglobalidtype-id",children:(0,s.jsx)(n.code,{children:"toGlobalId(type, id)"})}),"\n",(0,s.jsx)(n.p,{children:"Encodes a type name and ID into a base64 Relay global ID."}),"\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{children:"Parameter"}),(0,s.jsx)(n.th,{children:"Type"}),(0,s.jsx)(n.th,{children:"Description"})]})}),(0,s.jsxs)(n.tbody,{children:[(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"type"})}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"string"})}),(0,s.jsxs)(n.td,{children:["The GraphQL type name (e.g., ",(0,s.jsx)(n.code,{children:"'User'"}),", ",(0,s.jsx)(n.code,{children:"'Organization'"}),")"]})]}),(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"id"})}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"string"})}),(0,s.jsx)(n.td,{children:"The underlying entity ID"})]})]})]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Returns:"})," ",(0,s.jsx)(n.code,{children:"string"})," \u2014 A base64-encoded string in the format ",(0,s.jsx)(n.code,{children:'Base64("Type:id")'})]}),"\n",(0,s.jsx)(n.h3,{id:"fromglobalidglobalid",children:(0,s.jsx)(n.code,{children:"fromGlobalId(globalId)"})}),"\n",(0,s.jsx)(n.p,{children:"Decodes a Relay global ID back to its type and ID components."}),"\n",(0,s.jsxs)(n.table,{children:[(0,s.jsx)(n.thead,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.th,{children:"Parameter"}),(0,s.jsx)(n.th,{children:"Type"}),(0,s.jsx)(n.th,{children:"Description"})]})}),(0,s.jsx)(n.tbody,{children:(0,s.jsxs)(n.tr,{children:[(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"globalId"})}),(0,s.jsx)(n.td,{children:(0,s.jsx)(n.code,{children:"string"})}),(0,s.jsx)(n.td,{children:"A base64-encoded Relay global ID"})]})})]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Returns:"})," ",(0,s.jsx)(n.code,{children:"{ type: string, id: string }"})]}),"\n",(0,s.jsx)(n.h3,{id:"base64input",children:(0,s.jsx)(n.code,{children:"base64(input)"})}),"\n",(0,s.jsx)(n.p,{children:"Low-level utility to base64-encode a string."}),"\n",(0,s.jsx)(n.h3,{id:"unbase64encoded",children:(0,s.jsx)(n.code,{children:"unbase64(encoded)"})}),"\n",(0,s.jsx)(n.p,{children:"Low-level utility to decode a base64 string."}),"\n",(0,s.jsx)(n.h2,{id:"example-decoding-api-responses",children:"Example: Decoding API Responses"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",children:"import { fromGlobalId } from '@availity/relay-id';\n\n// An API returns a list of items with opaque IDs\nconst items = await fetchItems();\n\nfor (const item of items) {\n const { type, id } = fromGlobalId(item.id);\n console.log(`${type} #${id}`); // e.g., \"Claim #4567\"\n}\n"})}),"\n",(0,s.jsx)(n.h2,{id:"example-building-relay-compatible-ids",children:"Example: Building Relay-Compatible IDs"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",children:"import { toGlobalId } from '@availity/relay-id';\n\n// Your service has a raw database record\nconst dbRecord = { table: 'providers', id: '42' };\n\n// Encode it as a global ID for a GraphQL response\nconst globalId = toGlobalId('Provider', dbRecord.id);\n// => 'UHJvdmlkZXI6NDI='\n"})})]})}function h(e={}){const{wrapper:n}={...(0,d.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(c,{...e})}):c(e)}},8453(e,n,l){l.d(n,{R:()=>a,x:()=>t});var i=l(6540);const s={},d=i.createContext(s);function a(e){const n=i.useContext(d);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function t(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:a(e.components),i.createElement(d.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.