Skip to content

fix: correct type error as a result of removed imports #2854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/setup-scalar.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,15 +492,15 @@ export { GraphQL${this.scalarName} };`;
}*/

async updateDocsMeta() {
const metaPath = path.join(this.baseDir, 'website/src/pages/docs/scalars/_meta.ts');
const metaPath = path.join(this.baseDir, 'website/src/content/scalars/_meta.ts');
const metaEntry = this.formatMetaEntry(this.scalarName);
await this.updateOrInsertContent(metaPath, 'export default', metaEntry, 'object');
}

async createDocumentation() {
const mdxPath = path.join(
this.baseDir,
`website/src/pages/docs/scalars/${this.toKebabCase(this.scalarName)}.mdx`,
`website/src/content/scalars/${this.toKebabCase(this.scalarName)}.mdx`,
);

const mdxContent = `# ${this.scalarName}
Expand Down
28 changes: 26 additions & 2 deletions src/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
export const GeoJSON = () => 'Example GeoJSON';
export const CountryName = () => 'Example CountryName';
export const GeoJSON = () => ({
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: { name: 'Sample Point' },
geometry: {
type: 'Point',
coordinates: [12.4924, 41.8902], // e.g. near the Colosseum in Rome
},
},
{
type: 'Feature',
properties: { name: 'Sample Line' },
geometry: {
type: 'LineString',
coordinates: [
[12.4924, 41.8902],
[12.4964, 41.9028], // simple line segment
],
},
},
],
});

export const CountryName = () => 'Nigeria';
const BigIntMock = () => BigInt(Number.MAX_SAFE_INTEGER);
const ByteMock = () => new Uint8Array([1988, 1981, 1965, 1963, 1959, 1955]);
const DateMock = () => '2007-12-03';
Expand Down
267 changes: 261 additions & 6 deletions src/scalars/CountryCode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,262 @@
import { GraphQLScalarType, Kind, ValueNode } from 'graphql';
import { createGraphQLError } from '../error.js';

const COUNTRY_CODE_REGEX =
/^(AD|AE|AF|AG|AI|AL|AM|AO|AQ|AR|AS|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BJ|BL|BM|BN|BO|BQ|BR|BS|BT|BV|BW|BY|BZ|CA|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|CR|CU|CV|CW|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EE|EG|EH|ER|ES|ET|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|IO|IQ|IR|IS|IT|JE|JM|JO|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MF|MG|MH|MK|ML|MM|MN|MO|MP|MQ|MR|MS|MT|MU|MV|MW|MX|MY|MZ|NA|NC|NE|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|SS|ST|SV|SX|SY|SZ|TC|TD|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TR|TT|TV|TW|TZ|UA|UG|UM|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XK|YE|YT|ZA|ZM|ZW)$/i;
export const COUNTRY_CODES = [
'AD',
'AE',
'AF',
'AG',
'AI',
'AL',
'AM',
'AO',
'AQ',
'AR',
'AS',
'AT',
'AU',
'AW',
'AX',
'AZ',
'BA',
'BB',
'BD',
'BE',
'BF',
'BG',
'BH',
'BI',
'BJ',
'BL',
'BM',
'BN',
'BO',
'BQ',
'BR',
'BS',
'BT',
'BV',
'BW',
'BY',
'BZ',
'CA',
'CC',
'CD',
'CF',
'CG',
'CH',
'CI',
'CK',
'CL',
'CM',
'CN',
'CO',
'CR',
'CU',
'CV',
'CW',
'CX',
'CY',
'CZ',
'DE',
'DJ',
'DK',
'DM',
'DO',
'DZ',
'EC',
'EE',
'EG',
'EH',
'ER',
'ES',
'ET',
'FI',
'FJ',
'FK',
'FM',
'FO',
'FR',
'GA',
'GB',
'GD',
'GE',
'GF',
'GG',
'GH',
'GI',
'GL',
'GM',
'GN',
'GP',
'GQ',
'GR',
'GS',
'GT',
'GU',
'GW',
'GY',
'HK',
'HM',
'HN',
'HR',
'HT',
'HU',
'ID',
'IE',
'IL',
'IM',
'IN',
'IO',
'IQ',
'IR',
'IS',
'IT',
'JE',
'JM',
'JO',
'JP',
'KE',
'KG',
'KH',
'KI',
'KM',
'KN',
'KP',
'KR',
'KW',
'KY',
'KZ',
'LA',
'LB',
'LC',
'LI',
'LK',
'LR',
'LS',
'LT',
'LU',
'LV',
'LY',
'MA',
'MC',
'MD',
'ME',
'MF',
'MG',
'MH',
'MK',
'ML',
'MM',
'MN',
'MO',
'MP',
'MQ',
'MR',
'MS',
'MT',
'MU',
'MV',
'MW',
'MX',
'MY',
'MZ',
'NA',
'NC',
'NE',
'NF',
'NG',
'NI',
'NL',
'NO',
'NP',
'NR',
'NU',
'NZ',
'OM',
'PA',
'PE',
'PF',
'PG',
'PH',
'PK',
'PL',
'PM',
'PN',
'PR',
'PS',
'PT',
'PW',
'PY',
'QA',
'RE',
'RO',
'RS',
'RU',
'RW',
'SA',
'SB',
'SC',
'SD',
'SE',
'SG',
'SH',
'SI',
'SJ',
'SK',
'SL',
'SM',
'SN',
'SO',
'SR',
'SS',
'ST',
'SV',
'SX',
'SY',
'SZ',
'TC',
'TD',
'TF',
'TG',
'TH',
'TJ',
'TK',
'TL',
'TM',
'TN',
'TO',
'TR',
'TT',
'TV',
'TW',
'TZ',
'UA',
'UG',
'UM',
'US',
'UY',
'UZ',
'VA',
'VC',
'VE',
'VG',
'VI',
'VN',
'VU',
'WF',
'WS',
'XK',
'YE',
'YT',
'ZA',
'ZM',
'ZW',
] as const;

type CountryCode = (typeof COUNTRY_CODES)[number];

const COUNTRY_CODES_SET = new Set(COUNTRY_CODES);

const validate = (value: any, ast?: ValueNode) => {
if (typeof value !== 'string') {
Expand All @@ -16,7 +270,8 @@ const validate = (value: any, ast?: ValueNode) => {
);
}

if (!COUNTRY_CODE_REGEX.test(value)) {
const upper = value.toUpperCase().trim();
if (!COUNTRY_CODES_SET.has(upper as CountryCode)) {
throw createGraphQLError(
`Value is not a valid country code: ${value}`,
ast
Expand All @@ -26,7 +281,7 @@ const validate = (value: any, ast?: ValueNode) => {
: undefined,
);
}
return value;
return upper as CountryCode;
};

export const GraphQLCountryCode = /*#__PURE__*/ new GraphQLScalarType({
Expand All @@ -52,11 +307,11 @@ export const GraphQLCountryCode = /*#__PURE__*/ new GraphQLScalarType({
return validate(ast.value, ast);
},
extensions: {
codegenScalarType: 'string',
codegenScalarType: `(${COUNTRY_CODES.map(code => `"${code}"`).join(' | ')})`,
jsonSchema: {
title: 'CountryCode',
type: 'string',
pattern: COUNTRY_CODE_REGEX.source,
enum: COUNTRY_CODES,
},
},
});
Loading