Skip to content

Commit

Permalink
feat(client): scalar type checks name (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 31, 2024
1 parent 537a170 commit 0b7439f
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/documentBuilder/InferResult/OutputField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type GetCodecForCodecless<
$Schema extends Schema,
$Node extends Schema.Scalar.ScalarCodecless
> =
$Node['name'] extends keyof $Schema['scalars']['map']
? $Schema['scalars']['map'][$Node['name']]
$Node['name'] extends keyof $Schema['scalarRegistry']['map']
? $Schema['scalarRegistry']['map'][$Node['name']]
: Schema.Scalar.String

// dprint-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,22 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
Error: Schema.Error
Interface: Schema.Interface
}
scalars: $Scalars
scalarNamesUnion:
| 'Date'
| 'Boolean'
| 'Float'
| 'ID'
| 'Int'
| 'String'
scalars: {
Date: Schema.Date
Boolean: Schema.Boolean
Float: Schema.Float
ID: Schema.ID
Int: Schema.Int
String: Schema.String
}
scalarRegistry: $Scalars
extensions: {
SchemaErrors: {
objectNames: 'ErrorOne' | 'ErrorTwo'
Expand Down
25 changes: 23 additions & 2 deletions src/generator/generator/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,22 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
Error: Schema.Error
Interface: Schema.Interface
}
scalars: $Scalars
scalarNamesUnion:
| 'Date'
| 'Boolean'
| 'Float'
| 'ID'
| 'Int'
| 'String'
scalars: {
Date: Schema.Date
Boolean: Schema.Boolean
Float: Schema.Float
ID: Schema.ID
Int: Schema.Int
String: Schema.String
}
scalarRegistry: $Scalars
extensions: $$Utilities.GlobalRegistry.TypeExtensions
}
"
Expand Down Expand Up @@ -7688,7 +7703,13 @@ export interface Schema<
objects: {};
unions: {};
interfaces: {};
scalars: $Scalars;
scalarNamesUnion: "ID" | "Boolean" | "String";
scalars: {
ID: Schema.ID;
Boolean: Schema.Boolean;
String: Schema.String;
};
scalarRegistry: $Scalars;
extensions: $$Utilities.GlobalRegistry.TypeExtensions;
}
"
Expand Down
24 changes: 15 additions & 9 deletions src/generator/generators/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,15 @@ export const SchemaGenerator = createCodeGenerator(
({ config, code }) => {
const kindMap = config.schema.kindMap
// dprint-ignore
const root = kindMap.list.Root.map(_ => [_.name, `${identifiers.Schema}.${_.name}`])
const objects = kindMap.list.OutputObject.map(_ => [_.name, `${identifiers.Schema}.${_.name}`])
const unions = kindMap.list.Union.map(_ => [_.name, `${identifiers.Schema}.${_.name}`])
const interfaces = kindMap.list.Interface.map(_ => [_.name, `${identifiers.Schema}.${_.name}`])
const enums = kindMap.list.Enum.map(_ => [_.name, `${identifiers.Schema}.${_.name}`])
const root = kindMap.list.Root.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const)
const objects = kindMap.list.OutputObject.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const)
const unions = kindMap.list.Union.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const)
const interfaces = kindMap.list.Interface.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const)
const enums = kindMap.list.Enum.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const)
const scalars = [
...kindMap.list.ScalarCustom.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const),
...kindMap.list.ScalarStandard.map(_ => [_.name, `${identifiers.Schema}.${_.name}`] as const),
]
const operationsAvailable = entries(kindMap.index.Root).filter(_ => _[1] !== null).map(_ => _[0])
const schema: Code.TermObject = {
name: `$$Data.Name`,
Expand All @@ -290,10 +294,12 @@ export const SchemaGenerator = createCodeGenerator(
...unions,
...interfaces,
]),
objects: Object.fromEntries(objects),
unions: Object.fromEntries(unions),
interfaces: Object.fromEntries(interfaces),
scalars: `$Scalars`,
objects,
unions,
interfaces,
scalarNamesUnion: Code.tsUnionItems(scalars.map(_ => _[0]).map(Code.string)),
scalars,
scalarRegistry: `$Scalars`,
extensions: `${identifiers.$$Utilities}.GlobalRegistry.TypeExtensions`,
}

