Hey,
I am pretty new to TypeScript and I am struggling to wrap my head around the type definitions.
I have setup GraphQL with automated type generation and that works pretty well.
Now I am wondering: Is it “enough” type checking, if I check the query results against the generated types or do I need to implement this much more detailed on every function I am using the data?
This topic has been created from a Discord post (1274650852086251531) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord
a better practice is to use the generated types as reference of what the api gives
pass your api data through a mapper function that giives the results in the types used for your app
with this you make sure that you have 100% you need
if api changes and types generated changes you wont be going through all the app fixing types
you will only edit the mappers
and the mapper function will also play the role of doing a fall back behaviour if certain data does nto exists
example
type ProductGeneratedType = {
id:string
name:string
code:string
description:string | undefined
collectionId:string
variants: VariantGeneratedType[]
}
type AppProduct {
id:string
name:string
description:string
}
function fetchOneProductMapper(product: ProductGeneratedType):AppProduct {
return {
id:product.id
name:product.name
description: product.description ?? ''
}
}