MSW is a fantastic testing utility library that has completely replaced my team’s usage of @apollo/client
's MockedProvider. In my opinion, it’s a more “complete” test of your GraphQL operations, and it has been significantly less fussy in throwing spurious runtime errors or mysteriously not returning data during testing. My only complaint was that writing JSON encoders in ReScript was more effort than in TypeScript or JavaScript, and due to the way rescript-apollo-client
+ graphql-ppx
parses GraphQL responses, writing tests in TypeScript for ReScript code while getting compiler errors is possible but tricky (would not recommend trying). Considering graphql-ppx
already creates the parser/serializer functions, why not reuse them for these bindings.
This package is intended to be an ergonomic, data-first wrapper for ReScript bindings to msw, particularly for usage with graphql-ppx and rescript-apollo-client. Being data-first bindings, it also plays nicely with rescript-jest and rescript-promise. It can also be used for REST APIs.
Here’s an example of constructing a network mock:
open MSW
let server = setupServer()
server->Server.listenWithOptions({onUnhandledRequest: #error})
server->Server.use(
GraphQL_PPX.operation(module(UserQuery), ({variables, _}, {once, _}, ctx) => {
once()
->ctx.status(200)
->ctx.data({
userById: Some({
__typename: "foo",
id: variables.id,
avatarUrl: Some("http://test.com/avatar.png"),
fullName: "John Doe",
}),
})
}),
)
I’ve already got a branch for work with the ReScript 10 upgrade and converting over to these bindings from some in-house ones, and the difference is very noticeable on complex queries.