@@ -26,6 +26,17 @@ export type GraphQLRequestHandler = <
2626 options ?: RequestHandlerOptions ,
2727) => GraphQLHandler
2828
29+ export type GraphQLOperationHandler = <
30+ Query extends GraphQLQuery = GraphQLQuery ,
31+ Variables extends GraphQLVariables = GraphQLVariables ,
32+ > (
33+ resolver : GraphQLResponseResolver <
34+ [ Query ] extends [ never ] ? GraphQLQuery : Query ,
35+ Variables
36+ > ,
37+ options ?: RequestHandlerOptions ,
38+ ) => GraphQLHandler
39+
2940export type GraphQLResponseResolver <
3041 Query extends GraphQLQuery = GraphQLQuery ,
3142 Variables extends GraphQLVariables = GraphQLVariables ,
@@ -44,22 +55,28 @@ function createScopedGraphQLHandler(
4455 }
4556}
4657
47- function createGraphQLOperationHandler ( url : Path ) {
48- return <
49- Query extends GraphQLQuery = GraphQLQuery ,
50- Variables extends GraphQLVariables = GraphQLVariables ,
51- > (
52- resolver : ResponseResolver <
53- GraphQLResolverExtras < Variables > ,
54- null ,
55- GraphQLResponseBody < Query >
56- > ,
57- ) => {
58- return new GraphQLHandler ( 'all' , new RegExp ( '.*' ) , url , resolver )
58+ function createGraphQLOperationHandler ( url : Path ) : GraphQLOperationHandler {
59+ return ( resolver , options ) => {
60+ return new GraphQLHandler ( 'all' , new RegExp ( '.*' ) , url , resolver , options )
5961 }
6062}
6163
62- const standardGraphQLHandlers = {
64+ export interface GraphQLLinkHandlers {
65+ query : GraphQLRequestHandler
66+ mutation : GraphQLRequestHandler
67+ operation : GraphQLOperationHandler
68+ }
69+
70+ /**
71+ * A namespace to intercept and mock GraphQL operations
72+ *
73+ * @example
74+ * graphql.query('GetUser', resolver)
75+ * graphql.mutation('DeletePost', resolver)
76+ *
77+ * @see {@link https://mswjs.io/docs/api/graphql `graphql` API reference }
78+ */
79+ export const graphql = {
6380 /**
6481 * Intercepts a GraphQL query by a given name.
6582 *
@@ -96,27 +113,6 @@ const standardGraphQLHandlers = {
96113 * @see {@link https://mswjs.io/docs/api/graphql#graphqloperationresolver `graphql.operation()` API reference }
97114 */
98115 operation : createGraphQLOperationHandler ( '*' ) ,
99- }
100-
101- function createGraphQLLink ( url : Path ) : typeof standardGraphQLHandlers {
102- return {
103- operation : createGraphQLOperationHandler ( url ) ,
104- query : createScopedGraphQLHandler ( 'query' as OperationTypeNode , url ) ,
105- mutation : createScopedGraphQLHandler ( 'mutation' as OperationTypeNode , url ) ,
106- }
107- }
108-
109- /**
110- * A namespace to intercept and mock GraphQL operations
111- *
112- * @example
113- * graphql.query('GetUser', resolver)
114- * graphql.mutation('DeletePost', resolver)
115- *
116- * @see {@link https://mswjs.io/docs/api/graphql `graphql` API reference }
117- */
118- export const graphql = {
119- ...standardGraphQLHandlers ,
120116
121117 /**
122118 * Intercepts GraphQL operations scoped by the given URL.
@@ -127,5 +123,14 @@ export const graphql = {
127123 *
128124 * @see {@link https://mswjs.io/docs/api/graphql#graphqllinkurl `graphql.link()` API reference }
129125 */
130- link : createGraphQLLink ,
126+ link ( url : Path ) : GraphQLLinkHandlers {
127+ return {
128+ operation : createGraphQLOperationHandler ( url ) ,
129+ query : createScopedGraphQLHandler ( 'query' as OperationTypeNode , url ) ,
130+ mutation : createScopedGraphQLHandler (
131+ 'mutation' as OperationTypeNode ,
132+ url ,
133+ ) ,
134+ }
135+ } ,
131136}
0 commit comments