Mocking shopify api #179
Replies: 2 comments 3 replies
-
Hey, @allforabit. Thank you for your kind words. Our pagination API is not configurable and I don't think it should be, honestly. The multitude of different scenarios and API design conventions is too broad, and I generally dislike configurations when it comes to empowering different use cases. Instead, I recommend you create your custom handlers while utilizing the Data's built-in pagination. import { rest } from 'msw'
import { factory, primaryKey } from '@mswjs/data'
const db = factory({
product: {
id: primaryKey(String),
title: String,
price: Number,
},
})
export const handlers = [
rest.get('/products', (req, res, ctx) => {
// Take the pagination query parameters supplied by Shopify.
const first = req.url.searchParams.get('first')
const products = db.product.findMany({
// Skipping the "where" predicate will match all products.
where: {},
// Map Shopify's pagination parameters with those
// supported by the Data.
take: first,
})
return res(ctx.json(products))
}),
] You will have to implement the CRUD request handlers by yourself but that is not a bad thing. In the end, you get much more control over the handlers, so you can map to any third-party API requirements with ease. Alternatively, you can modify the handlers returned from const shopifyHandlers = db.product.toHandlers('graphql')
export const handlers = [
...shopifyHandlers.map((handler) => {
const { resolver } = handler
handler.resolver = (req, res, ctx) => {
// Set the "take" query parameter expected by the Data
// to equal the "first" query parameter sent by Shopify.
req.url.searchParams.set('take', req.url.searchParams.get('first'))
return resolver(req, res, ctx)
}
return handler
}),
] This is generally a more brittle approach because you're coupling your logic with MSW's internals. By using this approach it becomes your responsibility to ensure proper implementation across different MSW versions if they change the internal handler implementation, for example. Use it as the last resort. |
Beta Was this translation helpful? Give feedback.
-
What if I need to query the previous page? So for shopify the variables are |
Beta Was this translation helpful? Give feedback.
-
Hi, have just started to dig into this library. It really looks like it's solving the problem of creating maintainable mock data really. Thanks, super work on this! I'm thinking of using the library to mock responses from the Shopify backend but hit the initial snag of it using different arguments for paging. I.e. shopify uses
first
but msw data uses take and skip, etc. What's the best way around this do you think? Would there need to be a customgenerateGraphQLHandlers
for Shopify's graphql api?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions