You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A common foot gun I've seen happen when building client code is the misuse of what I refer to as "server types", or the entire type as defined by the schema.
On the client, with the typescript plugin, this will generate a type for User
typeUser={id: stringname: string}
And then they'll write some code like the following:
import{User}from"../generated";// bad importconstquery=gql`query user { user { id name }}`constContainer: React.FC=()=>{const{ data }=useQuery(query);returndata?.user ? <Useruser={data.user}/> : null;}constUser: React.FC<{user: User}>=({ user })=>{returnuser.name;}
This all works fine, because our client query exhausts all fields on User, but what if I extend type User { email: String! }?. Suddenly our client code is getting a type error, despite nothing changing with client code, and our schema change being backwards compat.
The true "fix" is to make sure we use the correct type, but on a large application with many developers, these mistakes are likely, and the poor usage can be quite disruptive.
Is there a better way to handle this? Is there some way to prevent generation of these types, as we don't need them on the client.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
A common foot gun I've seen happen when building client code is the misuse of what I refer to as "server types", or the entire type as defined by the schema.
With the following schema:
On the client, with the
typescript
plugin, this will generate a type forUser
And then they'll write some code like the following:
This all works fine, because our client query exhausts all fields on
User
, but what if Iextend type User { email: String! }
?. Suddenly our client code is getting a type error, despite nothing changing with client code, and our schema change being backwards compat.The true "fix" is to make sure we use the correct type, but on a large application with many developers, these mistakes are likely, and the poor usage can be quite disruptive.
Is there a better way to handle this? Is there some way to prevent generation of these types, as we don't need them on the client.
Beta Was this translation helpful? Give feedback.
All reactions