msw
Version:
1 lines • 4.96 kB
Source Map (JSON)
{"version":3,"sources":["../../src/core/graphql.ts"],"sourcesContent":["import type { OperationTypeNode } from 'graphql'\nimport {\n ResponseResolver,\n RequestHandlerOptions,\n} from './handlers/RequestHandler'\nimport {\n GraphQLHandler,\n GraphQLVariables,\n GraphQLOperationType,\n GraphQLResolverExtras,\n GraphQLResponseBody,\n GraphQLQuery,\n GraphQLPredicate,\n} from './handlers/GraphQLHandler'\nimport type { Path } from './utils/matching/matchRequestUrl'\n\nexport type GraphQLRequestHandler = <\n Query extends GraphQLQuery = GraphQLQuery,\n Variables extends GraphQLVariables = GraphQLVariables,\n>(\n predicate: GraphQLPredicate<Query, Variables>,\n resolver: GraphQLResponseResolver<\n [Query] extends [never] ? GraphQLQuery : Query,\n Variables\n >,\n options?: RequestHandlerOptions,\n) => GraphQLHandler\n\nexport type GraphQLOperationHandler = <\n Query extends GraphQLQuery = GraphQLQuery,\n Variables extends GraphQLVariables = GraphQLVariables,\n>(\n resolver: GraphQLResponseResolver<\n [Query] extends [never] ? GraphQLQuery : Query,\n Variables\n >,\n options?: RequestHandlerOptions,\n) => GraphQLHandler\n\nexport type GraphQLResponseResolver<\n Query extends GraphQLQuery = GraphQLQuery,\n Variables extends GraphQLVariables = GraphQLVariables,\n> = ResponseResolver<\n GraphQLResolverExtras<Variables>,\n null,\n GraphQLResponseBody<[Query] extends [never] ? GraphQLQuery : Query>\n>\n\nfunction createScopedGraphQLHandler(\n operationType: GraphQLOperationType,\n url: Path,\n): GraphQLRequestHandler {\n return (predicate, resolver, options = {}) => {\n return new GraphQLHandler(operationType, predicate, url, resolver, options)\n }\n}\n\nfunction createGraphQLOperationHandler(url: Path): GraphQLOperationHandler {\n return (resolver, options) => {\n return new GraphQLHandler('all', new RegExp('.*'), url, resolver, options)\n }\n}\n\nexport interface GraphQLLinkHandlers {\n query: GraphQLRequestHandler\n mutation: GraphQLRequestHandler\n operation: GraphQLOperationHandler\n}\n\n/**\n * A namespace to intercept and mock GraphQL operations\n *\n * @example\n * graphql.query('GetUser', resolver)\n * graphql.mutation('DeletePost', resolver)\n *\n * @see {@link https://mswjs.io/docs/api/graphql `graphql` API reference}\n */\nexport const graphql = {\n /**\n * Intercepts a GraphQL query by a given name.\n *\n * @example\n * graphql.query('GetUser', () => {\n * return HttpResponse.json({ data: { user: { name: 'John' } } })\n * })\n *\n * @see {@link https://mswjs.io/docs/api/graphql#graphqlqueryqueryname-resolver `graphql.query()` API reference}\n */\n query: createScopedGraphQLHandler('query' as OperationTypeNode, '*'),\n\n /**\n * Intercepts a GraphQL mutation by its name.\n *\n * @example\n * graphql.mutation('SavePost', () => {\n * return HttpResponse.json({ data: { post: { id: 'abc-123 } } })\n * })\n *\n * @see {@link https://mswjs.io/docs/api/graphql#graphqlmutationmutationname-resolver `graphql.query()` API reference}\n *\n */\n mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, '*'),\n\n /**\n * Intercepts any GraphQL operation, regardless of its type or name.\n *\n * @example\n * graphql.operation(() => {\n * return HttpResponse.json({ data: { name: 'John' } })\n * })\n *\n * @see {@link https://mswjs.io/docs/api/graphql#graphqloperationresolver `graphql.operation()` API reference}\n */\n operation: createGraphQLOperationHandler('*'),\n\n /**\n * Intercepts GraphQL operations scoped by the given URL.\n *\n * @example\n * const github = graphql.link('https://api.github.com/graphql')\n * github.query('GetRepo', resolver)\n *\n * @see {@link https://mswjs.io/docs/api/graphql#graphqllinkurl `graphql.link()` API reference}\n */\n link(url: Path): GraphQLLinkHandlers {\n return {\n operation: createGraphQLOperationHandler(url),\n query: createScopedGraphQLHandler('query' as OperationTypeNode, url),\n mutation: createScopedGraphQLHandler(\n 'mutation' as OperationTypeNode,\n url,\n ),\n }\n },\n}\n"],"mappings":"AAKA;AAAA,EACE;AAAA,OAOK;AAmCP,SAAS,2BACP,eACA,KACuB;AACvB,SAAO,CAAC,WAAW,UAAU,UAAU,CAAC,MAAM;AAC5C,WAAO,IAAI,eAAe,eAAe,WAAW,KAAK,UAAU,OAAO;AAAA,EAC5E;AACF;AAEA,SAAS,8BAA8B,KAAoC;AACzE,SAAO,CAAC,UAAU,YAAY;AAC5B,WAAO,IAAI,eAAe,OAAO,IAAI,OAAO,IAAI,GAAG,KAAK,UAAU,OAAO;AAAA,EAC3E;AACF;AAiBO,MAAM,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWrB,OAAO,2BAA2B,SAA8B,GAAG;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAanE,UAAU,2BAA2B,YAAiC,GAAG;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYzE,WAAW,8BAA8B,GAAG;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW5C,KAAK,KAAgC;AACnC,WAAO;AAAA,MACL,WAAW,8BAA8B,GAAG;AAAA,MAC5C,OAAO,2BAA2B,SAA8B,GAAG;AAAA,MACnE,UAAU;AAAA,QACR;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}