|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Query Context`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn how to pass context when retrieving data with [Query](../page.mdx). |
|
8
|
|
|
9
|
## What is Query Context? |
|
10
|
|
|
11
|
Query context is additional information you pass to Query when retrieving data. You can use it to apply custom transformations to the returned data based on the current context. |
|
12
|
|
|
13
|
For example, consider a Blog Module with posts and authors. You can send the user's language as context to Query to retrieve posts in the user's language. |
|
14
|
|
|
15
|
Medusa also uses Query Context to [retrieve product variants' prices based on the customer's currency](!resources!/commerce-modules/product/guides/price). |
|
16
|
|
|
17
|
--- |
|
18
|
|
|
19
|
## How to Pass Query Context |
|
20
|
|
|
21
|
The `query.graph` method accepts an optional `context` parameter. You can use this to pass additional context to the data model you're retrieving (for example, `post`) or its related and linked models (for example, `author`). |
|
22
|
|
|
23
|
You can initialize a context using `QueryContext` from the Modules SDK. It accepts an object of contexts as an argument. |
|
24
|
|
|
25
|
For example, to retrieve posts using Query while passing the user's language as context: |
|
26
|
|
|
27
|
export const highlights1 = [ |
|
28
|
["8", "context", "Pass additional context to the query."], |
|
29
|
["8", "QueryContext", "Create a query context."] |
|
30
|
] |
|
31
|
|
|
32
|
```ts highlights={highlights1} |
|
33
|
import { QueryContext } from "@medusajs/framework/utils" |
|
34
|
|
|
35
|
// ... |
|
36
|
|
|
37
|
const { data } = await query.graph({ |
|
38
|
entity: "post", |
|
39
|
fields: ["*"], |
|
40
|
context: QueryContext({ |
|
41
|
lang: "es", |
|
42
|
}), |
|
43
|
}) |
|
44
|
``` |
|
45
|
|
|
46
|
In this example, you pass a `lang` property with the value `es` in the context. You create the context using `QueryContext`. |
|
47
|
|
|
48
|
### How to Handle Query Context |
|
49
|
|
|
50
|
To handle the Query context passed while retrieving records of your data models, override the [generated list method](!resources!/service-factory-reference/methods/list) of the associated module's service. |
|
51
|
|
|
52
|
For example, continuing the example above, you can override the `listPosts` method of the Blog Module's service to handle the Query context: |
|
53
|
|
|
54
|
export const highlights2 = [ |
|
55
|
["11", "listPosts", "Override the generated listPosts method."], |
|
56
|
["16", "context", "The context is passed as part of `filters`."], |
|
57
|
["19", "super", "Use the parent's `listPosts` method to retrieve the posts."], |
|
58
|
["21", "", "If the language is set in the context, you transform the titles of the posts."] |
|
59
|
] |
|
60
|
|
|
61
|
```ts highlights={highlights2} |
|
62
|
import { MedusaContext, MedusaService } from "@medusajs/framework/utils" |
|
63
|
import { Context, FindConfig } from "@medusajs/framework/types" |
|
64
|
import Post from "./models/post" |
|
65
|
import Author from "./models/author" |
|
66
|
|
|
67
|
class BlogModuleService extends MedusaService({ |
|
68
|
Post, |
|
69
|
Author, |
|
70
|
}){ |
|
71
|
// @ts-ignore |
|
72
|
async listPosts( |
|
73
|
filters?: any, |
|
74
|
config?: FindConfig<any> | undefined, |
|
75
|
@MedusaContext() sharedContext?: Context | undefined |
|
76
|
) { |
|
77
|
const context = filters.context ?? {} |
|
78
|
delete filters.context |
|
79
|
|
|
80
|
let posts = await super.listPosts(filters, config, sharedContext) |
|
81
|
|
|
82
|
if (context.lang === "es") { |
|
83
|
posts = posts.map((post) => { |
|
84
|
return { |
|
85
|
...post, |
|
86
|
title: post.title + " en español", |
|
87
|
} |
|
88
|
}) |
|
89
|
} |
|
90
|
|
|
91
|
return posts |
|
92
|
} |
|
93
|
} |
|
94
|
|
|
95
|
export default BlogModuleService |
|
96
|
``` |
|
97
|
|
|
98
|
In the above example, you override the generated `listPosts` method. This method receives the filters passed to `query.graph` as its first parameter. The first parameter includes a `context` property that holds the Query context alsopassed to `query.graph`. |
|
99
|
|
|
100
|
You extract the context from `filters`, then retrieve the posts using the parent's `listPosts` method. If the language is set in the context, you transform the post titles. |
|
101
|
|
|
102
|
All posts returned will now have their titles appended with "en español". |
|
103
|
|
|
104
|
### Using Pagination with Query |
|
105
|
|
|
106
|
If you pass pagination fields to `query.graph`, you must also override the [generated listAndCount method](!resources!/service-factory-reference/methods/listAndCount) in the service. |
|
107
|
|
|
108
|
For example, following the previous example, you must override the `listAndCountPosts` method of the Blog Module's service: |
|
109
|
|
|
110
|
```ts |
|
111
|
import { MedusaContext, MedusaService } from "@medusajs/framework/utils" |
|
112
|
import { Context, FindConfig } from "@medusajs/framework/types" |
|
113
|
import Post from "./models/post" |
|
114
|
import Author from "./models/author" |
|
115
|
|
|
116
|
class BlogModuleService extends MedusaService({ |
|
117
|
Post, |
|
118
|
Author, |
|
119
|
}){ |
|
120
|
// @ts-ignore |
|
121
|
async listAndCountPosts( |
|
122
|
filters?: any, |
|
123
|
config?: FindConfig<any> | undefined, |
|
124
|
@MedusaContext() sharedContext?: Context | undefined |
|
125
|
) { |
|
126
|
const context = filters.context ?? {} |
|
127
|
delete filters.context |
|
128
|
|
|
129
|
const result = await super.listAndCountPosts( |
|
130
|
filters, |
|
131
|
config, |
|
132
|
sharedContext |
|
133
|
) |
|
134
|
|
|
135
|
if (context.lang === "es") { |
|
136
|
result[0] = result[0].map((post) => { |
|
137
|
return { |
|
138
|
...post, |
|
139
|
title: post.title + " en español", |
|
140
|
} |
|
141
|
}) |
|
142
|
} |
|
143
|
|
|
144
|
return result |
|
145
|
} |
|
146
|
} |
|
147
|
|
|
148
|
export default BlogModuleService |
|
149
|
``` |
|
150
|
|
|
151
|
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 post titles to a separate method and call it from both `listPosts` and `listAndCountPosts`. |
|
152
|
|
|
153
|
--- |
|
154
|
|
|
155
|
## Passing Query Context to Related Data Models |
|
156
|
|
|
157
|
If you're retrieving a data model and want to pass Query context to its associated model in the same module, pass them as part of `QueryContext`'s parameter. Then, you can handle them in the same `list` method. |
|
158
|
|
|
159
|
<Note> |
|
160
|
|
|
161
|
To pass Query context to linked data models, check out the [next section](#passing-query-context-to-linked-data-models). |
|
162
|
|
|
163
|
</Note> |
|
164
|
|
|
165
|
For example, to pass a context for the post's authors: |
|
166
|
|
|
167
|
export const highlights3 = [ |
|
168
|
["6", "author", "Pass a context for the author."] |
|
169
|
] |
|
170
|
|
|
171
|
```ts highlights={highlights3} |
|
172
|
const { data } = await query.graph({ |
|
173
|
entity: "post", |
|
174
|
fields: ["*"], |
|
175
|
context: QueryContext({ |
|
176
|
lang: "es", |
|
177
|
author: QueryContext({ |
|
178
|
lang: "es", |
|
179
|
}), |
|
180
|
}), |
|
181
|
}) |
|
182
|
``` |
|
183
|
|
|
184
|
Then, in the `listPosts` method, you can handle the context for the post's authors: |
|
185
|
|
|
186
|
export const highlights4 = [ |
|
187
|
["22", "context.author?.lang", "Access the author's context."] |
|
188
|
] |
|
189
|
|
|
190
|
```ts highlights={highlights4} |
|
191
|
import { MedusaContext, MedusaService } from "@medusajs/framework/utils" |
|
192
|
import { Context, FindConfig } from "@medusajs/framework/types" |
|
193
|
import Post from "./models/post" |
|
194
|
import Author from "./models/author" |
|
195
|
|
|
196
|
class BlogModuleService extends MedusaService({ |
|
197
|
Post, |
|
198
|
Author, |
|
199
|
}){ |
|
200
|
// @ts-ignore |
|
201
|
async listPosts( |
|
202
|
filters?: any, |
|
203
|
config?: FindConfig<any> | undefined, |
|
204
|
@MedusaContext() sharedContext?: Context | undefined |
|
205
|
) { |
|
206
|
const context = filters.context ?? {} |
|
207
|
delete filters.context |
|
208
|
|
|
209
|
let posts = await super.listPosts(filters, config, sharedContext) |
|
210
|
|
|
211
|
const isPostLangEs = context.lang === "es" |
|
212
|
const isAuthorLangEs = context.author?.lang === "es" |
|
213
|
|
|
214
|
if (isPostLangEs || isAuthorLangEs) { |
|
215
|
posts = posts.map((post) => { |
|
216
|
return { |
|
217
|
...post, |
|
218
|
title: isPostLangEs ? post.title + " en español" : post.title, |
|
219
|
author: { |
|
220
|
...post.author, |
|
221
|
name: isAuthorLangEs ? post.author.name + " en español" : post.author.name, |
|
222
|
}, |
|
223
|
} |
|
224
|
}) |
|
225
|
} |
|
226
|
|
|
227
|
return posts |
|
228
|
} |
|
229
|
} |
|
230
|
|
|
231
|
export default BlogModuleService |
|
232
|
``` |
|
233
|
|
|
234
|
The context in `filters` will also include the context for `author`, which you can use to transform the post's authors. |
|
235
|
|
|
236
|
--- |
|
237
|
|
|
238
|
## Passing Query Context to Linked Data Models |
|
239
|
|
|
240
|
If you're retrieving a data model and want to pass Query context to a linked model in a different module, pass an object to the `context` property instead. The object's keys should be the linked model's name, and the values should be the Query context for that linked model. |
|
241
|
|
|
242
|
For example, consider the Product Module's `Product` data model is linked to the Blog Module's `Post` data model. You can pass context to the `Post` data model while retrieving products: |
|
243
|
|
|
244
|
export const highlights5 = [ |
|
245
|
["5", "post", "Pass a context for posts."] |
|
246
|
] |
|
247
|
|
|
248
|
```ts highlights={highlights5} |
|
249
|
const { data } = await query.graph({ |
|
250
|
entity: "product", |
|
251
|
fields: ["*", "post.*"], |
|
252
|
context: { |
|
253
|
post: QueryContext({ |
|
254
|
lang: "es", |
|
255
|
}), |
|
256
|
}, |
|
257
|
}) |
|
258
|
``` |
|
259
|
|
|
260
|
In this example, you retrieve products and their associated posts. You also pass Query context for `post`, indicating the customer's language. |
|
261
|
|
|
262
|
To handle the context, override the generated `listPosts` method of the Blog Module as explained [previously](#how-to-handle-query-context). |