Scalar Types
GraphQL has a set of built-in scalars that are used to represent primitive types, in @graphql-ts/schema
, these are accessible at g.Int
, g.Float
, g.String
, g.Boolean
, and g.ID
.
Custom Scalars
You can define custom scalars using the g.scalar
function. In general since you will likely want to use your custom scalars all around your schema, a common pattern is to include them in the g
object like this:
import { function gWithContext<Context>(): GWithContext<Context>
gWithContext } from "@graphql-ts/schema";import { class GraphQLError
GraphQLError } from "graphql";
const const _g: GWithContext<unknown>
_g = gWithContext<unknown>(): GWithContext<unknown>
gWithContext();
export const const g: { BigInt: GScalarType<bigint, string>; object: <Source>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: { youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true; }) => <Fields extends { [Key in keyof Fields]: GField<...>; } & InterfaceFieldsToOutputFields<...>, const Interfaces extends readonly GInterfaceType<...>[] = []>(config: { ...; } & Omit<...>) => GObjectType<...>; ... 16 more ...; Boolean: GScalarType<boolean>;}
g = { ...const _g: GWithContext<unknown>
_g, type BigInt: GScalarType<bigint, string>
BigInt: const _g: GWithContext<unknown>
_g.scalar: <bigint, string>(config: GraphQLScalarTypeConfig<bigint, string>) => GScalarType<bigint, string>
scalar({ GraphQLScalarTypeConfig<TInternal, TExternal>.name: string
name: "BigInt", GraphQLScalarTypeConfig<bigint, string>.serialize?: GraphQLScalarSerializer<string>
serialize(value: unknown
value) { if (typeof value: unknown
value === "bigint") { return value: bigint
value.BigInt.toString(radix?: number): string
toString(); } throw new new GraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError (+1 overload)
GraphQLError( "BigInt cannot represent non-bigint value: " + value: unknown
value ); }, GraphQLScalarTypeConfig<bigint, string>.parseValue?: GraphQLScalarValueParser<bigint>
parseValue(value: unknown
value) { if (typeof value: unknown
value === "bigint") { return value: bigint
value; } if (typeof value: unknown
value === "string") { return var BigInt: BigIntConstructor(value: bigint | boolean | number | string) => bigint
BigInt(value: string
value); } throw new new GraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError (+1 overload)
GraphQLError("BigInt must be a string"); }, GraphQLScalarTypeConfig<bigint, string>.parseLiteral?: GraphQLScalarLiteralParser<bigint>
parseLiteral(ast: ValueNode
ast) { if (ast: ValueNode
ast.kind: Kind.VARIABLE | Kind.INT | Kind.FLOAT | Kind.STRING | Kind.BOOLEAN | Kind.NULL | Kind.ENUM | Kind.LIST | Kind.OBJECT
kind === "StringValue") { return var BigInt: BigIntConstructor(value: bigint | boolean | number | string) => bigint
BigInt(ast: StringValueNode
ast.StringValueNode.value: string
value); } throw new new GraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError (+1 overload)
GraphQLError("BigInt must be a string"); }, }),};export type type g<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
g<function (type parameter) T in type g<T>
T> = (alias) namespace gWithContextimport gWithContext
gWithContext.type gWithContext.infer<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
infer<function (type parameter) T in type g<T>
T>;
scalar BigInt
Unlike all other GraphQL types in @graphql-ts/schema
, the TypeScript types for scalars are exactly the same in @graphql-ts/schema
as GraphQL.js so you can use GraphQLScalarType
from GraphQL.js interchangably with g.scalar
/GScalarType
, For example, the above code can be rewritten using GraphQLScalarType
:
import { function gWithContext<Context>(): GWithContext<Context>
gWithContext } from "@graphql-ts/schema";import { class GraphQLError
GraphQLError, class GraphQLScalarType<TInternal = unknown, TExternal = TInternal>
GraphQLScalarType } from "graphql";
export const const g: { BigInt: GraphQLScalarType<bigint, string>; object: <Source>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: { youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true; }) => <Fields extends { [Key in keyof Fields]: GField<...>; } & InterfaceFieldsToOutputFields<...>, const Interfaces extends readonly GInterfaceType<...>[] = []>(config: { ...; } & Omit<...>) => GObjectType<...>; ... 16 more ...; Boolean: GScalarType<boolean>;}
g = { ...gWithContext<unknown>(): GWithContext<unknown>
gWithContext(), type BigInt: GraphQLScalarType<bigint, string>
BigInt: new new GraphQLScalarType<bigint, string>(config: Readonly<GraphQLScalarTypeConfig<bigint, string>>): GraphQLScalarType<bigint, string>
GraphQLScalarType({ name: string
name: "BigInt", serialize?: GraphQLScalarSerializer<string>
serialize(value: unknown
value) { if (typeof value: unknown
value === "bigint") { return value: bigint
value.BigInt.toString(radix?: number): string
toString(); } throw new new GraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError (+1 overload)
GraphQLError( "BigInt cannot represent non-bigint value: " + value: unknown
value ); }, parseValue?: GraphQLScalarValueParser<bigint>
parseValue(value: unknown
value) { if (typeof value: unknown
value === "bigint") { return value: bigint
value; } if (typeof value: unknown
value === "string") { return var BigInt: BigIntConstructor(value: bigint | boolean | number | string) => bigint
BigInt(value: string
value); } throw new new GraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError (+1 overload)
GraphQLError("BigInt must be a string"); }, parseLiteral?: GraphQLScalarLiteralParser<bigint>
parseLiteral(ast: ValueNode
ast) { if (ast: ValueNode
ast.kind: Kind.VARIABLE | Kind.INT | Kind.FLOAT | Kind.STRING | Kind.BOOLEAN | Kind.NULL | Kind.ENUM | Kind.LIST | Kind.OBJECT
kind === "StringValue") { return var BigInt: BigIntConstructor(value: bigint | boolean | number | string) => bigint
BigInt(ast: StringValueNode
ast.StringValueNode.value: string
value); } throw new new GraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError (+1 overload)
GraphQLError("BigInt must be a string"); }, }),};export type type g<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
g<function (type parameter) T in type g<T>
T> = (alias) namespace gWithContextimport gWithContext
gWithContext.type gWithContext.infer<T> = T extends () => (args: any) => infer R ? R : T extends (args: any) => infer R ? R : never
infer<function (type parameter) T in type g<T>
T>;
scalar BigInt