Mocking your GraphQL API is a common practice when developing and testing your application. It
allows you to simulate the behavior of your API without making real network requests.
Installing
Start by installing the @graphql-mesh/plugin-mock package:
When defined manually, properties can return values either directly or through a method. This is
useful when defining static mocks because a mock property will be called as many times as there are
items in an array. Here’s an example on how this could be achieved:
Hive Gateway supports GraphQL Tools’ Stateful Mocking feature. So you can have stateful mocking by
using the store provided in the context context.mockStore;
Initialize store
When having a schema that returns a list, in this case, a list of users:
init-store.ts
import type { IMockStore } from "@graphql-mesh/plugin-mock";export function initializeStore(mockStore: IMockStore) { const users = [{ id: "uuid", name: "John Snow" }]; // Set individual users' data in the store so that they can be queried as individuals later on users.forEach((user) => { mockStore.set("User", user.id, user); }); // Populate the `users` query on the root with data mockStore.set("Query", "ROOT", "users", users);}
Get from the store
You can implement the mock query field *ById declaratively like below:
type User { id: ID name: String}type Query { me: User user(id: ID): User users: [User]}type Mutation { changeMyName(newName: String): User updateUser(id: ID, name: String): User}