Skip to content

Commit 0cf2e57

Browse files
authored
Merge pull request #2975 from SeedCompany/edgedb/fix-scalar-rendering
2 parents 3026096 + 32352f5 commit 0cf2e57

File tree

6 files changed

+128
-11
lines changed

6 files changed

+128
-11
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/dist/datatypes/datetime.d.ts b/dist/datatypes/datetime.d.ts
2+
index 3f9300f558bca2a95988c2f356e0b19c504e00d1..241d7fa97e09bd47001dbaca66e8080e683117b9 100644
3+
--- a/dist/datatypes/datetime.d.ts
4+
+++ b/dist/datatypes/datetime.d.ts
5+
@@ -35,7 +35,6 @@ export declare class LocalTime {
6+
valueOf(): any;
7+
}
8+
export declare class LocalDate {
9+
- private readonly _date;
10+
constructor(isoYear: number, isoMonth: number, isoDay: number);
11+
get year(): number;
12+
get month(): number;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"cypher-query-builder": "patch:[email protected]#.yarn/patches/cypher-query-builder.patch",
6262
"dotenv": "^16.3.1",
6363
"dotenv-expand": "^10.0.0",
64-
"edgedb": "^1.4.0",
64+
"edgedb": "patch:edgedb@npm%3A1.4.0#~/.yarn/patches/edgedb-npm-1.4.0-902d833279.patch",
6565
"execa": "^8.0.1",
6666
"express": "^4.18.2",
6767
"extensionless": "^1.7.0",

src/common/temporal/calendar-date.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export class CalendarDate
9292
return CalendarDate.fromDateTime(super.invalid(reason));
9393
}
9494

95+
static now() {
96+
return CalendarDate.local();
97+
}
98+
9599
static local(
96100
year: number,
97101
month: number,
@@ -101,7 +105,7 @@ export class CalendarDate
101105
second: number,
102106
millisecond: number,
103107
opts?: DateTimeJSOptions,
104-
): DateTime;
108+
): CalendarDate;
105109
static local(
106110
year: number,
107111
month: number,
@@ -110,31 +114,35 @@ export class CalendarDate
110114
minute: number,
111115
second: number,
112116
opts?: DateTimeJSOptions,
113-
): DateTime;
117+
): CalendarDate;
114118
static local(
115119
year: number,
116120
month: number,
117121
day: number,
118122
hour: number,
119123
minute: number,
120124
opts?: DateTimeJSOptions,
121-
): DateTime;
125+
): CalendarDate;
122126
static local(
123127
year: number,
124128
month: number,
125129
day: number,
126130
hour: number,
127131
opts?: DateTimeJSOptions,
128-
): DateTime;
132+
): CalendarDate;
129133
static local(
130134
year: number,
131135
month: number,
132136
day: number,
133137
opts?: DateTimeJSOptions,
134-
): DateTime;
135-
static local(year: number, month: number, opts?: DateTimeJSOptions): DateTime;
136-
static local(year: number, opts?: DateTimeJSOptions): DateTime;
137-
static local(opts?: DateTimeJSOptions): DateTime;
138+
): CalendarDate;
139+
static local(
140+
year: number,
141+
month: number,
142+
opts?: DateTimeJSOptions,
143+
): CalendarDate;
144+
static local(year: number, opts?: DateTimeJSOptions): CalendarDate;
145+
static local(opts?: DateTimeJSOptions): CalendarDate;
138146
static local(...args: any) {
139147
const dt = super.local(...args);
140148
return CalendarDate.fromDateTime(dt);

src/common/temporal/date-time.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ declare module 'luxon/src/datetime' {
99
toNeo4JDateTime(this: DateTime): Neo.DateTime<number>;
1010
toPostgres(this: DateTime): string;
1111
[inspect.custom](): string;
12+
13+
// Compatibility with EdgeDB's LocalDate which is a subset of Temporal.PlainDate
14+
get dayOfWeek(): number;
15+
get dayOfYear(): number;
16+
get daysInWeek(): number;
17+
get monthsInYear(): number;
18+
get inLeapYear(): boolean;
1219
}
1320
}
1421
/* eslint-enable @typescript-eslint/method-signature-style */
@@ -39,3 +46,31 @@ DateTime.prototype[inspect.custom] = function (this: DateTime) {
3946
const str = this.toLocaleString(DateTime.DATETIME_SHORT_WITH_SECONDS);
4047
return `[DateTime] ${str}`;
4148
};
49+
50+
Object.defineProperties(DateTime.prototype, {
51+
dayOfWeek: {
52+
get(this: DateTime) {
53+
return this.weekday;
54+
},
55+
},
56+
dayOfYear: {
57+
get(this: DateTime) {
58+
return this.ordinal;
59+
},
60+
},
61+
daysInWeek: {
62+
get(this: DateTime) {
63+
return 7;
64+
},
65+
},
66+
monthsInYear: {
67+
get(this: DateTime) {
68+
return 12;
69+
},
70+
},
71+
inLeapYear: {
72+
get(this: DateTime) {
73+
return this.isInLeapYear;
74+
},
75+
},
76+
});

