SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @flow
  2. import qs from 'query-string';
  3.  
  4. import type { ConnectionArguments } from 'graphql-relay';
  5.  
  6. import * as ConnectionUtil from '../ConnectionUtil';
  7.  
  8. import type { GraphQLContext, Edge } from '../../TypeDefinition';
  9. import type { OffsetsOptions, Offsets, PageInfoOptions, PageInfo } from '../ConnectionUtil';
  10.  
  11. import api from './api';
  12.  
  13. export const PREFIX = 'rest:';
  14.  
  15. export const cursorToOffset = (cursor: string): number => ConnectionUtil.cursorToOffset(PREFIX, cursor);
  16.  
  17. export const offsetToCursor = (offset: number): string => ConnectionUtil.offsetToCursor(PREFIX, offset);
  18.  
  19. export const getOffsetWithDefault = (cursor: string, defaultOffset: number): number =>
  20.   ConnectionUtil.getOffsetWithDefault(PREFIX, cursor, defaultOffset);
  21.  
  22. export const calculateOffsets = (options: OffsetsOptions): Offsets => ConnectionUtil.calculateOffsets(PREFIX, options);
  23.  
  24. export const getPageInfo = ({
  25.   edges,
  26.   before,
  27.   after,
  28.   first,
  29.   last,
  30.   afterOffset,
  31.   beforeOffset,
  32.   startOffset,
  33.   endOffset,
  34.   totalCount,
  35. }: PageInfoOptions): PageInfo => {
  36.   const firstEdge = edges[0];
  37.   const lastEdge = edges[edges.length - 1];
  38.   const lowerBound = after ? afterOffset + 1 : 0;
  39.   const upperBound = before ? Math.min(beforeOffset, totalCount) : totalCount;
  40.  
  41.   return {
  42.     startCursor: firstEdge ? firstEdge.cursor : null,
  43.     endCursor: lastEdge ? lastEdge.cursor : null,
  44.     hasPreviousPage: last !== null ? startOffset > lowerBound : false,
  45.     hasNextPage: first !== null ? endOffset < upperBound : false,
  46.   };
  47. };
  48.  
  49. // TODO - implement this
  50. type ConnectionOptions = {
  51.   // Endpoint URL
  52.   endpoint: String,
  53.   // Headers
  54.   headers?: Object,
  55.   // QueryString
  56.   query?: Object,
  57.   // API params
  58.   params?: {
  59.     // Endpoint URL
  60.     endpoint: String,
  61.     // Headers
  62.     headers: Object,
  63.     // QueryString
  64.     query?: Object,
  65.   },
  66.   // GraphQL context
  67.   context: GraphQLContext,
  68.   // Connection Args
  69.   args: ConnectionArguments,
  70.   // Loader to load individually objects
  71.   loader: (context: GraphQLContext, id: string) => Object,
  72.   // data from api already loaded
  73.   edges?: Edge[],
  74. };
  75. const connectionFromRest = async ({
  76.   context,
  77.   loader,
  78.   args = {},
  79.   callApi: (skip: number, limit: number) => Promise<any>  
  80. }: ConnectionOptions) => {
  81.   const totalCount = 10000;
  82.   const {
  83.     first,
  84.     last,
  85.     before,
  86.     after,
  87.     skip: _skip,
  88.     limit: _limit,
  89.     beforeOffset,
  90.     afterOffset,
  91.     startOffset,
  92.     endOffset,
  93.   } = calculateOffsets({ args, totalCount });
  94.  
  95.   // TODO - use skip and limit here
  96.   try {
  97.     const data = await callApi(skip, limit);
  98.  
  99.     const edges = data.map((value, index) => ({
  100.       cursor: offsetToCursor(startOffset + index),
  101.       // TODO - not sure if this is needed
  102.       node: loader(context, value),
  103.     }));
  104.  
  105.     return {
  106.       edges,
  107.       count: totalCount,
  108.       pageInfo: getPageInfo({
  109.         edges,
  110.         before,
  111.         after,
  112.         first,
  113.         last,
  114.         afterOffset,
  115.         beforeOffset,
  116.         startOffset,
  117.         endOffset,
  118.         totalCount,
  119.       }),
  120.     };
  121.   } catch (err) {
  122.     console.log(new Error('ConnectionFromRest: ', err));
  123.     return null;
  124.   }
  125. };
  126.  
  127. export default connectionFromRest;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top