Expand Down
11 changes: 10 additions & 1 deletion src/layers/6_client/chainExtensions/scalar.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { DateScalar, FooScalar } from '../../../../tests/_/fixtures/scalars.js'
import { schemaMap } from '../../../../tests/_/schemas/kitchen-sink/graffle/__.js'
import { Graffle } from '../../../entrypoints/__Graffle.js'
import { assertEqual } from '../../../lib/assert-equal.js'
import type { SomeFunction } from '../../../lib/prelude.js'
import { any, type SomeFunction } from '../../../lib/prelude.js'
import type { TypeErrorMissingSchemaMap } from './scalar.js'

const g1 = Graffle.create({ schema: `foo` })
assertEqual<typeof g1.scalar, TypeErrorMissingSchemaMap>()

const g2 = Graffle.create({ schema: `foo`, schemaMap })
assertEqual<typeof g2.scalar, SomeFunction>()

// @ts-expect-error "Foo" is not a scalar name in the schema.
Graffle.create({ schema: `foo`, schemaMap }).scalar(`Foo`, any)
// @ts-expect-error "Foo" is not a scalar name in the schema.
Graffle.create({ schema: `foo`, schemaMap }).scalar(FooScalar)
Graffle.create({ schema: `foo`, schemaMap }).scalar(`Date`, any)
Graffle.create({ schema: `foo`, schemaMap }).scalar(DateScalar)
Graffle.create({ schema: `foo`, schemaMap }).scalar(`Int`, any)
20 changes: 14 additions & 6 deletions src/layers/6_client/chainExtensions/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Simplify } from 'type-fest'
import { Chain } from '../../../lib/chain/__.js'
import type { ConfigManager } from '../../../lib/config-manager/__.js'
import type { GlobalRegistry } from '../../../types/GlobalRegistry/GlobalRegistry.js'
import { Schema } from '../../../types/Schema/__.js'
import { type Context } from '../context.js'

Expand All @@ -24,11 +25,16 @@ type ScalarMethod<$Args extends Chain.Extension.Parameters<Scalar_>> = {
/**
* TODO Docs.
*/
// TODO limit $Name to what is in the schema.
<$Name extends string, $Decoded>(name: $Name, $Codec: {
decode: (value: string) => $Decoded
encode: (value: $Decoded) => string
}): Chain.Definition.MaterializeWithNewContext<
<
$Name extends GlobalRegistry.GetOrGeneric<$Args['context']['name']>['schema']['scalarNamesUnion'],
$Decoded,
>(
name: $Name,
$Codec: {
decode: (value: string) => $Decoded
encode: (value: $Decoded) => string
},
): Chain.Definition.MaterializeWithNewContext<
$Args['chain'],
ConfigManager.SetAtPath<
$Args['context'],
Expand All @@ -42,7 +48,9 @@ type ScalarMethod<$Args extends Chain.Extension.Parameters<Scalar_>> = {
/*
* TODO Docs.
*/
<$Scalar extends Schema.Scalar>(scalar: $Scalar): Chain.Definition.MaterializeWithNewContext<
<$Scalar extends Schema.Scalar<GlobalRegistry.GetOrGeneric<$Args['context']['name']>['schema']['scalarNamesUnion']>>(
scalar: $Scalar,
): Chain.Definition.MaterializeWithNewContext<
$Args['chain'],
ConfigManager.SetAtPath<
$Args['context'],
Expand Down
2 changes: 2 additions & 0 deletions src/lib/prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,5 @@ export type SimplifyExcept<$ExcludeType, $Type> =
: $Type extends $ExcludeType
? $Type
: {[TypeKey in keyof $Type]: $Type[TypeKey]}

export const any = undefined as any
7 changes: 5 additions & 2 deletions src/types/Schema/__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Enum } from './nodes/Enum.js'
import type { Interface } from './nodes/Interface.js'
import type { OutputObject } from './nodes/OutputObject.js'
import type { Scalar } from './nodes/Scalar/Scalar.js'
import type { ScalarCodecless } from './nodes/ScalarCodecless.js'
import type { Union } from './nodes/Union.js'
// import type { Mutation, Query, RootType, Subscription } from './StandardTypes/object.js'

Expand Down Expand Up @@ -35,9 +36,11 @@ export interface Schema<
objects: Record<string, OutputObject>
unions: Record<string, Union>
interfaces: Record<string, Interface>
scalars: Record<string, Scalar | ScalarCodecless>
scalarNamesUnion: string
/**
* A map of scalar definitions. Useful for custom scalars.
* A registry of scalar definitions. Useful for custom scalars.
*/
scalars: $Scalars
scalarRegistry: $Scalars
extensions: $Extensions
}
5 changes: 5 additions & 0 deletions tests/_/fixtures/scalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const DateScalar = Graffle.Scalars.create(`Date`, {
encode: (value: globalThis.Date) => value.toISOString(),
decode: (value: string) => new globalThis.Date(value),
})