src/core/edgedb/generator/query-builder.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export async function generateQueryBuilder({
2222
});
2323
addJsExtensionDeepPathsOfEdgedbLibrary(qbDir);
2424
changeCustomScalars(qbDir);
25+
updateEdgeQLRenderingForOurCustomScalars(qbDir);
26+
updateCastMapsForOurCustomScalars(qbDir);
2527
changeImplicitIDType(qbDir);
2628
allowOrderingByEnums(qbDir);
2729
mergeDefaultTypesWithModuleNames(qbDir);
@@ -66,6 +68,57 @@ function changeImplicitIDType(qbDir: Directory) {
6668
);
6769
}
6870

71+
function updateCastMapsForOurCustomScalars(qbDir: Directory) {
72+
const file = qbDir.addSourceFileAtPath('castMaps.ts');
73+
file.insertImportDeclaration(1, {
74+
namedImports: ['DateTime'],
75+
moduleSpecifier: 'luxon',
76+
});
77+
file.insertImportDeclaration(1, {
78+
namedImports: ['CalendarDate'],
79+
moduleSpecifier: '~/common',
80+
leadingTrivia: '\n',
81+
});
82+
const updated = file
83+
.getText()
84+
// Update Luxon instances to point to correct scalar UUIDs
85+
.replace(
86+
'(type instanceof Date)',
87+
'(type instanceof Date || (type instanceof DateTime && !(type instanceof CalendarDate)))',
88+
)
89+
.replace(
90+
'(type instanceof edgedb.LocalDate)',
91+
'(type instanceof edgedb.LocalDate || type instanceof CalendarDate)',
92+
);
93+
// Attempting to pick the right type based on shape.
94+
// This doesn't fix any errors, and currently we are unable to distinguish
95+
// CalendarDate from DateTime based on shape since they are the same.
96+
// import * as _std from '../generated-client/modules/std';
97+
// .replace(
98+
// ' T extends Date ? scalarWithConstType<_std.$datetime, T> :\n',
99+
// ' T extends CalendarDate ? scalarWithConstType<_cal.$local_date, T> :\n' +
100+
// ' T extends Date | DateTime ? scalarWithConstType<_std.$datetime, T> :\n',
101+
// )
102+
file.replaceWithText(updated);
103+
}
104+
105+
function updateEdgeQLRenderingForOurCustomScalars(qbDir: Directory) {
106+
const file = qbDir.addSourceFileAtPath('toEdgeQL.ts');
107+
file.insertImportDeclaration(1, {
108+
namedImports: ['DateTime'],
109+
moduleSpecifier: 'luxon',
110+
leadingTrivia: '\n',
111+
});
112+
const condition = ' } else if (val instanceof Date) {\n';
113+
const updated = file.getText().replace(
114+
condition,
115+
` } else if (val instanceof DateTime) {
116+
stringRep = \`'\${val.toISO()}'\`;
117+
` + condition,
118+
);
119+
file.replaceWithText(updated);
120+
}
121+
69122
function allowOrderingByEnums(qbDir: Directory) {
70123
const file = qbDir.getSourceFileOrThrow('select.ts');
71124
file

yarn.lock

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5363,7 +5363,7 @@ __metadata:
53635363
debugger-is-attached: "npm:^1.2.0"
53645364
dotenv: "npm:^16.3.1"
53655365
dotenv-expand: "npm:^10.0.0"
5366-
edgedb: "npm:^1.4.0"
5366+
edgedb: "patch:edgedb@npm%3A1.4.0#~/.yarn/patches/edgedb-npm-1.4.0-902d833279.patch"
53675367
eslint: "npm:^8.52.0"
53685368
eslint-plugin-no-only-tests: "npm:^3.1.0"
53695369
eslint-plugin-typescript-sort-keys: "npm:^2.3.0"
@@ -5948,7 +5948,7 @@ __metadata:
59485948
languageName: node
59495949
linkType: hard
59505950

5951-
"edgedb@npm:^1.4.0":
5951+
"edgedb@npm:1.4.0":
59525952
version: 1.4.0
59535953
resolution: "edgedb@npm:1.4.0"
59545954
bin:
@@ -5957,6 +5957,15 @@ __metadata:
59575957
languageName: node
59585958
linkType: hard
59595959

5960+
"edgedb@patch:edgedb@npm%3A1.4.0#~/.yarn/patches/edgedb-npm-1.4.0-902d833279.patch":
5961+
version: 1.4.0
5962+
resolution: "edgedb@patch:edgedb@npm%3A1.4.0#~/.yarn/patches/edgedb-npm-1.4.0-902d833279.patch::version=1.4.0&hash=e71bbb"
5963+
bin:
5964+
edgeql-js: dist/cli.js
5965+
checksum: f6eabbf93a2e22a5b050daf7ac779eafc186297f75febbae0aa8ea2e17f09f176cee67adc937c89139082d09cf08227aec402c91a0c2f5168b677428d234425a
5966+
languageName: node
5967+
linkType: hard
5968+
59605969
"editorconfig@npm:^1.0.3":
59615970
version: 1.0.4
59625971
resolution: "editorconfig@npm:1.0.4"

0 commit comments

Comments
 (0)