dotansimha / graphql-code-generator-community

MIT License
112 stars 143 forks source link

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

Open underfisk opened 8 months ago

underfisk commented 8 months ago

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`
  )
lachieh commented 8 months ago

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.

lachieh commented 7 months ago

Added a PR to solve this issue ☝🏻