Skip to content

Commit 15d554c

Browse files
authored
feat(custom-scalars): overload method for easy inline add (#1212)
1 parent 2705620 commit 15d554c

File tree

17 files changed

+164
-156
lines changed

17 files changed

+164
-156
lines changed

examples/35_custom-scalar/custom-scalar.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
* have arguments and data automatically encoded and decoded respectively.
44
*/
55

6-
import { Graffle } from '../../src/entrypoints/__Graffle.js'
76
import { Pokemon } from '../../tests/_/schemas/pokemon/graffle/__.js'
87
import { show } from '../$/helpers.js'
98

109
const graffle = Pokemon
1110
.create()
12-
.scalar(Graffle.Scalars.create(`Date`, {
13-
encode: (value: globalThis.Date) => value.toISOString(),
14-
decode: (value: string) => {
15-
return new globalThis.Date(value)
16-
},
17-
}))
11+
.scalar(`Date`, {
12+
decode: (value) => new globalThis.Date(value),
13+
encode: (value) => value.toISOString(),
14+
})
1815

1916
const pokemons = await graffle.query.pokemons({
2017
$: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },

examples/__outputs__/10_transport-http/transport-http_extension_headers__dynamicHeaders.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
headers: Headers {
55
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
66
'content-type': 'application/json',
7-
'x-sent-at-time': '1729695636061'
7+
'x-sent-at-time': '1729696838476'
88
},
99
signal: undefined,
1010
method: 'post',

examples/__outputs__/20_output/output_envelope.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
headers: Headers {
1919
'content-type': 'application/graphql-response+json; charset=utf-8',
2020
'content-length': '142',
21-
date: 'Wed, 23 Oct 2024 15:00:36 GMT',
21+
date: 'Wed, 23 Oct 2024 15:20:39 GMT',
2222
connection: 'keep-alive',
2323
'keep-alive': 'timeout=5'
2424
},

examples/__outputs__/60_extension/extension_opentelemetry__opentelemetry.output.txt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
}
1010
},
1111
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
12-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
13-
parentId: '996dbc7722cae52f',
12+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
13+
parentId: 'fdbedbd147dc630c',
1414
traceState: undefined,
1515
name: 'encode',
16-
id: '013ccc636ba5ab5a',
16+
id: '4f8b5fccd96cc062',
1717
kind: 0,
18-
timestamp: 1729695637401000,
19-
duration: 1347.708,
18+
timestamp: 1729696839648000,
19+
duration: 1705.875,
2020
attributes: {},
2121
status: { code: 0 },
2222
events: [],
@@ -33,14 +33,14 @@
3333
}
3434
},
3535
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
36-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
37-
parentId: '996dbc7722cae52f',
36+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
37+
parentId: 'fdbedbd147dc630c',
3838
traceState: undefined,
3939
name: 'pack',
40-
id: '9f77c7eb28b781b8',
40+
id: 'a2cb6c68bb391b81',
4141
kind: 0,
42-
timestamp: 1729695637404000,
43-
duration: 11403.833,
42+
timestamp: 1729696839652000,
43+
duration: 16711.541,
4444
attributes: {},
4545
status: { code: 0 },
4646
events: [],
@@ -57,14 +57,14 @@
5757
}
5858
},
5959
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
60-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
61-
parentId: '996dbc7722cae52f',
60+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
61+
parentId: 'fdbedbd147dc630c',
6262
traceState: undefined,
6363
name: 'exchange',
64-
id: '37e8810021b4127d',
64+
id: '95bc25186061869d',
6565
kind: 0,
66-
timestamp: 1729695637416000,
67-
duration: 28133.5,
66+
timestamp: 1729696839669000,
67+
duration: 21608.417,
6868
attributes: {},
6969
status: { code: 0 },
7070
events: [],
@@ -81,14 +81,14 @@
8181
}
8282
},
8383
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
84-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
85-
parentId: '996dbc7722cae52f',
84+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
85+
parentId: 'fdbedbd147dc630c',
8686
traceState: undefined,
8787
name: 'unpack',
88-
id: '87bd4d2329beb31f',
88+
id: 'f33a840d705729c6',
8989
kind: 0,
90-
timestamp: 1729695637445000,
91-
duration: 1657.667,
90+
timestamp: 1729696839691000,
91+
duration: 1755.75,
9292
attributes: {},
9393
status: { code: 0 },
9494
events: [],
@@ -105,14 +105,14 @@
105105
}
106106
},
107107
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
108-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
109-
parentId: '996dbc7722cae52f',
108+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
109+
parentId: 'fdbedbd147dc630c',
110110
traceState: undefined,
111111
name: 'decode',
112-
id: 'e8b9d88dc53ff408',
112+
id: 'd5abeead8c05a053',
113113
kind: 0,
114-
timestamp: 1729695637447000,
115-
duration: 214.875,
114+
timestamp: 1729696839693000,
115+
duration: 313.709,
116116
attributes: {},
117117
status: { code: 0 },
118118
events: [],
@@ -129,14 +129,14 @@
129129
}
130130
},
131131
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
132-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
132+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
133133
parentId: undefined,
134134
traceState: undefined,
135135
name: 'request',
136-
id: '996dbc7722cae52f',
136+
id: 'fdbedbd147dc630c',
137137
kind: 0,
138-
timestamp: 1729695637401000,
139-
duration: 46241.5,
138+
timestamp: 1729696839647000,
139+
duration: 46403,
140140
attributes: {},
141141
status: { code: 0 },
142142
events: [],

