How to use the graphql-relay.cursorToOffset function in graphql-relay

To help you get started, we’ve selected a few graphql-relay examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github acarl005 / join-monster / src / stringifiers / pg.js View on Github external
if (direction !== 'ASC' && direction !== 'DESC') {
        throw new Error (direction + ' is not a valid sorting direction')
      }
      orderColumns[column] = direction
    }
  } else if (typeof node.orderBy === 'string') {
    orderColumns[node.orderBy] = 'ASC'
  } else {
    throw new Error('"orderBy" is required for pagination')
  }
  let limit = 'ALL', offset = 0
  if (node.args && node.args.first) {
    // we'll get one extra item (hence the +1). this is to determine if there is a next page or not
    limit = parseInt(node.args.first) + 1
    if (node.args.after) {
      offset = cursorToOffset(node.args.after) + 1
    }
  }
  return { limit, offset, orderColumns }
}
github soonlive / relay-cart / data / types / productListType.js View on Github external
resolve: ({}, args) => {
        logger.info('Resolving queryProductList with params:', { args });
        const start = args.after ? cursorToOffset(args.after) + 1 : 0;
        const size = (args.first || 8) + 1;

        const result = productService.findAll({ start, size });

        // support pagination
        const array = args.after ? new Array(start).concat(result.items) : result.items;

        return connectionFromArray(
          array,
          args,
        );
      },
    },
github acarl005 / join-monster / src / array-to-connection.js View on Github external
const cursor = {}
        const key = sortKey.key
        for (let column of wrap(key)) {
          cursor[column] = obj[column]
        }
        return { cursor: objToCursor(cursor), node: obj }
      })
      if (data.length) {
        pageInfo.startCursor = edges[0].cursor
        pageInfo.endCursor = last(edges).cursor
      }
      return { edges, pageInfo, _paginated: true }
    } else if (sqlAST.orderBy || (sqlAST.junction && sqlAST.junction.orderBy)) {
      let offset = 0
      if (idx(sqlAST, _ => _.args.after)) {
        offset = cursorToOffset(sqlAST.args.after) + 1
      }
      // $total was a special column for determining the total number of items
      const arrayLength = data[0] && parseInt(data[0].$total, 10)
      const connection = connectionFromArraySlice(data, sqlAST.args || {}, { sliceStart: offset, arrayLength })
      connection.total = arrayLength || 0
      connection._paginated = true
      return connection
    }
  }
  return data
}
github kazekyo / nestjs-graphql-relay / src / common / connection-paging.ts View on Github external
export function getPagingParameters(args: ConnectionArgs) {
  const meta = getMeta(args);

  switch (meta.pagingType) {
    case 'forward': {
      return {
        limit: meta.first,
        offset: meta.after ? Relay.cursorToOffset(meta.after) + 1 : 0,
      };
    }
    case 'backward': {
      const { last, before } = meta;
      let limit = last;
      let offset = Relay.cursorToOffset(before!) - last;

      // Check to see if our before-page is underflowing past the 0th item
      if (offset < 0) {
        // Adjust the limit with the underflow value
        limit = Math.max(last + offset, 0);
        offset = 0;
      }

      return { offset, limit };
    }
    default:
      return {};
  }
}
github kazekyo / nestjs-graphql-relay / src / common / connection-paging.ts View on Github external
export function getPagingParameters(args: ConnectionArgs) {
  const meta = getMeta(args);

  switch (meta.pagingType) {
    case 'forward': {
      return {
        limit: meta.first,
        offset: meta.after ? Relay.cursorToOffset(meta.after) + 1 : 0,
      };
    }
    case 'backward': {
      const { last, before } = meta;
      let limit = last;
      let offset = Relay.cursorToOffset(before!) - last;

      // Check to see if our before-page is underflowing past the 0th item
      if (offset < 0) {
        // Adjust the limit with the underflow value
        limit = Math.max(last + offset, 0);
        offset = 0;
      }

      return { offset, limit };
    }
github acarl005 / join-monster / src / stringifiers / mysql.js View on Github external
if (direction !== 'ASC' && direction !== 'DESC') {
        throw new Error (direction + ' is not a valid sorting direction')
      }
      orderColumns[column] = direction
    }
  } else if (typeof node.orderBy === 'string') {
    orderColumns[node.orderBy] = 'ASC'
  } else {
    throw new Error('"orderBy" is required for pagination')
  }
  let limit = '18446744073709551615', offset = 0
  if (node.args && node.args.first) {
    // we'll get one extra item (hence the +1). this is to determine if there is a next page or not
    limit = parseInt(node.args.first) + 1
    if (node.args.after) {
      offset = cursorToOffset(node.args.after) + 1
    }
  }
  return { limit, offset, orderColumns }
}
function orderColumnsToString(orderColumns) {
github parse-community / parse-server / src / GraphQL / helpers / objectsQueries.js View on Github external
let needToPreCount = false;

  // Validates the skip input
  if (skipInput || skipInput === 0) {
    if (skipInput < 0) {
      throw new Parse.Error(
        Parse.Error.INVALID_QUERY,
        'Skip should be a positive number'
      );
    }
    skip = skipInput;
  }

  // Validates the after param
  if (after) {
    after = cursorToOffset(after);
    if ((!after && after !== 0) || after < 0) {
      throw new Parse.Error(
        Parse.Error.INVALID_QUERY,
        'After is not a valid cursor'
      );
    }

    // If skip and after are passed, a new skip is calculated by adding them
    skip = (skip || 0) + (after + 1);
  }

  // Validates the first param
  if (first || first === 0) {
    if (first < 0) {
      throw new Parse.Error(
        Parse.Error.INVALID_QUERY,