Is it possible to specify a type for scalar types during type generation? #1026
-
IssueSo when you use custom or third party scalar types as shown here: const DateScalar = scalarType({
name: 'Date',
asNexusMethod: 'date',
description: 'Date custom scalar type',
parseValue(value) {
return new Date(value)
},
serialize(value) {
return value.getTime()
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(ast.value)
}
return null
},
})
export interface NexusGenScalars {
String: string
Int: number
Float: number
Boolean: boolean
ID: string
Address: any
Date: any
JSONObject: any
} This is really unfortunate because having the types set to QuestionIs there a way to specify types for custom/third party scalar types? Taking the example above I would have liked to have following export interface NexusGenScalars {
// ...
Address: string
Date: Date
JSONObject: Record<string, any>
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After looking into this same issue myself, it appears that Here's the full function declaration: Digging into the types of I think this code will generate what you're looking for (using resolvers from graphql-scalars): export const GQLDate = asNexusMethod(GraphQLDateTime, 'dateTime', 'Date');
export const GQJson = asNexusMethod(GraphQLJSONObject, 'json', 'Record<string, any>'); In my case, I was trying to create a custom URL scalar with the built-in node 'URL' class as its TS type. Here's how I got that to work using a export const GQLUrl = asNexusMethod(GraphQLURL, 'url', {
module: 'url',
export: 'URL',
}); |
Beta Was this translation helpful? Give feedback.
After looking into this same issue myself, it appears that
asNexusMethod()
takes a 3rd argument:sourceType?: SourceTypingDef
.Here's the full function declaration:
export declare function asNexusMethod<T extends GraphQLScalarType>(scalar: T, methodName: string, sourceType?: SourceTypingDef): T;
Digging into the types of
SourceTypingDef
, you can specify the generated TS type for a scalar by providing either: the type as astring
; or an object that contains the module and export for the type (theTypingImport
interface).I think this code will generate what you're looking for (using resolvers from graphql-scalars):