-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathURL.ts
40 lines (33 loc) · 973 Bytes
/
URL.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
import { GraphQLScalarType, Kind } from 'graphql';
import { createGraphQLError } from '../error.js';
export const GraphQLURL = /*#__PURE__*/ new GraphQLScalarType({
name: 'URL',
description:
'A field whose value conforms to the standard URL format as specified in RFC3986: https://www.ietf.org/rfc/rfc3986.txt.',
serialize(value) {
if (value === null) {
return value;
}
return new URL(value.toString()).toString();
},
parseValue: value => (value === null ? value : new URL(value.toString())),
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw createGraphQLError(`Can only validate strings as URLs but got a: ${ast.kind}`, {
nodes: ast,
});
}
if (ast.value === null) {
return ast.value;
} else {
return new URL(ast.value.toString());
}
},
extensions: {
codegenScalarType: 'URL | string',
jsonSchema: {
type: 'string',
format: 'uri',
},
},
});