How to use the graphql-relay.getOffsetWithDefault 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 exogen / graphbrainz / src / resolvers.js View on Github external
export function resolveBrowse(
  root,
  { first, after, type = [], status = [], discID, isrc, iswc, ...args },
  { loaders },
  info
) {
  const pluralName = toDashed(info.fieldName)
  const singularName = toSingular(pluralName)
  let params = {
    ...args,
    type,
    status,
    limit: first,
    offset: getOffsetWithDefault(after, -1) + 1 || undefined
  }
  params = includeSubqueries(params, info)
  params = includeRelationships(params, info, info.fragments)
  const formatParam = value => value.toLowerCase().replace(/ /g, '')
  params.type = params.type.map(formatParam)
  params.status = params.status.map(formatParam)
  let request
  if (discID) {
    request = loaders.lookup.load(['discid', discID, params])
    // If fetching releases by disc ID, they will already include the `media`
    // and `discids` subqueries, and it is invalid to specify them.
    if (params.inc) {
      params.inc = params.inc.filter(value => {
        return value !== 'media' && value !== 'discids'
      })
    }
github exogen / graphbrainz / src / resolvers.js View on Github external
export function resolveSearch(
  root,
  { after, first, query, ...args },
  { loaders },
  info
) {
  const pluralName = toDashed(info.fieldName)
  const singularName = toSingular(pluralName)
  let params = {
    ...args,
    limit: first,
    offset: getOffsetWithDefault(after, -1) + 1 || undefined
  }
  params = includeSubqueries(params, info)
  return loaders.search.load([singularName, query, params]).then(list => {
    const {
      [pluralName]: arraySlice,
      offset: sliceStart,
      count: arrayLength
    } = list
    const meta = { sliceStart, arrayLength }
    const connection = connectionFromArraySlice(
      arraySlice,
      { first, after },
      meta
    )
    // Move the `score` field up to the edge object and make sure it's a
    // number (MusicBrainz returns a string).
github hanpama / girin / tests / starwars-relay / ArrayConnection.ts View on Github external
constructor(protected array: TEdgeSource[], args: ConnectionArguments) {
    super(args);

    const { after, before, first, last } = args;

    const arrayLength = array.length;
    const beforeOffset = getOffsetWithDefault(before, arrayLength);
    const afterOffset = getOffsetWithDefault(after, -1);

    let startOffset = Math.max(afterOffset + 1, 0);
    let endOffset = Math.min(beforeOffset, arrayLength);
    if (typeof first === 'number') {
      endOffset = Math.min(endOffset, startOffset + first);
    }
    if (typeof last === 'number') {
      startOffset = Math.max(startOffset, endOffset - last);
    }
    this.startOffset = startOffset;
    this.endOffset = endOffset;
    this.afterOffset = afterOffset;
    this.beforeOffset = beforeOffset;
  }
github hanpama / girin / tests / starwars-relay / ArrayConnection.ts View on Github external
constructor(protected array: TEdgeSource[], args: ConnectionArguments) {
    super(args);

    const { after, before, first, last } = args;

    const arrayLength = array.length;
    const beforeOffset = getOffsetWithDefault(before, arrayLength);
    const afterOffset = getOffsetWithDefault(after, -1);

    let startOffset = Math.max(afterOffset + 1, 0);
    let endOffset = Math.min(beforeOffset, arrayLength);
    if (typeof first === 'number') {
      endOffset = Math.min(endOffset, startOffset + first);
    }
    if (typeof last === 'number') {
      startOffset = Math.max(startOffset, endOffset - last);
    }
    this.startOffset = startOffset;
    this.endOffset = endOffset;
    this.afterOffset = afterOffset;
    this.beforeOffset = beforeOffset;
  }