Skip to content

Instantly share code, notes, and snippets.

@Renaud009
Created October 24, 2022 21:06
Show Gist options
  • Save Renaud009/130e00db34f4891105ba469a68479e01 to your computer and use it in GitHub Desktop.
Save Renaud009/130e00db34f4891105ba469a68479e01 to your computer and use it in GitHub Desktop.
import { graphql, GraphQLContext, GraphQLRequest, ResponseResolver } from 'msw';
import { api } from '../../../services/graphqlApi.generated';
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type Scalars = {
String: string;
};
export type GetSomethingQueryVariables = Exact<{
id: Scalars['String'];
}>;
export type GetSomethingQuery = { __typename?: 'Query', getSomething: { __typename?: 'Something', id: string, name: string, routingNumber: string } };
/**
* @param resolver a function that accepts a captured request and may return a mocked response.
* @see https://mswjs.io/docs/basics/response-resolver
* @example
* mockGetSomethingQuery((req, res, ctx) => {
* const { id } = req.variables;
* return res(
* ctx.data({ getSomething })
* )
* })
*/
export const mockGetSomethingQuery = (resolver: ResponseResolver<GraphQLRequest<GetSomethingQueryVariables>, GraphQLContext<GetSomethingQuery>, any>) =>
graphql.query<GetSomethingQuery, GetSomethingQueryVariables>(
'GetSomething',
resolver
)
export const GetSomethingDocument = `
query GetSomething($id: String!) {
getSomething(id: $id) {
id
name
routingNumber
}
}
`;
const injectedRtkApi = api.injectEndpoints({
overrideExisting: module.hot?.status() === "apply",
endpoints: (build) => ({
GetSomething: build.query<GetSomethingQuery, GetSomethingQueryVariables>({
query: (variables) => ({ document: GetSomethingDocument, variables })
})
}),
});
export { injectedRtkApi as api };
export const { useGetSomethingQuery, useLazyGetSomethingQuery } = injectedRtkApi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment