dotansimha / graphql-code-generator-community

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

msw plugin does not allow to pass `RequestHandlerOptions` on generated mocks

underfisk opened this issue · comments

Is your feature request related to a problem? Please describe.

msw plugin does not allow to pass RequestHandlerOptions on generated mocks

Describe the solution you'd like

The mocks should include the optional 3rd parameter RequestHandlerOptions which would enable us to provide once https://mswjs.io/docs/api/graphql#once (v2 API)

Example of the current generated mock

/**
 * @param resolver a function that accepts a captured request and may return a mocked response.
 * @see https://mswjs.io/docs/basics/response-resolver
 * @example
 * mockWhateverQuery((req, res, ctx) => {
 *   const { property1, property2 } = req.variables;
 *   return res(
 *     ctx.data({ property1, property })
 *   )
 * })
 */
export const mockWhateverQuery = (resolver: Parameters<typeof graphql.query<Types.WhateverQuery, Types.WhateverQueryVariables>>[1]) =>
  graphql.query<Types.WhateverQuery, Types.WhateverQueryVariables>(
    'whatever',
    resolver
  )

Proposal

/**
 * @param resolver a function that accepts a captured request and may return a mocked response.
 * @param options object that if `once` is set to true, marks this request handler as used after the first successful match. Used request handlers have no effect on the outgoing traffic and will be ignored during request interception.
 * @see https://mswjs.io/docs/basics/response-resolver
 * @example
 * mockWhateverQuery((req, res, ctx) => {
 *   const { property1, property2 } = req.variables;
 *   return res(
 *     ctx.data({ property1, property })
 *   )
 * })
 */
export const mockWhateverQuery = (resolver: Parameters<typeof graphql.query<Types.WhateverQuery, Types.WhateverQueryVariables>>[1]) =>
  graphql.query<Types.WhateverQuery, Types.WhateverQueryVariables>(
    'whatever',
    resolver,
    options // <-- Notice this is a valid optional object where we could provide `once`
  )

Looks like #468 was closed prematurely. It's nice that part of the features work with msw@v2 but now the generated example is incorrect and so are the docs. MSW got a major version bump for a reason as the API surface is totally different.

Happy to contribute here in a couple of days if no one gets to it before me.

Added a PR to solve this issue ☝🏻