forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter-value.js
More file actions
39 lines (33 loc) · 1.04 KB
/
Copy pathfilter-value.js
File metadata and controls
39 lines (33 loc) · 1.04 KB
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
import { GraphQLScalarType, Kind } from 'graphql'
import { filterEnum } from '../enums/system-filter-value'
// Convert enum name → value into a plain JS object
const enumValueMap = filterEnum.getValues().reduce((acc, { name, value }) => {
acc[name] = value
return acc
}, {})
export const FilterValueScalar = new GraphQLScalarType({
name: 'FilterValue',
description: 'Filter value: either a system-defined enum name or a user-defined tag string.',
serialize(value) {
return value
},
parseValue(value) {
if (typeof value !== 'string') {
throw new TypeError(`FilterValue must be a string, got ${typeof value}`)
}
if (Object.prototype.hasOwnProperty.call(enumValueMap, value)) {
return enumValueMap[value]
}
return value
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new TypeError(`FilterValue must be a string`)
}
const input = ast.value
if (Object.prototype.hasOwnProperty.call(enumValueMap, input)) {
return enumValueMap[input]
}
return input
},
})