-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathgraphql.test-d.ts
200 lines (180 loc) · 5.45 KB
/
graphql.test-d.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { it, expectTypeOf } from 'vitest'
import { parse } from 'graphql'
import { graphql, HttpResponse, passthrough } from 'msw'
it('graphql mutation can be used without variables generic type', () => {
graphql.mutation('GetUser', () => {
return HttpResponse.json({ data: { id: '2' } })
})
})
it('graphql mutation accepts inline generic variables type', () => {
graphql.mutation<never, { id: string }>('GetUser', ({ variables }) => {
expectTypeOf(variables).toEqualTypeOf<{ id: string }>()
})
})
it('graphql mutation accepts inline generic variables never type', () => {
graphql.mutation<never, never>('CreateUser', ({ variables }) => {
expectTypeOf(variables).toEqualTypeOf<never>()
})
})
it("graphql mutation does not accept null as variables' generic mutation type", () => {
graphql.mutation<
{ key: string },
// @ts-expect-error `null` is not a valid variables type.
null
>('', () => {})
})
it('graphql mutation allows explicit null as the response body type for the mutation', () => {
graphql.mutation<{ key: string }>('MutateData', () => {
return HttpResponse.json({
// Explicit null in mutations must also be allowed.
data: null,
})
})
})
it('graphql mutation does not allow mismatched mutation response', () => {
graphql.mutation<{ key: string }>('MutateData', () => {
return HttpResponse.json({
// @ts-expect-error Response data doesn't match the query type.
data: {},
})
})
})
it("graphql query does not accept null as variables' generic query type ", () => {
graphql.query<
{ key: string },
// @ts-expect-error `null` is not a valid variables type.
null
>('', () => {})
})
it("graphql query accepts the correct type for the variables' generic query type", () => {
/**
* Response body type (GraphQL query type).
*/
// Returned mocked response body must satisfy the
// GraphQL query generic.
graphql.query<{ id: string }>('GetUser', () => {
return HttpResponse.json({
data: { id: '2' },
})
})
})
it('graphql query allows explicit null as the response body type for the query', () => {
graphql.query<{ id: string }>('GetUser', () => {
return HttpResponse.json({
// Explicit null must be allowed.
data: null,
})
})
})
it('graphql query does not accept invalid data type for the response body type for the query', () => {
graphql.query<{ id: string }>('GetUser', () => {
return HttpResponse.json({
data: {
// @ts-expect-error "id" type is incorrect
id: 123,
},
})
})
})
it('graphql query does not allow empty response when the query type is defined', () => {
graphql.query<{ id: string }>(
'GetUser',
// @ts-expect-error response json is empty
() => HttpResponse.json({ data: {} }),
)
})
it('graphql query does not allow incompatible response body type', () => {
graphql.query<{ id: string }>(
'GetUser',
// @ts-expect-error incompatible response body type
() => HttpResponse.text('hello'),
)
})
it("graphql operation does not accept null as variables' generic operation type", () => {
graphql.operation<
{ key: string },
// @ts-expect-error `null` is not a valid variables type.
null
>(() => {
return HttpResponse.json({ data: { key: 'a' } })
})
})
it('graphql operation does not allow mismatched operation response', () => {
graphql.operation<{ key: string }>(() => {
return HttpResponse.json({
// @ts-expect-error Response data doesn't match the query type.
data: {},
})
})
})
it('graphql operation allows explicit null as the response body type for the operation', () => {
graphql.operation<{ key: string }>(() => {
return HttpResponse.json({ data: null })
})
})
it('graphql handlers allow passthrough responses', () => {
// Passthrough responses.
graphql.query('GetUser', () => passthrough())
graphql.mutation('AddPost', () => passthrough())
graphql.operation(() => passthrough())
graphql.query('GetUser', ({ request }) => {
if (request.headers.has('cookie')) {
return passthrough()
}
return HttpResponse.json({ data: {} })
})
})
it("graphql variables cannot extract type from the runtime 'DocumentNode'", () => {
/**
* Supports `DocumentNode` as the GraphQL operation name.
*/
const getUser = parse(`
query GetUser {
user {
firstName
}
}
`)
graphql.query(getUser, () => {
return HttpResponse.json({
// Cannot extract query type from the runtime `DocumentNode`.
data: { arbitrary: true },
})
})
})
it('graphql query cannot extract variable and reponse types', () => {
const getUserById = parse(`
query GetUserById($userId: String!) {
user(id: $userId) {
firstName
}
}
`)
graphql.query(getUserById, ({ variables }) => {
// Cannot extract variables type from a DocumentNode.
expectTypeOf(variables).toEqualTypeOf<Record<string, any>>()
return HttpResponse.json({
data: {
user: {
firstName: 'John',
// Extracting a query body type from the "DocumentNode" is impossible.
lastName: 'Maverick',
},
},
})
})
})
it('graphql mutation cannot extract variable and reponse types', () => {
const createUser = parse(`
mutation CreateUser {
user {
id
}
}
`)
graphql.mutation(createUser, () => {
return HttpResponse.json({
data: { arbitrary: true },
})
})
})