src/query/query.js
async function connectionFromModel(graffitiModel, args, context, info) {
  const Collection = graffitiModel.model;
  if (!Collection) {
    return emptyConnection();
  }

  const { before, after, first, last, id, orderBy = { _id: 1 }, ...selector } = args;

  const begin = getId(after);
  const end = getId(before);

  const offset = (first - last) || 0;
  const limit = last || first;

  if (id) {
    selector.id = id;
  }

  if (begin) {
    selector._id = selector._id || {};
    selector._id.$gt = begin;
  }

  if (end) {
    selector._id = selector._id || {};
    selector._id.$lt = end;
  }

  const result = await getList(Collection, selector, {
    limit,
    skip: offset,
    sort: orderBy
  }, context, info);
  const count = await getCount(Collection, selector);

  if (result.length === 0) {
    return emptyConnection();
  }

  const edges = result.map((value) => ({
    cursor: idToCursor(value._id),
    node: value
  }));

  const firstElement = await getFirst(Collection);
  return {
    count,
    edges,
    pageInfo: {
      startCursor: edges[0].cursor,
      endCursor: edges[edges.length - 1].cursor,
      hasPreviousPage: cursorToId(edges[0].cursor) !== firstElement._id.toString(),
      hasNextPage: result.length === limit
    }
  };
}
async function connectionFromModel(graffitiModel, args, context, info) {
  const Collection = graffitiModel.model;
  if (!Collection) {
    return emptyConnection();
  }

  const { before, after, first, last, id, orderBy = { _id: 1 }, ...selector } = args;

  const begin = getId(after);
  const end = getId(before);

  const offset = (first - last) || 0;
  const limit = last || first;

  if (id) {
    selector.id = id;
  }

  if (begin) {
    selector._id = selector._id || {};
    selector._id.$gt = begin;
  }

  if (end) {
    selector._id = selector._id || {};
    selector._id.$lt = end;
  }

  const result = await getList(Collection, selector, {
    limit,
    skip: offset,
    sort: orderBy
  }, context, info);
  const count = await getCount(Collection, selector);

  if (result.length === 0) {
    return emptyConnection();
  }

  const edges = result.map((value) => ({
    cursor: idToCursor(value._id),
    node: value
  }));

  const firstElement = await getFirst(Collection);
  return {
    count,
    edges,
    pageInfo: {
      startCursor: edges[0].cursor,
      endCursor: edges[edges.length - 1].cursor,
      hasPreviousPage: cursorToId(edges[0].cursor) !== firstElement._id.toString(),
      hasNextPage: result.length === limit
    }
  };
}
async function connectionFromModel ( graffitiModel , args , context , info ) { const Collection = graffitiModel . model ; if ( ! Collection ) { return emptyConnection ( ) ; } const { before , after , first , last , id , orderBy = { _id : 1 } , ... selector } = args ; const begin = getId ( after ) ; const end = getId ( before ) ; const offset = ( first - last ) || 0 ; const limit = last || first ; if ( id ) { selector . id = id ; } if ( begin ) { selector . _id = selector . _id || { } ; selector . _id . $gt = begin ; } if ( end ) { selector . _id = selector . _id || { } ; selector . _id . $lt = end ; } const result = await getList ( Collection , selector , { limit , skip : offset , sort : orderBy } , context , info ) ; const count = await getCount ( Collection , selector ) ; if ( result . length === 0 ) { return emptyConnection ( ) ; } const edges = result . map ( ( value ) => ( { cursor : idToCursor ( value . _id ) , node : value } ) ) ; const firstElement = await getFirst ( Collection ) ; return { count , edges , pageInfo : { startCursor : edges [ 0 ] . cursor , endCursor : edges [ edges . length - 1 ] . cursor , hasPreviousPage : cursorToId ( edges [ 0 ] . cursor ) !== firstElement . _id . toString ( ) , hasNextPage : result . length === limit } } ; }
Returns a connection based on a graffitiModel
Returns a connection based on a graffitiModel