Skip to content

Commit

Permalink
typescript-msw: future proof types for generated mock functions (#484)
Browse files Browse the repository at this point in the history
* future proof types for generated mock functions

* fixes

* prettier

* changeset

* Update tidy-trains-wait.md

* test fixes

* Apply suggestions from code review

---------

Co-authored-by: Dotan Simha <[email protected]>
Co-authored-by: Saihajpreet Singh <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2023
1 parent 04e3afe commit 1f056e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-trains-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-msw': minor
---

future proof types for generated mock functions - this also adds support for `msw@2`.
6 changes: 4 additions & 2 deletions packages/plugins/typescript/msw/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class MSWVisitor extends ClientSideBaseVisitor<MSWRawPluginConfig, MSWPlu
return [];
}

return [`import { graphql, ResponseResolver, GraphQLRequest, GraphQLContext } from 'msw'`];
return [`import { graphql } from 'msw'`];
}

public getContent() {
Expand Down Expand Up @@ -80,7 +80,9 @@ export class MSWVisitor extends ClientSideBaseVisitor<MSWRawPluginConfig, MSWPlu
* )
* })
*/
export const ${handlerName} = (resolver: ResponseResolver<GraphQLRequest<${operationVariablesTypes}>, GraphQLContext<${operationResultType}>, any>) =>
export const ${handlerName} = (resolver: Parameters<typeof ${
link?.name || 'graphql'
}.${operationType.toLowerCase()}<${operationResultType}, ${operationVariablesTypes}>>[1]) =>
${
link?.name || 'graphql'
}.${operationType.toLowerCase()}<${operationResultType}, ${operationVariablesTypes}>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`msw Should generate JSDoc documentation with variables and selection fr
* )
* })
*/
export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<UserQueryVariables>, GraphQLContext<UserQuery>, any>) =>
export const mockUserQuery = (resolver: Parameters<typeof graphql.query<UserQuery, UserQueryVariables>>[1]) =>
graphql.query<UserQuery, UserQueryVariables>(
'User',
resolver
Expand All @@ -30,7 +30,7 @@ export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<UserQuer
* )
* })
*/
export const mockUpdateUserMutation = (resolver: ResponseResolver<GraphQLRequest<UpdateUserMutationVariables>, GraphQLContext<UpdateUserMutation>, any>) =>
export const mockUpdateUserMutation = (resolver: Parameters<typeof graphql.mutation<UpdateUserMutation, UpdateUserMutationVariables>>[1]) =>
graphql.mutation<UpdateUserMutation, UpdateUserMutationVariables>(
'UpdateUser',
resolver
Expand All @@ -51,7 +51,7 @@ exports[`msw Should generate a link with an endpoint when passed a link variable
* )
* })
*/
export const mockUserQueryApi = (resolver: ResponseResolver<GraphQLRequest<UserQueryVariables>, GraphQLContext<UserQuery>, any>) =>
export const mockUserQueryApi = (resolver: Parameters<typeof api.query<UserQuery, UserQueryVariables>>[1]) =>
api.query<UserQuery, UserQueryVariables>(
'User',
resolver
Expand All @@ -67,7 +67,7 @@ export const mockUserQueryApi = (resolver: ResponseResolver<GraphQLRequest<UserQ
* )
* })
*/
export const mockUpdateUserMutationApi = (resolver: ResponseResolver<GraphQLRequest<UpdateUserMutationVariables>, GraphQLContext<UpdateUserMutation>, any>) =>
export const mockUpdateUserMutationApi = (resolver: Parameters<typeof api.mutation<UpdateUserMutation, UpdateUserMutationVariables>>[1]) =>
api.mutation<UpdateUserMutation, UpdateUserMutationVariables>(
'UpdateUser',
resolver
Expand All @@ -88,7 +88,7 @@ exports[`msw Should generate handlerNames without suffix when withSuffix is fals
* )
* })
*/
export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<UserQueryVariables>, GraphQLContext<UserQuery>, any>) =>
export const mockUserQuery = (resolver: Parameters<typeof api.query<UserQuery, UserQueryVariables>>[1]) =>
api.query<UserQuery, UserQueryVariables>(
'User',
resolver
Expand All @@ -104,7 +104,7 @@ export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<UserQuer
* )
* })
*/
export const mockUpdateUserMutation = (resolver: ResponseResolver<GraphQLRequest<UpdateUserMutationVariables>, GraphQLContext<UpdateUserMutation>, any>) =>
export const mockUpdateUserMutation = (resolver: Parameters<typeof api.mutation<UpdateUserMutation, UpdateUserMutationVariables>>[1]) =>
api.mutation<UpdateUserMutation, UpdateUserMutationVariables>(
'UpdateUser',
resolver
Expand All @@ -124,7 +124,7 @@ exports[`msw Should generate mocks based on queries and mutations: content 1`] =
* )
* })
*/
export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<UserQueryVariables>, GraphQLContext<UserQuery>, any>) =>
export const mockUserQuery = (resolver: Parameters<typeof graphql.query<UserQuery, UserQueryVariables>>[1]) =>
graphql.query<UserQuery, UserQueryVariables>(
'User',
resolver
Expand All @@ -140,7 +140,7 @@ export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<UserQuer
* )
* })
*/
export const mockUpdateUserMutation = (resolver: ResponseResolver<GraphQLRequest<UpdateUserMutationVariables>, GraphQLContext<UpdateUserMutation>, any>) =>
export const mockUpdateUserMutation = (resolver: Parameters<typeof graphql.mutation<UpdateUserMutation, UpdateUserMutationVariables>>[1]) =>
graphql.mutation<UpdateUserMutation, UpdateUserMutationVariables>(
'UpdateUser',
resolver
Expand All @@ -150,7 +150,7 @@ export const mockUpdateUserMutation = (resolver: ResponseResolver<GraphQLRequest
exports[`msw Should generate mocks based on queries and mutations: imports 1`] = `
[
"import { graphql, ResponseResolver, GraphQLRequest, GraphQLContext } from 'msw'",
"import { graphql } from 'msw'",
]
`;
Expand All @@ -166,7 +166,7 @@ exports[`msw Should use the "importOperationTypesFrom" setting: content with typ
* )
* })
*/
export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<Types.UserQueryVariables>, GraphQLContext<Types.UserQuery>, any>) =>
export const mockUserQuery = (resolver: Parameters<typeof graphql.query<Types.UserQuery, Types.UserQueryVariables>>[1]) =>
graphql.query<Types.UserQuery, Types.UserQueryVariables>(
'User',
resolver
Expand All @@ -182,7 +182,7 @@ export const mockUserQuery = (resolver: ResponseResolver<GraphQLRequest<Types.Us
* )
* })
*/
export const mockUpdateUserMutation = (resolver: ResponseResolver<GraphQLRequest<Types.UpdateUserMutationVariables>, GraphQLContext<Types.UpdateUserMutation>, any>) =>
export const mockUpdateUserMutation = (resolver: Parameters<typeof graphql.mutation<Types.UpdateUserMutation, Types.UpdateUserMutationVariables>>[1]) =>
graphql.mutation<Types.UpdateUserMutation, Types.UpdateUserMutationVariables>(
'UpdateUser',
resolver
Expand Down
10 changes: 6 additions & 4 deletions packages/plugins/typescript/msw/tests/msw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ describe('msw', () => {
// handler variable and result type
const queryVariablesType = `${importOperationTypesFrom}.${queryName}QueryVariables`;
const queryType = `${importOperationTypesFrom}.${queryName}Query`;
expect(result.content).toContain(`GraphQLRequest<${queryVariablesType}>`);
expect(result.content).toContain(`GraphQLContext<${queryType}>`);
expect(result.content).toContain(
`Parameters<typeof graphql.query<${queryType}, ${queryVariablesType}>>[1]`,
);
expect(result.content).toContain(`graphql.query<${queryType}, ${queryVariablesType}>`);

const mutationVariablesType = `${importOperationTypesFrom}.${mutationName}MutationVariables`;
const mutationType = `${importOperationTypesFrom}.${mutationName}Mutation`;
expect(result.content).toContain(`GraphQLRequest<${mutationVariablesType}>`);
expect(result.content).toContain(`GraphQLContext<${mutationType}>`);
expect(result.content).toContain(
`Parameters<typeof graphql.mutation<${mutationType}, ${mutationVariablesType}>>[1]`,
);
expect(result.content).toContain(`graphql.mutation<${mutationType}, ${mutationVariablesType}>`);

expect(result.content).toMatchSnapshot(
Expand Down

0 comments on commit 1f056e9

Please sign in to comment.