arrayConnection.d.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type {
  2. Connection,
  3. ConnectionArguments,
  4. ConnectionCursor,
  5. } from './connection';
  6. interface ArraySliceMetaInfo {
  7. sliceStart: number;
  8. arrayLength: number;
  9. }
  10. /**
  11. * A simple function that accepts an array and connection arguments, and returns
  12. * a connection object for use in GraphQL. It uses array offsets as pagination,
  13. * so pagination will only work if the array is static.
  14. */
  15. export declare function connectionFromArray<T>(
  16. data: ReadonlyArray<T>,
  17. args: ConnectionArguments,
  18. ): Connection<T>;
  19. /**
  20. * A version of `connectionFromArray` that takes a promised array, and returns a
  21. * promised connection.
  22. */
  23. export declare function connectionFromPromisedArray<T>(
  24. dataPromise: Promise<ReadonlyArray<T>>,
  25. args: ConnectionArguments,
  26. ): Promise<Connection<T>>;
  27. /**
  28. * Given a slice (subset) of an array, returns a connection object for use in
  29. * GraphQL.
  30. *
  31. * This function is similar to `connectionFromArray`, but is intended for use
  32. * cases where you know the cardinality of the connection, consider it too large
  33. * to materialize the entire array, and instead wish pass in a slice of the
  34. * total result large enough to cover the range specified in `args`.
  35. */
  36. export declare function connectionFromArraySlice<T>(
  37. arraySlice: ReadonlyArray<T>,
  38. args: ConnectionArguments,
  39. meta: ArraySliceMetaInfo,
  40. ): Connection<T>;
  41. /**
  42. * A version of `connectionFromArraySlice` that takes a promised array slice,
  43. * and returns a promised connection.
  44. */
  45. export declare function connectionFromPromisedArraySlice<T>(
  46. dataPromise: Promise<ReadonlyArray<T>>,
  47. args: ConnectionArguments,
  48. arrayInfo: ArraySliceMetaInfo,
  49. ): Promise<Connection<T>>;
  50. /**
  51. * Creates the cursor string from an offset.
  52. */
  53. export declare function offsetToCursor(offset: number): ConnectionCursor;
  54. /**
  55. * Extracts the offset from the cursor string.
  56. */
  57. export declare function cursorToOffset(cursor: ConnectionCursor): number;
  58. /**
  59. * Return the cursor associated with an object in an array.
  60. */
  61. export declare function cursorForObjectInConnection<T>(
  62. data: ReadonlyArray<T>,
  63. object: T,
  64. ): ConnectionCursor | null;
  65. /**
  66. * Given an optional cursor and a default offset, returns the offset
  67. * to use; if the cursor contains a valid offset, that will be used,
  68. * otherwise it will be the default.
  69. */
  70. export declare function getOffsetWithDefault(
  71. cursor: ConnectionCursor | null | undefined,
  72. defaultOffset: number,
  73. ): number;
  74. export {};