-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.ts
27 lines (24 loc) · 828 Bytes
/
errors.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
import type { GraphQLError } from 'graphql'
import { ResponseTransformer } from '../response'
import { jsonParse } from '../utils/internal/jsonParse'
import { mergeRight } from '../utils/internal/mergeRight'
import { json } from './json'
/**
* Sets a given list of GraphQL errors on the mocked response.
* @example res(ctx.errors([{ message: 'Unauthorized' }]))
* @see {@link https://mswjs.io/docs/api/context/errors}
*/
export const errors = <
ErrorsType extends readonly Partial<GraphQLError>[] | null | undefined,
>(
errorsList: ErrorsType,
): ResponseTransformer<string> => {
return (res) => {
if (errorsList == null) {
return res
}
const prevBody = jsonParse(res.body) || {}
const nextBody = mergeRight(prevBody, { errors: errorsList })
return json(nextBody)(res as any) as any
}
}