Mocking GraphQL in Storybook
Pre-requisites
- Storybook should be running, start it by running
yarn rw storybook
- Have a Cell, Query, or Mutation that you would like to mock
Where to put mock-requests
- Mock-requests placed in a file ending with
.mock.js|ts
are automatically imported and become globally scoped, which means that they will be available in all of your stories. - Mock-requests in a story will be locally scoped and will overwrite globally scoped mocks.
Mocking a Cell's Query
Locate the file ending with .mock.js
in your Cell's folder. This file exports a value named standard
, which is the mock-data that will be returned for your Cell's QUERY
.
UserProfileCell/UserProfileCell.js
export const QUERY = gql`
query UserProfileQuery {
userProfile {
id
}
}
`
// UserProfileCell/UserProfileCell.mock.js
export const standard = {
userProfile: {
id: 42,
},
}
The value assigned to standard
is the mock-data associated to the QUERY
, so modifying the QUERY
means you need to modify the mock-data.
UserProfileCell/UserProfileCell.js
export const QUERY = gql`
query UserProfileQuery {
userProfile {
id
+ name
}
}
`
// UserProfileCell/UserProfileCell.mock.js
export const standard = {
userProfile: {
id: 42,
+ name: 'peterp',
}
}
Behind the scenes: Redwood uses the value associated to
standard
as the second argument tomockGraphQLQuery
.