Custom scalars helper method? #568
-
Hi, I was wondering if it's possible to extends Example of a custom // no helper
t.field({
type: 'DateTime',
resolve: () => new Date(),
})
// helper
t.datetime({ resolve: () => new Date() }) Something similar to this in Nexus https://nexusjs.org/docs/api/scalar-type#exposing-scalar-as-method |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This doesn't exist as an official feature right now, but it's possible to extend things yourself. The types involved in doing this correctly are fairly complex though because of interactions with various plugins. You should be able to just use the following (and modify the list of custom scalars at the top to suite your needs). You can put this in a file, and simply import it from the file that creates your builder: import {
CompatibleTypes,
FieldBuilder,
FieldKind,
FieldNullability,
FieldOptionsFromKind,
FieldRef,
InputFieldMap,
NormalizeArgs,
OutputType,
RootFieldBuilder,
SchemaTypes,
ShapeFromTypeParam,
} from '@pothos/core';
// Add scalar types here to create field helpers
const scalars = {
dateTime: 'DateTime',
} as const;
type CustomScalars = typeof scalars;
type ScalarHelpers<Types extends SchemaTypes, ParentShape, Kind extends FieldKind> = {
[K in keyof CustomScalars]: <
Args extends InputFieldMap,
ResolveShape,
ResolveReturnShape,
Nullable extends FieldNullability<Type> = Types['DefaultFieldNullability'],
Type extends OutputType<Types> = CustomScalars[K] extends OutputType<Types>
? CustomScalars[K]
: never,
>(
options: Omit<
FieldOptionsFromKind<
Types,
ParentShape,
Type,
Nullable,
Args,
Kind,
ResolveShape,
ResolveReturnShape
>,
'type'
>,
) => FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind>;
};
type ScalarListHelpers<Types extends SchemaTypes, ParentShape, Kind extends FieldKind> = {
[K in keyof CustomScalars as `${K}List`]: <
Args extends InputFieldMap,
ResolveShape,
ResolveReturnShape,
Nullable extends FieldNullability<Type> = Types['DefaultFieldNullability'],
Type extends [OutputType<Types>] = CustomScalars[K] extends OutputType<Types>
? [CustomScalars[K]]
: never,
>(
options: Omit<
FieldOptionsFromKind<
Types,
ParentShape,
Type,
Nullable,
Args,
Kind,
ResolveShape,
ResolveReturnShape
>,
'type'
>,
) => FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind>;
};
type ScalarExposeHelpers<Types extends SchemaTypes, ParentShape, Kind extends FieldKind> = {
[K in keyof CustomScalars as `expose${Capitalize<K>}`]: <
Name extends CompatibleTypes<Types, ParentShape, Type, Nullable>,
ResolveReturnShape,
Nullable extends FieldNullability<Type> = Types['DefaultFieldNullability'],
Type extends OutputType<Types> = CustomScalars[K] extends OutputType<Types>
? CustomScalars[K]
: never,
>(
...args: NormalizeArgs<
[
name: Name,
options?: Omit<
FieldOptionsFromKind<
Types,
ParentShape,
Type,
Nullable,
{},
Kind,
ParentShape,
ResolveReturnShape
>,
'resolve' | 'type'
>,
]
>
) => FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind>;
};
type ScalarExposeListHelpers<Types extends SchemaTypes, ParentShape, Kind extends FieldKind> = {
[K in keyof CustomScalars as `expose${Capitalize<K>}List`]: <
Name extends CompatibleTypes<Types, ParentShape, Type, Nullable>,
ResolveReturnShape,
Nullable extends FieldNullability<Type> = Types['DefaultFieldNullability'],
Type extends [OutputType<Types>] = CustomScalars[K] extends OutputType<Types>
? [CustomScalars[K]]
: never,
>(
...args: NormalizeArgs<
[
name: Name,
options?: Omit<
FieldOptionsFromKind<
Types,
ParentShape,
Type,
Nullable,
{},
Kind,
ParentShape,
ResolveReturnShape
>,
'resolve' | 'type'
>,
]
>
) => FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind>;
};
declare global {
export namespace PothosSchemaTypes {
export interface RootFieldBuilder<
Types extends SchemaTypes,
ParentShape,
Kind extends FieldKind = FieldKind,
> extends ScalarHelpers<Types, ParentShape, Kind>,
ScalarListHelpers<Types, ParentShape, Kind> {}
export interface FieldBuilder<
Types extends SchemaTypes,
ParentShape,
Kind extends FieldKind = FieldKind,
> extends ScalarExposeHelpers<Types, ParentShape, Kind>,
ScalarExposeListHelpers<Types, ParentShape, Kind> {}
}
}
for (const scalar of Object.keys(scalars)) {
RootFieldBuilder.prototype[scalar] = function scalarHelper(
this: PothosSchemaTypes.RootFieldBuilder<SchemaTypes, unknown>,
options: {},
) {
return this.field({
type: scalars[scalar as keyof typeof scalars],
...options,
} as never);
};
RootFieldBuilder.prototype[`${scalar}List`] = function scalarListHelper(
this: PothosSchemaTypes.RootFieldBuilder<SchemaTypes, unknown>,
options: {},
) {
return this.field({
type: [scalars[scalar as keyof typeof scalars]],
...options,
} as never);
};
FieldBuilder.prototype[`expose${scalar.slice(0, 1).toUpperCase()}${scalar.slice(1)}`] =
function scalarExposeHelper(
this: PothosSchemaTypes.FieldBuilder<SchemaTypes, unknown>,
name: never,
options: {},
) {
return this.expose(name, {
type: scalars[scalar as keyof typeof scalars],
...options,
} as never) as never;
};
FieldBuilder.prototype[`expose${scalar.slice(0, 1).toUpperCase()}${scalar.slice(1)}List`] =
function scalarExposeHelper(
this: PothosSchemaTypes.FieldBuilder<SchemaTypes, unknown>,
name: never,
options: {},
) {
return this.expose(name, {
type: [scalars[scalar as keyof typeof scalars]],
...options,
} as never) as never;
};
} |
Beta Was this translation helpful? Give feedback.
This doesn't exist as an official feature right now, but it's possible to extend things yourself. The types involved in doing this correctly are fairly complex though because of interactions with various plugins.
You should be able to just use the following (and modify the list of custom scalars at the top to suite your needs). You can put this in a file, and simply import it from the file that creates your builder: