Skip to content

Commit 0fb119a

Browse files
fix: remove explicit type for GraphQLScalarType (#1920)
Removing the explicit type helps with type inference. For example Before this change `GraphQLDate` is of type `GraphQLScalarType<unknown, unknown>` After this change `GraphQLDate` is of type `GraphQLScalarType<Date, string>` The alternative to this is to duplicate the types from the `GraphQLScalarTypeConfig` By adding the `TInternal` and `TExternal` generics to the explicit type These types are useful when adding scalars with a code first apporach such as in @pothos/core where scalar types are defined for the `SchemaBuilder` Co-authored-by: James Macfie <[email protected]>
1 parent 12ed1b5 commit 0fb119a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+518
-286
lines changed

src/scalars/AccountNumber.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const validate = (account: unknown, ast?: ValueNode): string => {
1717
? {
1818
nodes: ast,
1919
}
20-
: undefined
20+
: undefined,
2121
);
2222
}
2323

@@ -28,7 +28,7 @@ const validate = (account: unknown, ast?: ValueNode): string => {
2828
? {
2929
nodes: ast,
3030
}
31-
: undefined
31+
: undefined,
3232
);
3333
}
3434

@@ -38,7 +38,8 @@ const validate = (account: unknown, ast?: ValueNode): string => {
3838
export const GraphQLAccountNumberConfig: GraphQLScalarTypeConfig<string, string> = {
3939
name: 'AccountNumber',
4040
description:
41-
'Banking account number is a string of 5 to 17 alphanumeric values for ' + 'representing an generic account number',
41+
'Banking account number is a string of 5 to 17 alphanumeric values for ' +
42+
'representing an generic account number',
4243

4344
serialize(value: unknown) {
4445
return validate(value);
@@ -67,4 +68,4 @@ export const GraphQLAccountNumberConfig: GraphQLScalarTypeConfig<string, string>
6768
},
6869
};
6970

70-
export const GraphQLAccountNumber: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLAccountNumberConfig);
71+
export const GraphQLAccountNumber = /*#__PURE__*/ new GraphQLScalarType(GraphQLAccountNumberConfig);

src/scalars/BigInt.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,4 @@ export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
9797
},
9898
};
9999

100-
export const GraphQLBigInt: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(
101-
GraphQLBigIntConfig,
102-
);
100+
export const GraphQLBigInt = /*#__PURE__*/ new GraphQLScalarType(GraphQLBigIntConfig);

src/scalars/Byte.ts

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ function hexValidator(value: string) {
1919
// into smaller pieces to avoid this issue.
2020
if (value.length > 8) {
2121
let parsedString = '';
22-
for (let startIndex = 0, endIndex = 8; startIndex < value.length; startIndex += 8, endIndex += 8) {
22+
for (
23+
let startIndex = 0, endIndex = 8;
24+
startIndex < value.length;
25+
startIndex += 8, endIndex += 8
26+
) {
2327
parsedString += parseInt(value.slice(startIndex, endIndex), 16).toString(16);
2428
}
2529
return parsedString === sanitizedValue;
@@ -35,7 +39,7 @@ function validate(value: Buffer | string | BufferJson, ast?: ValueNode) {
3539
? {
3640
nodes: ast,
3741
}
38-
: undefined
42+
: undefined,
3943
);
4044
}
4145
if (typeof value === 'string') {
@@ -48,7 +52,7 @@ function validate(value: Buffer | string | BufferJson, ast?: ValueNode) {
4852
? {
4953
nodes: ast,
5054
}
51-
: undefined
55+
: undefined,
5256
);
5357
}
5458
return global.Buffer.from(value, isHex ? 'hex' : 'base64');
@@ -60,38 +64,49 @@ function validate(value: Buffer | string | BufferJson, ast?: ValueNode) {
6064
function parseObject(ast: ObjectValueNode) {
6165
const key = ast.fields[0].value;
6266
const value = ast.fields[1].value;
63-
if (ast.fields.length === 2 && key.kind === Kind.STRING && key.value === 'Buffer' && value.kind === Kind.LIST) {
64-
return global.Buffer.from(value.values.map((astValue: IntValueNode) => parseInt(astValue.value)));
67+
if (
68+
ast.fields.length === 2 &&
69+
key.kind === Kind.STRING &&
70+
key.value === 'Buffer' &&
71+
value.kind === Kind.LIST
72+
) {
73+
return global.Buffer.from(
74+
value.values.map((astValue: IntValueNode) => parseInt(astValue.value)),
75+
);
6576
}
6677
throw createGraphQLError(`Value is not a JSON representation of Buffer: ${print(ast)}`, {
6778
nodes: [ast],
6879
});
6980
}
7081

71-
export const GraphQLByteConfig: GraphQLScalarTypeConfig<Buffer | string | BufferJson, Buffer> = /*#__PURE__*/ {
72-
name: 'Byte',
73-
description: 'The `Byte` scalar type represents byte value as a Buffer',
74-
serialize: validate,
75-
parseValue: validate,
76-
parseLiteral(ast: ASTNode) {
77-
switch (ast.kind) {
78-
case Kind.STRING:
79-
return validate(ast.value, ast);
80-
case Kind.OBJECT:
81-
return parseObject(ast);
82-
default:
83-
throw createGraphQLError(`Can only parse base64 or hex encoded strings as Byte, but got a: ${ast.kind}`, {
84-
nodes: [ast],
85-
});
86-
}
87-
},
88-
extensions: {
89-
codegenScalarType: 'Buffer | string',
90-
jsonSchema: {
91-
type: 'string',
92-
format: 'byte',
82+
export const GraphQLByteConfig: GraphQLScalarTypeConfig<Buffer | string | BufferJson, Buffer> =
83+
/*#__PURE__*/ {
84+
name: 'Byte',
85+
description: 'The `Byte` scalar type represents byte value as a Buffer',
86+
serialize: validate,
87+
parseValue: validate,
88+
parseLiteral(ast: ASTNode) {
89+
switch (ast.kind) {
90+
case Kind.STRING:
91+
return validate(ast.value, ast);
92+
case Kind.OBJECT:
93+
return parseObject(ast);
94+
default:
95+
throw createGraphQLError(
96+
`Can only parse base64 or hex encoded strings as Byte, but got a: ${ast.kind}`,
97+
{
98+
nodes: [ast],
99+
},
100+
);
101+
}
102+
},
103+
extensions: {
104+
codegenScalarType: 'Buffer | string',
105+
jsonSchema: {
106+
type: 'string',
107+
format: 'byte',
108+
},
93109
},
94-
},
95-
};
110+
};
96111

97-
export const GraphQLByte: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLByteConfig);
112+
export const GraphQLByte = /*#__PURE__*/ new GraphQLScalarType(GraphQLByteConfig);

src/scalars/CountryCode.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Kind, GraphQLScalarType, ValueNode } from 'graphql';
1+
import { GraphQLScalarType, Kind, ValueNode } from 'graphql';
22
import { createGraphQLError } from '../error.js';
33

44
const COUNTRY_CODE_REGEX =
@@ -12,7 +12,7 @@ const validate = (value: any, ast?: ValueNode) => {
1212
? {
1313
nodes: ast,
1414
}
15-
: undefined
15+
: undefined,
1616
);
1717
}
1818

@@ -23,13 +23,13 @@ const validate = (value: any, ast?: ValueNode) => {
2323
? {
2424
nodes: ast,
2525
}
26-
: undefined
26+
: undefined,
2727
);
2828
}
2929
return value;
3030
};
3131

32-
export const GraphQLCountryCode: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType({
32+
export const GraphQLCountryCode = /*#__PURE__*/ new GraphQLScalarType({
3333
name: 'CountryCode',
3434
description: 'A country code as defined by ISO 3166-1 alpha-2',
3535
serialize(value) {
@@ -42,9 +42,12 @@ export const GraphQLCountryCode: GraphQLScalarType = /*#__PURE__*/ new GraphQLSc
4242

4343
parseLiteral(ast) {
4444
if (ast.kind !== Kind.STRING) {
45-
throw createGraphQLError(`Can only validate strings as country codes but got a: ${ast.kind}`, {
46-
nodes: [ast],
47-
});
45+
throw createGraphQLError(
46+
`Can only validate strings as country codes but got a: ${ast.kind}`,
47+
{
48+
nodes: [ast],
49+
},
50+
);
4851
}
4952
return validate(ast.value, ast);
5053
},

src/scalars/Cuid.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Kind, GraphQLScalarType, GraphQLScalarTypeConfig, ValueNode } from 'graphql';
1+
import { GraphQLScalarType, GraphQLScalarTypeConfig, Kind, ValueNode } from 'graphql';
22
import { createGraphQLError } from '../error.js';
33

44
const CUID_REGEX = /^c[^\s-]{8,}$/i;
@@ -11,7 +11,7 @@ const validate = (value: any, ast?: ValueNode) => {
1111
? {
1212
nodes: ast,
1313
}
14-
: undefined
14+
: undefined,
1515
);
1616
}
1717

@@ -22,7 +22,7 @@ const validate = (value: any, ast?: ValueNode) => {
2222
? {
2323
nodes: ast,
2424
}
25-
: undefined
25+
: undefined,
2626
);
2727
}
2828

@@ -63,4 +63,4 @@ export const GraphQLCuidConfig = /*#__PURE__*/ {
6363
},
6464
} as GraphQLScalarTypeConfig<string, string>;
6565

66-
export const GraphQLCuid: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLCuidConfig);
66+
export const GraphQLCuid = /*#__PURE__*/ new GraphQLScalarType(GraphQLCuidConfig);

src/scalars/Currency.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Kind, GraphQLScalarType, GraphQLScalarTypeConfig, ASTNode } from 'graphql';
1+
import { ASTNode, GraphQLScalarType, GraphQLScalarTypeConfig, Kind } from 'graphql';
22
import { createGraphQLError } from '../error.js';
33

44
const CURRENCY_REGEX =
@@ -10,7 +10,10 @@ const validate = (value: any, ast?: ASTNode) => {
1010
}
1111

1212
if (!CURRENCY_REGEX.test(value)) {
13-
throw createGraphQLError(`Value is not a valid currency value: ${value}`, ast ? { nodes: ast } : undefined);
13+
throw createGraphQLError(
14+
`Value is not a valid currency value: ${value}`,
15+
ast ? { nodes: ast } : undefined,
16+
);
1417
}
1518

1619
return value;
@@ -33,7 +36,9 @@ export const GraphQLCurrencyConfig = /*#__PURE__*/ {
3336

3437
parseLiteral(ast) {
3538
if (ast.kind !== Kind.STRING) {
36-
throw createGraphQLError(`Can only validate strings as a currency but got a: ${ast.kind}`, { nodes: ast });
39+
throw createGraphQLError(`Can only validate strings as a currency but got a: ${ast.kind}`, {
40+
nodes: ast,
41+
});
3742
}
3843

3944
return validate(ast.value, ast);
@@ -51,4 +56,4 @@ export const GraphQLCurrencyConfig = /*#__PURE__*/ {
5156
},
5257
} as GraphQLScalarTypeConfig<string, string>;
5358

54-
export const GraphQLCurrency: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLCurrencyConfig);
59+
export const GraphQLCurrency = /*#__PURE__*/ new GraphQLScalarType(GraphQLCurrencyConfig);

src/scalars/DID.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLScalarType, Kind, GraphQLScalarTypeConfig, ASTNode } from 'graphql';
1+
import { ASTNode, GraphQLScalarType, GraphQLScalarTypeConfig, Kind } from 'graphql';
22
import { createGraphQLError } from '../error.js';
33

44
// See: https://www.w3.org/TR/2021/PR-did-core-20210803/#did-syntax
@@ -11,7 +11,10 @@ const validate = (value: any, ast?: ASTNode) => {
1111
}
1212

1313
if (!DID_REGEX.test(value)) {
14-
throw createGraphQLError(`Value is not a valid DID: ${value}`, ast ? { nodes: ast } : undefined);
14+
throw createGraphQLError(
15+
`Value is not a valid DID: ${value}`,
16+
ast ? { nodes: ast } : undefined,
17+
);
1518
}
1619

1720
return value;
@@ -31,7 +34,9 @@ export const GraphQLDIDConfig = {
3134

3235
parseLiteral(ast) {
3336
if (ast.kind !== Kind.STRING) {
34-
throw createGraphQLError(`Can only validate strings as DID but got a: ${ast.kind}`, { nodes: ast });
37+
throw createGraphQLError(`Can only validate strings as DID but got a: ${ast.kind}`, {
38+
nodes: ast,
39+
});
3540
}
3641

3742
return validate(ast.value, ast);
@@ -49,4 +54,4 @@ export const GraphQLDIDConfig = {
4954
},
5055
} as GraphQLScalarTypeConfig<string, string>;
5156

52-
export const GraphQLDID: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLDIDConfig);
57+
export const GraphQLDID = /*#__PURE__*/ new GraphQLScalarType(GraphQLDIDConfig);

src/scalars/EmailAddress.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,4 @@ export const GraphQLEmailAddressConfig = /*#__PURE__*/ {
5050
},
5151
} as GraphQLScalarTypeConfig<string, string>;
5252

53-
export const GraphQLEmailAddress: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(
54-
GraphQLEmailAddressConfig,
55-
);
53+
export const GraphQLEmailAddress = /*#__PURE__*/ new GraphQLScalarType(GraphQLEmailAddressConfig);

src/scalars/GUID.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export const GraphQLGUIDConfig = /*#__PURE__*/ Object.assign({}, GraphQLUUIDConf
55
name: 'GUID',
66
});
77

8-
export const GraphQLGUID: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLGUIDConfig);
8+
export const GraphQLGUID = /*#__PURE__*/ new GraphQLScalarType(GraphQLGUIDConfig);

src/scalars/HSL.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import { Kind, GraphQLScalarType, GraphQLScalarTypeConfig, ASTNode } from 'graphql';
1+
import { ASTNode, GraphQLScalarType, GraphQLScalarTypeConfig, Kind } from 'graphql';
22
import { createGraphQLError } from '../error.js';
33

4-
const HSL_REGEX = /^hsl\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*\)$/;
4+
const HSL_REGEX =
5+
/^hsl\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*\)$/;
56

67
const validate = (value: any, ast?: ASTNode) => {
78
if (typeof value !== 'string') {
89
throw createGraphQLError(`Value is not string: ${value}`, ast ? { nodes: ast } : undefined);
910
}
1011

1112
if (!HSL_REGEX.test(value)) {
12-
throw createGraphQLError(`Value is not a valid HSL color: ${value}`, ast ? { nodes: ast } : undefined);
13+
throw createGraphQLError(
14+
`Value is not a valid HSL color: ${value}`,
15+
ast ? { nodes: ast } : undefined,
16+
);
1317
}
1418

1519
return value;
1620
};
1721

18-
const specifiedByURL = 'https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl()_and_hsla()';
22+
const specifiedByURL =
23+
'https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl()_and_hsla()';
1924

2025
export const GraphQLHSLConfig: GraphQLScalarTypeConfig<string, string> = /*#__PURE__*/ {
2126
name: `HSL`,
@@ -32,7 +37,9 @@ export const GraphQLHSLConfig: GraphQLScalarTypeConfig<string, string> = /*#__PU
3237

3338
parseLiteral(ast) {
3439
if (ast.kind !== Kind.STRING) {
35-
throw createGraphQLError(`Can only validate strings as HSL colors but got a: ${ast.kind}`, { nodes: ast });
40+
throw createGraphQLError(`Can only validate strings as HSL colors but got a: ${ast.kind}`, {
41+
nodes: ast,
42+
});
3643
}
3744

3845
return validate(ast.value, ast);
@@ -50,4 +57,4 @@ export const GraphQLHSLConfig: GraphQLScalarTypeConfig<string, string> = /*#__PU
5057
},
5158
} as GraphQLScalarTypeConfig<string, string>;
5259

53-
export const GraphQLHSL: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLHSLConfig);
60+
export const GraphQLHSL = /*#__PURE__*/ new GraphQLScalarType(GraphQLHSLConfig);

0 commit comments

Comments
 (0)