/
graphql.ts
110 lines (102 loc) · 2.97 KB
/
graphql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { DocumentNode, OperationTypeNode } from 'graphql'
import { ResponseResolver } from './handlers/RequestHandler'
import {
GraphQLHandler,
GraphQLContext,
GraphQLRequest,
GraphQLVariables,
ExpectedOperationTypeNode,
GraphQLHandlerNameSelector,
} from './handlers/GraphQLHandler'
import { Path } from './utils/matching/matchRequestUrl'
export interface TypedDocumentNode<
Result = { [key: string]: any },
Variables = { [key: string]: any },
> extends DocumentNode {
__apiType?: (variables: Variables) => Result
__resultType?: Result
__variablesType?: Variables
}
function createScopedGraphQLHandler(
operationType: ExpectedOperationTypeNode,
url: Path,
) {
return <
Query extends Record<string, any>,
Variables extends GraphQLVariables = GraphQLVariables,
>(
operationName:
| GraphQLHandlerNameSelector
| DocumentNode
| TypedDocumentNode<Query, Variables>,
resolver: ResponseResolver<
GraphQLRequest<Variables>,
GraphQLContext<Query>
>,
) => {
return new GraphQLHandler<GraphQLRequest<Variables>>(
operationType,
operationName,
url,
resolver,
)
}
}
function createGraphQLOperationHandler(url: Path) {
return <
Query extends Record<string, any>,
Variables extends GraphQLVariables = GraphQLVariables,
>(
resolver: ResponseResolver<
GraphQLRequest<Variables>,
GraphQLContext<Query>
>,
) => {
return new GraphQLHandler<GraphQLRequest<Variables>>(
'all',
new RegExp('.*'),
url,
resolver,
)
}
}
const standardGraphQLHandlers = {
/**
* Captures any GraphQL operation, regardless of its name, under the current scope.
* @example
* graphql.operation((req, res, ctx) => {
* return res(ctx.data({ name: 'John' }))
* })
* @see {@link https://mswjs.io/docs/api/graphql/operation `graphql.operation()`}
*/
operation: createGraphQLOperationHandler('*'),
/**
* Captures a GraphQL query by a given name.
* @example
* graphql.query('GetUser', (req, res, ctx) => {
* return res(ctx.data({ user: { name: 'John' } }))
* })
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
*/
query: createScopedGraphQLHandler('query' as OperationTypeNode, '*'),
/**
* Captures a GraphQL mutation by a given name.
* @example
* graphql.mutation('SavePost', (req, res, ctx) => {
* return res(ctx.data({ post: { id: 'abc-123' } }))
* })
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
*/
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, '*'),
}
function createGraphQLLink(url: Path): typeof standardGraphQLHandlers {
return {
operation: createGraphQLOperationHandler(url),
query: createScopedGraphQLHandler('query' as OperationTypeNode, url),
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, url),
}
}
export const graphql = {
...standardGraphQLHandlers,
link: createGraphQLLink,
}