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
The challenge now is, despite defining a reference resolver, the resolvers aren't aware that these fields are defined. It's using the federated entity type, rather than the defined User type (or mapped type if we had one)
Resolvers:
constresolvers: Resolvers={User: {__resolverReference: (user)=>{// expects a return type of Userreturn{id: user.id,firstName: "Bob",lastName: "Smith",};},fullName: (user)=>{returnuser.firstName+user.lastName;// property firstName does not exist},},};
My question is what's the best way to handle this? For now what we've done is define a basic type guard:
if("firstName"inuser){
...
}
But this is introducing extra runtime code (okay, a tiny amount), to perform a check that will always be true. Is there a better way?
Alternatively, could the resolver types be structured in such a way that the resolvers include the true type if a reference resolver has been defined? i.e.
typeResolverWithReference={__resolveReference: never;fullName: (parent: FederatedUser)=>string;};typeResolverWithoutReference={__resolveReference: (entity: FederatedUser)=>User;fullName: (parent: User)=>string;};// I know the types here are actually vastly more complicated, but I hope it captures my thinkingtypeResolver=ResolverWithReference|ResolverWithoutReference;
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.
-
The latest version of typescript resolvers has improved the type safety of resolvers for fields that are federated entities (thank Eddy!): #10297
In practice, what this means is that the parent argument in resolvers is a union of the federated entity or the type of the parent (or its mapper).
E.g. schema:
The challenge now is, despite defining a reference resolver, the resolvers aren't aware that these fields are defined. It's using the federated entity type, rather than the defined
User
type (or mapped type if we had one)Resolvers:
My question is what's the best way to handle this? For now what we've done is define a basic type guard:
But this is introducing extra runtime code (okay, a tiny amount), to perform a check that will always be true. Is there a better way?
Alternatively, could the resolver types be structured in such a way that the resolvers include the true type if a reference resolver has been defined? i.e.
Beta Was this translation helpful? Give feedback.
All reactions