-
-
Notifications
You must be signed in to change notification settings - Fork 531
/
graphql.ts
146 lines (135 loc) · 3.96 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type { DocumentNode, OperationTypeNode } from 'graphql'
import {
ResponseResolver,
RequestHandlerOptions,
} from './handlers/RequestHandler'
import {
GraphQLHandler,
GraphQLVariables,
ExpectedOperationTypeNode,
GraphQLHandlerNameSelector,
GraphQLResolverExtras,
GraphQLResponseBody,
GraphQLQuery,
} from './handlers/GraphQLHandler'
import type { 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
}
export type GraphQLRequestHandler = <
Query extends GraphQLQuery = GraphQLQuery,
Variables extends GraphQLVariables = GraphQLVariables,
>(
operationName:
| GraphQLHandlerNameSelector
| DocumentNode
| TypedDocumentNode<Query, Variables>,
resolver: GraphQLResponseResolver<Query, Variables>,
options?: RequestHandlerOptions,
) => GraphQLHandler
export type GraphQLResponseResolver<
Query extends GraphQLQuery = GraphQLQuery,
Variables extends GraphQLVariables = GraphQLVariables,
> = ResponseResolver<
GraphQLResolverExtras<Variables>,
null,
GraphQLResponseBody<Query>
>
function createScopedGraphQLHandler(
operationType: ExpectedOperationTypeNode,
url: Path,
): GraphQLRequestHandler {
return (operationName, resolver, options = {}) => {
return new GraphQLHandler(
operationType,
operationName,
url,
resolver,
options,
)
}
}
function createGraphQLOperationHandler(url: Path) {
return <
Query extends Record<string, any>,
Variables extends GraphQLVariables = GraphQLVariables,
>(
resolver: ResponseResolver<
GraphQLResolverExtras<Variables>,
null,
GraphQLResponseBody<Query>
>,
) => {
return new GraphQLHandler('all', new RegExp('.*'), url, resolver)
}
}
const standardGraphQLHandlers = {
/**
* Intercepts a GraphQL query by a given name.
*
* @example
* graphql.query('GetUser', () => {
* return HttpResponse.json({ data: { user: { name: 'John' } } })
* })
*
* @see {@link https://mswjs.io/docs/api/graphql#graphqlqueryqueryname-resolver `graphql.query()` API reference}
*/
query: createScopedGraphQLHandler('query' as OperationTypeNode, '*'),
/**
* Intercepts a GraphQL mutation by its name.
*
* @example
* graphql.mutation('SavePost', () => {
* return HttpResponse.json({ data: { post: { id: 'abc-123 } } })
* })
*
* @see {@link https://mswjs.io/docs/api/graphql#graphqlmutationmutationname-resolver `graphql.query()` API reference}
*
*/
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, '*'),
/**
* Intercepts any GraphQL operation, regardless of its type or name.
*
* @example
* graphql.operation(() => {
* return HttpResponse.json({ data: { name: 'John' } })
* })
*
* @see {@link https://mswjs.io/docs/api/graphql#graphloperationresolver `graphql.operation()` API reference}
*/
operation: createGraphQLOperationHandler('*'),
}
function createGraphQLLink(url: Path): typeof standardGraphQLHandlers {
return {
operation: createGraphQLOperationHandler(url),
query: createScopedGraphQLHandler('query' as OperationTypeNode, url),
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, url),
}
}
/**
* A namespace to intercept and mock GraphQL operations
*
* @example
* graphql.query('GetUser', resolver)
* graphql.mutation('DeletePost', resolver)
*
* @see {@link https://mswjs.io/docs/api/graphql `graphql` API reference}
*/
export const graphql = {
...standardGraphQLHandlers,
/**
* Intercepts GraphQL operations scoped by the given URL.
*
* @example
* const github = graphql.link('https://api.github.com/graphql')
* github.query('GetRepo', resolver)
*
* @see {@link https://mswjs.io/docs/api/graphql#graphqllinkurl `graphql.link()` API reference}
*/
link: createGraphQLLink,
}