Skip to content

Commit

Permalink
Merge branch 'main' into renovate/webpack-dev-server-4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jtoar authored May 4, 2022
2 parents eb5ec48 + b010a15 commit 87a6947
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/testing/src/web/mockRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ export const registerHandler = (handler: RequestHandler) => {
}
}

export type DataFunction = (
variables: Record<string, any>,
export type DataFunction<
Query extends Record<string, unknown> = Record<string, unknown>,
QueryVariables = Record<string, any>
> = (
variables: QueryVariables,
{
req,
ctx,
}: {
req: GraphQLRequest<any>
ctx: GraphQLContext<Record<string, any>>
}
) => Record<string, unknown> | void
) => Query | void

// These should get exported from MSW
type ResponseFunction<BodyType = any> = (
Expand Down Expand Up @@ -144,17 +147,23 @@ const mockGraphQL = (
return data
}

export const mockGraphQLQuery = (
export const mockGraphQLQuery = <
Query extends Record<string, unknown> = Record<string, unknown>,
QueryVariables = Record<string, any>
>(
operation: string,
data: DataFunction | Record<string, unknown>,
data: DataFunction<Query, QueryVariables> | Query,
responseEnhancer?: ResponseEnhancer
) => {
return mockGraphQL('query', operation, data, responseEnhancer)
}

export const mockGraphQLMutation = (
export const mockGraphQLMutation = <
Query extends Record<string, unknown> = Record<string, unknown>,
QueryVariables = Record<string, any>
>(
operation: string,
data: DataFunction | Record<string, unknown>,
data: DataFunction<Query, QueryVariables> | Query,
responseEnhancer?: ResponseEnhancer
) => {
return mockGraphQL('mutation', operation, data, responseEnhancer)
Expand Down

0 comments on commit 87a6947

Please sign in to comment.