{"version":3,"file":"paginate.mjs","sources":["../../../../../src/query/paginate.ts"],"sourcesContent":["import { type EventCallableAsReturnType, type Store, createEvent, sample } from \"effector\"\n\nimport type { OperationVariables } from \"@apollo/client\"\n\nimport type { Optional } from \"src/lib/optional\"\n\nimport { not } from \"../lib/not\"\n\nimport type { Query } from \"./query\"\n\n/**\n * A pagination helper for a {@link Query}.\n *\n * Allows you to fetch more records of your query\n * by reusing most recent `variables`, just like `fetchMore` does.\n *\n * This requires the Query to not be `idle`, so ensure you\n * first call `.start` or `.refresh` on a query before fetching new page.\n *\n * `paginate` does not provide a way to merge different pages,\n * instead, it employs pre-defined `typePolicies` on your\n * `InMemoryCache`.\n * Make sure you define a proper merge/read strategy in your `typePolicy`\n * when creating the `client`.\n *\n * Documentation:\n * {@link https://www.apollographql.com/docs/react/pagination/core-api | Apollo Pagination API}\n *\n * @example\n *\n * sample({\n *   clock: pageRequested,\n *   source: query.$data.map(data => data.pageInfo.endCursor),\n *   fn: (cursor) => ({ cursor }),\n *   target: paginate(query)\n * })\n *\n * @returns Event to trigger pagination\n */\nexport function paginate<Variables extends OperationVariables = OperationVariables>(\n  query: Query<any, Variables>,\n): EventCallableAsReturnType<Partial<Variables>> {\n  const paginate = createEvent<Partial<Variables>>({ name: `${query.meta.name}.paginate` })\n\n  sample({\n    clock: paginate,\n    source: query.__.$variables as Store<Optional<Variables>>,\n    fn: (original, override) => ({ ...original, ...override }),\n    // We can't paginate idle queries that have no variables yet\n    filter: not(query.$idle),\n    // force a network request for pagination\n    target: query.start,\n  })\n\n  return paginate\n}\n"],"names":["paginate"],"mappings":";;AAuCO,SAAS,SACd,OAC+C;AACzCA,QAAAA,YAAW,YAAgC,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,aAAa;AAEjF,SAAA;AAAA,IACL,OAAOA;AAAAA,IACP,QAAQ,MAAM,GAAG;AAAA,IACjB,IAAI,CAAC,UAAU,cAAc,EAAE,GAAG,UAAU,GAAG;;IAE/C,QAAQ,IAAI,MAAM,KAAK;AAAA;AAAA,IAEvB,QAAQ,MAAM;AAAA,EAAA,CACf;AAEMA,SAAAA;AACT;"}