docs: add section on pagination for query context (#11432)
* docs: add section on pagination for query context * fix lint * merge
This commit is contained in:
@@ -99,6 +99,55 @@ Learn more about the generated `list` method in [this reference](!resources!/ser
|
||||
|
||||
</Note>
|
||||
|
||||
### Using Pagination with Query
|
||||
|
||||
If you pass pagination fields to `query.graph`, you must also override the `listAndCount` method in the service.
|
||||
|
||||
For example, following along with the previous example, you must override the `listAndCountPosts` method of the Blog Module's service:
|
||||
|
||||
```ts
|
||||
import { MedusaContext, MedusaService } from "@medusajs/framework/utils"
|
||||
import { Context, FindConfig } from "@medusajs/framework/types"
|
||||
import Post from "./models/post"
|
||||
import Author from "./models/author"
|
||||
|
||||
class BlogModuleService extends MedusaService({
|
||||
Post,
|
||||
Author,
|
||||
}){
|
||||
// @ts-ignore
|
||||
async listAndCountPosts(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
) {
|
||||
const context = filters.context ?? {}
|
||||
delete filters.context
|
||||
|
||||
const result = await super.listAndCountPosts(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
if (context.lang === "es") {
|
||||
result.posts = posts.map((post) => {
|
||||
return {
|
||||
...post,
|
||||
title: post.title + " en español",
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export default BlogModuleService
|
||||
```
|
||||
|
||||
Now, the `listAndCountPosts` method will handle the context passed to `query.graph` when you pass pagination fields. You can also move the logic to transform the posts' titles to a separate method and call it from both `listPosts` and `listAndCountPosts`.
|
||||
|
||||
---
|
||||
|
||||
## Passing Query Context to Related Data Models
|
||||
|
||||
@@ -115,6 +115,6 @@ export const generatedEditDates = {
|
||||
"app/learn/fundamentals/plugins/page.mdx": "2025-01-22T10:14:10.433Z",
|
||||
"app/learn/customization/reuse-customizations/page.mdx": "2025-01-22T10:01:57.665Z",
|
||||
"app/learn/update/page.mdx": "2025-01-27T08:45:19.030Z",
|
||||
"app/learn/fundamentals/module-links/query-context/page.mdx": "2025-02-11T15:56:03.835Z",
|
||||
"app/learn/fundamentals/module-links/query-context/page.mdx": "2025-02-12T16:59:20.963Z",
|
||||
"app/learn/fundamentals/admin/environment-variables/page.mdx": "2025-02-06T13:29:46.800Z"
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user