import { GraphQLScalarType, Kind, GraphQLError } from 'graphql'; export type RootResolver = { [P in keyof T]: T[P] extends (args: infer Args) => infer R ? (args: Args, ctx: Context) => Return : never }; export type QueryParameters = { [P in keyof Q]: Q[P] extends (args: infer Args) => unknown ? Args : never }; // type PromisifyObj = { [P in keyof T]: PromisifyValue }; // type Obj = PromisifyObj | Promise> | (() => PromisifyObj) | (() => Promise>); // type PromisifyPrimitive = T | Promise | (() => T) | (() => Promise); // type PromisifyValue = [T] extends [object] // ? (T extends Date ? PromisifyPrimitive : Obj) // : PromisifyPrimitive; // export type Return = Promise<[T] extends [object] ? ([T] extends [Date] ? Date : PromisifyObj) : T>; export type Return = Promise; export function fromPromise(val: Promise | (() => Promise) | T) { return (val as unknown) as T extends Array> ? V[] : T extends Array<() => Promise> ? V : T; } const customTypeMap = new Map(); export const graphQLBigintTypeFactory = (typeName: string) => { if (!/ID$/.test(typeName)) return; if (customTypeMap.has(typeName)) return customTypeMap.get(typeName); const type = new GraphQLScalarType({ name: typeName, serialize: value, parseValue: value, parseLiteral(ast) { if (ast.kind === Kind.STRING) { return value(ast.value); } return null; }, }); customTypeMap.set(typeName, type); return type; function value(value: string) { try { if (BigInt(value) === 0n) throw 1; } catch (e) { throw new GraphQLError(`${typeName} should be numeric string: ${value}`); } return value; } };