How to use the graphql-relay.offsetToCursor 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 bfwg / relay-gallery / schema / schema.js View on Github external
.spread((allImages, newImage) => {
            const newImageStr = JSON.stringify(newImage);
            /* If edge is in list return index */
            const offset = allImages.reduce((pre, ele, idx) => {
              if (JSON.stringify(ele) === newImageStr) {
                return idx;
              }
              return pre;
            }, -1);

            return {
              cursor: offset !== -1 ? Relay.offsetToCursor(offset) : null,
              node: newImage,
            };
          });
        });
github bfwg / relay-gallery / schema / schema.js View on Github external
.spread((allImages, newImage) => {
            const newImageStr = JSON.stringify(newImage);
            /* If edge is in list return index */
            const offset = allImages.reduce((pre, ele, idx) => {
              if (JSON.stringify(ele) === newImageStr) {
                return idx;
              }
              return pre;
            }, -1);

            return {
              cursor: offset !== -1 ? Relay.offsetToCursor(offset) : null,
              node: newImage,
            };
          });
        });
github acarl005 / join-monster / test / pagination / offset-paging.js View on Github external
test('should handle pagination at the root', async t => {
  const query = makeUsersQuery()
  const { data, errors } = await run(query)
  errCheck(t, errors)
  t.deepEqual(data.users.pageInfo, {
    hasNextPage: false,
    startCursor: offsetToCursor(0),
    endCursor: offsetToCursor(5)
  })
  t.deepEqual(data.users.edges[0], {
    cursor: offsetToCursor(0),
    node: {
      id: toGlobalId('User', 1),
      fullName: 'Alivia Waelchi',
      email: 'Mohammed.Hayes@hotmail.com'
    }
  })
  t.is(data.users.edges.last().cursor, data.users.pageInfo.endCursor)
})
github acarl005 / join-monster / test / pagination / offset-paging.js View on Github external
test('nested paging should handle "first" and "after" args that reaches the last page', async t => {
  const query = makePostsQuery({ first: 5, after: offsetToCursor(3) })
  const { data, errors } = await run(query)
  errCheck(t, errors)
  const posts = data.user.posts
  t.is(posts.total, 8)
  t.deepEqual(posts.pageInfo, {
    hasNextPage: false,
    startCursor: offsetToCursor(4),
    endCursor: offsetToCursor(7)
  })
  t.is(posts.edges.length, 4)
  t.is(posts.edges.last().cursor, posts.pageInfo.endCursor)
})
github acarl005 / join-monster / test / relay.js View on Github external
import test from 'ava'
import { graphql } from 'graphql'
import { toGlobalId, offsetToCursor } from 'graphql-relay'
import schemaRelay from '../test-api/schema-paginated/index'
import { partial } from 'lodash'
import { errCheck } from './_util'

const run = partial(graphql, schemaRelay)

const user1Id = toGlobalId('User', 1)
const cursor0 = offsetToCursor(0)

test('it should get a globalId', async t => {
  const query = `{
    user(id:1) { id }
  }`
  const { data, errors } = await run(query)
  errCheck(t, errors)
  const expect = { user: { id: user1Id } }
  t.deepEqual(expect, data)
})

test('it should fetch a Node type with inline fragments', async t => {
  const query = `{
    node(id: "${toGlobalId('Post', 1)}") {
      ... on Post { body }
    }
github acarl005 / join-monster / test / pagination / offset-paging.js View on Github external
test('nested paging should handle "first" and "after" args that reaches the last page', async t => {
  const query = makePostsQuery({ first: 5, after: offsetToCursor(3) })
  const { data, errors } = await run(query)
  errCheck(t, errors)
  const posts = data.user.posts
  t.is(posts.total, 8)
  t.deepEqual(posts.pageInfo, {
    hasNextPage: false,
    startCursor: offsetToCursor(4),
    endCursor: offsetToCursor(7)
  })
  t.is(posts.edges.length, 4)
  t.is(posts.edges.last().cursor, posts.pageInfo.endCursor)
})
github acarl005 / join-monster / test / pagination / offset-paging.js View on Github external
}
                  }
                }
              }
            }
          }
        }
      }
    }
  }`
  const { data, errors } = await run(query)
  errCheck(t, errors)
  const comments = data.users.edges[0].node.posts.edges[0].node.comments
  t.deepEqual(comments.pageInfo, {
    hasNextPage: true,
    startCursor: offsetToCursor(0),
    endCursor: offsetToCursor(2)
  })
  t.is(comments.edges.length, 3)
  t.deepEqual(comments.edges[0], {
    cursor: offsetToCursor(0),
    node: {
      id: toGlobalId('Comment', 18),
      body: 'bypassing the hard drive won\'t do anything, we need to back up the primary EXE bandwidth!',
      author: {
        fullName: 'Coleman Abernathy'
      }
    }
  })
  t.is(comments.edges.last().cursor, comments.pageInfo.endCursor)
})
github parse-community / parse-server / src / GraphQL / helpers / objectsQueries.js View on Github external
}

  let edges = null;
  let pageInfo = null;
  if (results) {
    edges = results.map((result, index) => ({
      cursor: offsetToCursor((skip || 0) + index),
      node: result,
    }));

    pageInfo = {
      hasPreviousPage:
        ((preCount && preCount > 0) || (count && count > 0)) &&
        skip !== undefined &&
        skip > 0,
      startCursor: offsetToCursor(skip || 0),
      endCursor: offsetToCursor((skip || 0) + (results.length || 1) - 1),
      hasNextPage: (preCount || count) > (skip || 0) + results.length,
    };
  }

  return {
    edges,
    pageInfo,
    count: preCount || count,
  };
};
github tubackkhoa / tkframework / server / data / graphql / paging / getPagingModel.js View on Github external
const edges = rows.map((value, index) => ({    
    node: value,
    cursor: offsetToCursor(startOffset + index),
  }))
github OpenCTI-Platform / opencti / opencti-platform / opencti-graphql / src / database / utils.js View on Github external
mapObjIndexed((record, key) => {
      const { node } = record;
      const { relation } = record;
      const nodeOffset = offset + parseInt(key, 10) + 1;
      return { node, relation, cursor: offsetToCursor(nodeOffset) };
    }),
    values