alesso-x / relay-connections-deconstructor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Relay Connections Deconstructor

npm

Description

Not all of us love the Relay spec but sometimes we find ourselves wrestling with edges and nodes and edges and nodes and edges and...

Welp, I wrote a quick little function to flatten out said edges and nodes and edges and nodes.

Installation

yarn add relay-connections-deconstructor

or

npm install relay-connections-deconstructor

Usage

import { relayDeconstructor } from 'relay-connections-deconstructor';

const responseFollowingRelaySpec = {
  friends: {
    totalCount: 3,
    edges: [
      {
        node: {
          name: 'Han Solo',
        },
        cursor: 'Y3Vyc29yMg==',
      },
      {
        node: {
          name: 'Leia Organa',
        },
        cursor: 'Y3Vyc29yMw==',
      },
    ],
    pageInfo: {
      endCursor: 'Y3Vyc29yMw==',
      hasNextPage: false,
    },
  },
  planets: {
    totalCount: 2,
    edges: [
      { node: { name: 'Pluto' }, cursor: 'Y3Vyc29yMg==' },
      { node: { name: 'Mars' }, cursor: 'Y3Vyc29yMw==' },
    ],
  },
};

const deconstructedObject = relayDeconstructor(responseFollowingRelaySpec);
console.log(deconstructedObject);
// πŸ‘‡
// {
//   friends: [
//     {
//       name: 'Han Solo',
//       cursor: 'Y3Vyc29yMg==',
//     },
//     {
//       name: 'Leia Organa',
//       cursor: 'Y3Vyc29yMw==',
//     },
//   ],
//   planets: [
//     { cursor: 'Y3Vyc29yMg==', name: 'Pluto' },
//     { cursor: 'Y3Vyc29yMw==', name: 'Mars' },
//   ],
// };

const justTheFriends = relayDeconstructor(responseFollowingRelaySpec.friends);
console.log(justTheFriends);
// πŸ‘‡
// [
//   {
//     name: 'Han Solo',
//     cursor: 'Y3Vyc29yMg==',
//   },
//   {
//     name: 'Leia Organa',
//     cursor: 'Y3Vyc29yMw==',
//   },
// ]

About

License:MIT License


Languages

Language:TypeScript 100.0%