@@ -172,17 +172,18 @@ using the store provided in the context `context.mockStore`;
172172When having a schema that returns a list, in this case, a list of users:
173173
174174``` ts filename="init-store.ts"
175- import { MockStore } from ' @graphql-mesh/plugin-mock'
175+ import type { IMockStore } from ' @graphql-mesh/plugin-mock'
176176
177- export const store = new MockStore ()
178- const users = [{ id: ' uuid' , name: ' John Snow' }]
179- // Set individual users' data in the store so that they can be queried as individuals later on
180- users .forEach (user => {
181- store .set (' User' , user .id , user )
182- })
177+ export function initializeStore( mockStore : IMockStore ) {
178+ const users = [{ id: ' uuid' , name: ' John Snow' }]
179+ // Set individual users' data in the store so that they can be queried as individuals later on
180+ users .forEach (user => {
181+ mockStore .set (' User' , user .id , user )
182+ })
183183
184- // Populate the `users` query on the root with data
185- store .set (' Query' , ' ROOT' , ' users' , users )
184+ // Populate the `users` query on the root with data
185+ mockStore .set (' Query' , ' ROOT' , ' users' , users )
186+ }
186187```
187188
188189### Get from the store
@@ -192,22 +193,23 @@ You can implement the mock query field `*ById` declaratively like below:
192193``` graphql
193194type Query {
194195 user (id : ID ): User
196+ users : [User ]
195197}
196198```
197199
198200```ts filename ="gateway.config.ts"
199201import { defineConfig } from '@graphql -hive /gateway '
200202import { useMock } from '@graphql -mesh /plugin -mock '
201- import { store } from './init -store .js '
203+ import { initializeStore } from './init -store .js '
202204
203205export const gatewayConfig = defineConfig ({
204206 plugins : pluginCtx => [
205207 useMock ({
206- store ,
208+ initializeStore ,
207209 mocks : [
208210 {
209211 apply : 'Query .user ',
210- custom : (_ , args ) => store .get ('User ', args .id )
212+ custom : (_ , args , context ) => context . mockStore .get ('User ', args .id )
211213 }
212214 ]
213215 })
@@ -224,6 +226,8 @@ type User {
224226}
225227type Query {
226228 me : User
229+ user (id : ID ): User
230+ users : [User ]
227231}
228232type Mutation {
229233 changeMyName (newName : String ): User
@@ -234,32 +238,37 @@ type Mutation {
234238```ts filename ="gateway.config.ts"
235239import { defineConfig } from '@graphql -hive /gateway '
236240import { useMock } from '@graphql -mesh /plugin -mock '
237- import { store } from './init -store .js '
241+ import { initializeStore } from './init -store .js '
242+
243+ interface User {
244+ id : string
245+ name : string
246+ }
238247
239248export const gatewayConfig = defineConfig ({
240249 plugins : pluginCtx => [
241250 useMock ({
242- store ,
251+ initializeStore ,
243252 mocks : [
244253 {
245254 apply : 'Query .me ',
246- custom : (_ , args , context ) => store .get ('User ', 'uuid ')
255+ custom : (_ , args , context ) => context . mockStore .get ('User ', 'uuid ')
247256 },
248257 {
249258 apply : 'Mutation .changeMyName ',
250259 custom : (_ , args , context ) => {
251- const user = store . get ('User ', 'uuid ')
260+ const user = context . mockStore . get ('User ', 'uuid ') as User
252261 user .name = args .newName
253- store .set ('User ', 'uuid ', user )
262+ context . mockStore .set ('User ', 'uuid ', user )
254263 return user
255264 }
256265 },
257266 {
258267 apply : 'Mutation .updateUser ',
259268 custom : (_ , args , context ) => {
260- const user = store . get ('User ', args .id )
269+ const user = context . mockStore . get ('User ', args .id ) as User
261270 user .name = args .name
262- store .set ('User ', args .id , user )
271+ context . mockStore .set ('User ', args .id , user )
263272 return user
264273 }
265274 }
0 commit comments