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
There's no explicit way to know what the possible values for __typename field are i.e., what schema objects implement node interface. Typescript intellisense for possible values of __typename for a node interface object only kicks in when we're returning a response object inside the node query resolver.
Let's say I have utility functions like encodeGlobalId and decodeGlobalId, how would I restrict the possible values for __typename that are passed in our given out from these functions? Currently I'd have to manually check my graphql schema definition and create something like a union type for those values manually.
With this we can restrict typenames passed or given out by those utility functions:-
// By providing the union type NodeInterfaceTypeName, we can restrict what can be passed in as first parameter inside// encodeGlobalId function. This will prevent us from passing in wrong values for __typename.constuserGlobalId=encodeGlobalId<NodeInterfaceTypeName>("__typename",userId);// By providing the union type NodeInterfaceTypeName, we can expect what can be given out as __typename inside the object// returned by decodeGlobalId function.const{ __typename, id }=decodeGlobalId<NodeInterfaceTypeName>(userGlobalId);// __typename will be type safe here and will be of NodeInterfaceTypeName union type instead of a typescript string type.if(__typename!=="User"){
...dosomething}// Or even better, NodeInterfaceTypeName can be hard coded into the utility functions themselves.
If this is something that's already possible(some typescript trick maybe?) let me know. If not this can be issued as a feature request with proper details.
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
Uh oh!
There was an error while loading. Please reload this page.
-
@n1ru4l
There's no explicit way to know what the possible values for
__typename
field are i.e., what schema objects implement node interface. Typescript intellisense for possible values of__typename
for a node interface object only kicks in when we're returning a response object inside the node query resolver.Let's say I have utility functions like
encodeGlobalId
anddecodeGlobalId
, how would I restrict the possible values for__typename
that are passed in our given out from these functions? Currently I'd have to manually check my graphql schema definition and create something like a union type for those values manually.Currently I do something like this:-
With this we can restrict typenames passed or given out by those utility functions:-
If this is something that's already possible(some typescript trick maybe?) let me know. If not this can be issued as a feature request with proper details.
Beta Was this translation helpful? Give feedback.
All reactions