src/layers/6_client/properties/scalar.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ConfigManager } from '../../../lib/config-manager/__.js'
22
import type { Fluent } from '../../../lib/fluent/__.js'
3-
import type { Schema } from '../../../types/Schema/__.js'
3+
import { Schema } from '../../../types/Schema/__.js'
44
import { type ClientContext, defineProperties, type FnParametersProperty } from '../fluent.js'
55

66
export interface ScalarFn extends Fluent.FnProperty<`scalar`> {
@@ -12,8 +12,22 @@ export interface Scalar<$Args extends FnParametersProperty> {
1212
/**
1313
* TODO Docs.
1414
*/
15-
// @ts-expect-error todo
16-
<$Scalar extends Schema.Scalar.Scalar>(scalar: $Scalar): Fluent.IncrementWithStateSet<ClientContext, $Args, {
15+
// TODO limit $Name to what is in the schema.
16+
<$Name extends string, $Decoded>(name: $Name, $Codec: {
17+
decode: (value: string) => $Decoded
18+
encode: (value: $Decoded) => string
19+
}): Fluent.IncrementWithStateSet<ClientContext, $Args, {
20+
context: ConfigManager.SetAtPath<
21+
$Args['state']['context'],
22+
['scalars'],
23+
Schema.Scalar.Registry.AddScalar<$Args['state']['context']['scalars'], Schema.Scalar<$Name, $Decoded, string>>
24+
>
25+
properties: $Args['state']['properties']
26+
}>
27+
/**
28+
* TODO Docs.
29+
*/
30+
<$Scalar extends Schema.Scalar>(scalar: $Scalar): Fluent.IncrementWithStateSet<ClientContext, $Args, {
1731
context: ConfigManager.SetAtPath<
1832
$Args['state']['context'],
1933
['scalars'],
@@ -23,9 +37,15 @@ export interface Scalar<$Args extends FnParametersProperty> {
2337
}>
2438
}
2539

40+
type Arguments = [Schema.Scalar] | [string, { decode: (value: string) => any; encode: (value: any) => string }]
41+
2642
export const scalarProperties = defineProperties((builder, state) => {
2743
return {
28-
scalar: (scalar: Schema.Scalar) => {
44+
scalar: (...args: Arguments) => {
45+
const scalar = Schema.Scalar.isScalar(args[0])
46+
? args[0]
47+
: Schema.Scalar.create(args[0], args[1]!)
48+
2949
return builder({
3050
...state,
3151
scalars: {

website/content/_snippets/examples/custom-scalar/custom-scalar.detail.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55

66
<!-- dprint-ignore-start -->
77
```ts twoslash
8-
import { Graffle } from 'graffle/__Graffle'
98
import { Pokemon } from './pokemon/__.js'
109

1110
const graffle = Pokemon
1211
.create()
13-
.scalar(Graffle.Scalars.create(`Date`, {
14-
encode: (value: globalThis.Date) => value.toISOString(),
15-
decode: (value: string) => {
16-
return new globalThis.Date(value)
17-
},
18-
}))
12+
.scalar(`Date`, {
13+
decode: (value) => new globalThis.Date(value),
14+
encode: (value) => value.toISOString(),
15+
})
1916

2017
const pokemons = await graffle.query.pokemons({
2118
$: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },

website/content/_snippets/examples/custom-scalar/custom-scalar.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33

44
<!-- dprint-ignore-start -->
55
```ts twoslash
6-
import { Graffle } from 'graffle/__Graffle'
76
import { Pokemon } from './pokemon/__.js'
87

98
const graffle = Pokemon
109
.create()
11-
.scalar(Graffle.Scalars.create(`Date`, {
12-
encode: (value: globalThis.Date) => value.toISOString(),
13-
decode: (value: string) => {
14-
return new globalThis.Date(value)
15-
},
16-
}))
10+
.scalar(`Date`, {
11+
decode: (value) => new globalThis.Date(value),
12+
encode: (value) => value.toISOString(),
13+
})
1714

1815
const pokemons = await graffle.query.pokemons({
1916
$: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },

website/content/_snippets/examples/extension/opentelemetry.detail.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ console.log(data)
3737
}
3838
},
3939
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
40-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
41-
parentId: '996dbc7722cae52f',
40+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
41+
parentId: 'fdbedbd147dc630c',
4242
traceState: undefined,
4343
name: 'encode',
44-
id: '013ccc636ba5ab5a',
44+
id: '4f8b5fccd96cc062',
4545
kind: 0,
46-
timestamp: 1729695637401000,
47-
duration: 1347.708,
46+
timestamp: 1729696839648000,
47+
duration: 1705.875,
4848
attributes: {},
4949
status: { code: 0 },
5050
events: [],
@@ -64,14 +64,14 @@ console.log(data)
6464
}
6565
},
6666
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
67-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
68-
parentId: '996dbc7722cae52f',
67+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
68+
parentId: 'fdbedbd147dc630c',
6969
traceState: undefined,
7070
name: 'pack',
71-
id: '9f77c7eb28b781b8',
71+
id: 'a2cb6c68bb391b81',
7272
kind: 0,
73-
timestamp: 1729695637404000,
74-
duration: 11403.833,
73+
timestamp: 1729696839652000,
74+
duration: 16711.541,
7575
attributes: {},
7676
status: { code: 0 },
7777
events: [],
@@ -91,14 +91,14 @@ console.log(data)
9191
}
9292
},
9393
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
94-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
95-
parentId: '996dbc7722cae52f',
94+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
95+
parentId: 'fdbedbd147dc630c',
9696
traceState: undefined,
9797
name: 'exchange',
98-
id: '37e8810021b4127d',
98+
id: '95bc25186061869d',
9999
kind: 0,
100-
timestamp: 1729695637416000,
101-
duration: 28133.5,
100+
timestamp: 1729696839669000,
101+
duration: 21608.417,
102102
attributes: {},
103103
status: { code: 0 },
104104
events: [],
@@ -118,14 +118,14 @@ console.log(data)
118118
}
119119
},
120120
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
121-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
122-
parentId: '996dbc7722cae52f',
121+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
122+
parentId: 'fdbedbd147dc630c',
123123
traceState: undefined,
124124
name: 'unpack',
125-
id: '87bd4d2329beb31f',
125+
id: 'f33a840d705729c6',
126126
kind: 0,
127-
timestamp: 1729695637445000,
128-
duration: 1657.667,
127+
timestamp: 1729696839691000,
128+
duration: 1755.75,
129129
attributes: {},
130130
status: { code: 0 },
131131
events: [],
@@ -145,14 +145,14 @@ console.log(data)
145145
}
146146
},
147147
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
148-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
149-
parentId: '996dbc7722cae52f',
148+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
149+
parentId: 'fdbedbd147dc630c',
150150
traceState: undefined,
151151
name: 'decode',
152-
id: 'e8b9d88dc53ff408',
152+
id: 'd5abeead8c05a053',
153153
kind: 0,
154-
timestamp: 1729695637447000,
155-
duration: 214.875,
154+
timestamp: 1729696839693000,
155+
duration: 313.709,
156156
attributes: {},
157157
status: { code: 0 },
158158
events: [],
@@ -172,14 +172,14 @@ console.log(data)
172172
}
173173
},
174174
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
175-
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
175+
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
176176
parentId: undefined,
177177
traceState: undefined,
178178
name: 'request',
179-
id: '996dbc7722cae52f',
179+
id: 'fdbedbd147dc630c',
180180
kind: 0,
181-
timestamp: 1729695637401000,
182-
duration: 46241.5,
181+
timestamp: 1729696839647000,
182+
duration: 46403,
183183
attributes: {},
184184
status: { code: 0 },
185185
events: [],

0 commit comments

Comments
 (0)