Skip to content

Commit

Permalink
feat(custom-scalars): overload method for easy inline add (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 23, 2024
1 parent 2705620 commit 15d554c
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 156 deletions.
11 changes: 4 additions & 7 deletions examples/35_custom-scalar/custom-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
* have arguments and data automatically encoded and decoded respectively.
*/

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

const graffle = Pokemon
.create()
.scalar(Graffle.Scalars.create(`Date`, {
encode: (value: globalThis.Date) => value.toISOString(),
decode: (value: string) => {
return new globalThis.Date(value)
},
}))
.scalar(`Date`, {
decode: (value) => new globalThis.Date(value),
encode: (value) => value.toISOString(),
})

const pokemons = await graffle.query.pokemons({
$: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1729695636061'
'x-sent-at-time': '1729696838476'
},
signal: undefined,
method: 'post',
Expand Down
2 changes: 1 addition & 1 deletion examples/__outputs__/20_output/output_envelope.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
headers: Headers {
'content-type': 'application/graphql-response+json; charset=utf-8',
'content-length': '142',
date: 'Wed, 23 Oct 2024 15:00:36 GMT',
date: 'Wed, 23 Oct 2024 15:20:39 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=5'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'encode',
id: '013ccc636ba5ab5a',
id: '4f8b5fccd96cc062',
kind: 0,
timestamp: 1729695637401000,
duration: 1347.708,
timestamp: 1729696839648000,
duration: 1705.875,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -33,14 +33,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'pack',
id: '9f77c7eb28b781b8',
id: 'a2cb6c68bb391b81',
kind: 0,
timestamp: 1729695637404000,
duration: 11403.833,
timestamp: 1729696839652000,
duration: 16711.541,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -57,14 +57,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'exchange',
id: '37e8810021b4127d',
id: '95bc25186061869d',
kind: 0,
timestamp: 1729695637416000,
duration: 28133.5,
timestamp: 1729696839669000,
duration: 21608.417,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -81,14 +81,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'unpack',
id: '87bd4d2329beb31f',
id: 'f33a840d705729c6',
kind: 0,
timestamp: 1729695637445000,
duration: 1657.667,
timestamp: 1729696839691000,
duration: 1755.75,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -105,14 +105,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'decode',
id: 'e8b9d88dc53ff408',
id: 'd5abeead8c05a053',
kind: 0,
timestamp: 1729695637447000,
duration: 214.875,
timestamp: 1729696839693000,
duration: 313.709,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -129,14 +129,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: undefined,
traceState: undefined,
name: 'request',
id: '996dbc7722cae52f',
id: 'fdbedbd147dc630c',
kind: 0,
timestamp: 1729695637401000,
duration: 46241.5,
timestamp: 1729696839647000,
duration: 46403,
attributes: {},
status: { code: 0 },
events: [],
Expand Down
28 changes: 24 additions & 4 deletions src/layers/6_client/properties/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ConfigManager } from '../../../lib/config-manager/__.js'
import type { Fluent } from '../../../lib/fluent/__.js'
import type { Schema } from '../../../types/Schema/__.js'
import { Schema } from '../../../types/Schema/__.js'
import { type ClientContext, defineProperties, type FnParametersProperty } from '../fluent.js'

export interface ScalarFn extends Fluent.FnProperty<`scalar`> {
Expand All @@ -12,8 +12,22 @@ export interface Scalar<$Args extends FnParametersProperty> {
/**
* TODO Docs.
*/
// @ts-expect-error todo
<$Scalar extends Schema.Scalar.Scalar>(scalar: $Scalar): Fluent.IncrementWithStateSet<ClientContext, $Args, {
// TODO limit $Name to what is in the schema.
<$Name extends string, $Decoded>(name: $Name, $Codec: {
decode: (value: string) => $Decoded
encode: (value: $Decoded) => string
}): Fluent.IncrementWithStateSet<ClientContext, $Args, {
context: ConfigManager.SetAtPath<
$Args['state']['context'],
['scalars'],
Schema.Scalar.Registry.AddScalar<$Args['state']['context']['scalars'], Schema.Scalar<$Name, $Decoded, string>>
>
properties: $Args['state']['properties']
}>
/**
* TODO Docs.
*/
<$Scalar extends Schema.Scalar>(scalar: $Scalar): Fluent.IncrementWithStateSet<ClientContext, $Args, {
context: ConfigManager.SetAtPath<
$Args['state']['context'],
['scalars'],
Expand All @@ -23,9 +37,15 @@ export interface Scalar<$Args extends FnParametersProperty> {
}>
}

type Arguments = [Schema.Scalar] | [string, { decode: (value: string) => any; encode: (value: any) => string }]

export const scalarProperties = defineProperties((builder, state) => {
return {
scalar: (scalar: Schema.Scalar) => {
scalar: (...args: Arguments) => {
const scalar = Schema.Scalar.isScalar(args[0])
? args[0]
: Schema.Scalar.create(args[0], args[1]!)

return builder({
...state,
scalars: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

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

const graffle = Pokemon
.create()
.scalar(Graffle.Scalars.create(`Date`, {
encode: (value: globalThis.Date) => value.toISOString(),
decode: (value: string) => {
return new globalThis.Date(value)
},
}))
.scalar(`Date`, {
decode: (value) => new globalThis.Date(value),
encode: (value) => value.toISOString(),
})

const pokemons = await graffle.query.pokemons({
$: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@

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

const graffle = Pokemon
.create()
.scalar(Graffle.Scalars.create(`Date`, {
encode: (value: globalThis.Date) => value.toISOString(),
decode: (value: string) => {
return new globalThis.Date(value)
},
}))
.scalar(`Date`, {
decode: (value) => new globalThis.Date(value),
encode: (value) => value.toISOString(),
})

const pokemons = await graffle.query.pokemons({
$: { filter: { birthday: { lte: new Date(`1987-01-13`) } } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ console.log(data)
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'encode',
id: '013ccc636ba5ab5a',
id: '4f8b5fccd96cc062',
kind: 0,
timestamp: 1729695637401000,
duration: 1347.708,
timestamp: 1729696839648000,
duration: 1705.875,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -64,14 +64,14 @@ console.log(data)
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'pack',
id: '9f77c7eb28b781b8',
id: 'a2cb6c68bb391b81',
kind: 0,
timestamp: 1729695637404000,
duration: 11403.833,
timestamp: 1729696839652000,
duration: 16711.541,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -91,14 +91,14 @@ console.log(data)
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'exchange',
id: '37e8810021b4127d',
id: '95bc25186061869d',
kind: 0,
timestamp: 1729695637416000,
duration: 28133.5,
timestamp: 1729696839669000,
duration: 21608.417,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -118,14 +118,14 @@ console.log(data)
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'unpack',
id: '87bd4d2329beb31f',
id: 'f33a840d705729c6',
kind: 0,
timestamp: 1729695637445000,
duration: 1657.667,
timestamp: 1729696839691000,
duration: 1755.75,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -145,14 +145,14 @@ console.log(data)
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
parentId: '996dbc7722cae52f',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: 'fdbedbd147dc630c',
traceState: undefined,
name: 'decode',
id: 'e8b9d88dc53ff408',
id: 'd5abeead8c05a053',
kind: 0,
timestamp: 1729695637447000,
duration: 214.875,
timestamp: 1729696839693000,
duration: 313.709,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -172,14 +172,14 @@ console.log(data)
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: 'a06cc237fb19a0f3f72a6b58c7a1c197',
traceId: '9d3e044ef7452a5b0e773ac8a7e6c8bd',
parentId: undefined,
traceState: undefined,
name: 'request',
id: '996dbc7722cae52f',
id: 'fdbedbd147dc630c',
kind: 0,
timestamp: 1729695637401000,
duration: 46241.5,
timestamp: 1729696839647000,
duration: 46403,
attributes: {},
status: { code: 0 },
events: [],
Expand Down
Loading

0 comments on commit 15d554c

Please sign in to comment.