export const FooScalar = Graffle.Scalars.create(`Foo`, {
encode: (value) => String(value),
decode: (value) => value,
})
17 changes: 16 additions & 1 deletion tests/_/schemas/kitchen-sink/graffle/modules/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,21 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
Error: Schema.Error
Interface: Schema.Interface
}
scalars: $Scalars
scalarNamesUnion:
| 'Date'
| 'Boolean'
| 'Float'
| 'ID'
| 'Int'
| 'String'
scalars: {
Date: Schema.Date
Boolean: Schema.Boolean
Float: Schema.Float
ID: Schema.ID
Int: Schema.Int
String: Schema.String
}
scalarRegistry: $Scalars
extensions: $$Utilities.GlobalRegistry.TypeExtensions
}
15 changes: 14 additions & 1 deletion tests/_/schemas/mutation-only/graffle/modules/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
objects: {}
unions: {}
interfaces: {}
scalars: $Scalars
scalarNamesUnion:
| 'Boolean'
| 'Float'
| 'ID'
| 'Int'
| 'String'
scalars: {
Boolean: Schema.Boolean
Float: Schema.Float
ID: Schema.ID
Int: Schema.Int
String: Schema.String
}
scalarRegistry: $Scalars
extensions: $$Utilities.GlobalRegistry.TypeExtensions
}
17 changes: 16 additions & 1 deletion tests/_/schemas/pokemon/graffle/modules/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,21 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
interfaces: {
Being: Schema.Being
}
scalars: $Scalars
scalarNamesUnion:
| 'Date'
| 'Boolean'
| 'Float'
| 'ID'
| 'Int'
| 'String'
scalars: {
Date: Schema.Date
Boolean: Schema.Boolean
Float: Schema.Float
ID: Schema.ID
Int: Schema.Int
String: Schema.String
}
scalarRegistry: $Scalars
extensions: $$Utilities.GlobalRegistry.TypeExtensions
}
15 changes: 14 additions & 1 deletion tests/_/schemas/query-only/graffle/modules/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
objects: {}
unions: {}
interfaces: {}
scalars: $Scalars
scalarNamesUnion:
| 'Boolean'
| 'Float'
| 'ID'
| 'Int'
| 'String'
scalars: {
Boolean: Schema.Boolean
Float: Schema.Float
ID: Schema.ID
Int: Schema.Int
String: Schema.String
}
scalarRegistry: $Scalars
extensions: $$Utilities.GlobalRegistry.TypeExtensions
}
17 changes: 16 additions & 1 deletion website/graffle/modules/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,21 @@ export interface Schema<$Scalars extends $$Utilities.Schema.Scalar.Registry = $$
interfaces: {
Being: Schema.Being;
};
scalars: $Scalars;
scalarNamesUnion:
| "Date"
| "Float"
| "ID"
| "String"
| "Int"
| "Boolean";
scalars: {
Date: Schema.Date;
Float: Schema.Float;
ID: Schema.ID;
String: Schema.String;
Int: Schema.Int;
Boolean: Schema.Boolean;
};
scalarRegistry: $Scalars;
extensions: $$Utilities.GlobalRegistry.TypeExtensions;
}

0 comments on commit 0b7439f

Please sign in to comment.