-
I am having trouble with the loadable node builder. Specifically, I am confused about how to use classes to get isTypeOf checking using the Node Builder. If I try and pass the class to the builder (like with objects), I get the error that Is there something obvious I am doing wrong? How should I implement this? export class User {
id: string
firstName: string
lastName: string
name: string
email: string
constructor(id: string, name: string, email: string, firstName: string, lastName: string) {
this.id = id
this.firstName = firstName
this.lastName = lastName
this.name = name
this.email = email
}
}
const UserNode = builder.loadableNode('User', {
id: {
resolve: (user) => user.id
},
isTypeOf: (obj: Record<string, unknown>) => obj instanceof User,
fields: (t) => ({
firstName: t.string({
resolve: (src) => src.firstName
}),
lastName: t.string({
resolve: (src) => src.lastName,
nullable: true
}),
name: t.string({
resolve: (src) => (src.lastName ? `${src.firstName} ${src.lastName}` : src.firstName)
}),
email: t.string({
resolve: (src) => src.email
}),
}),
load: async (ids: string[], context: ContextType): Promise<User[]> => {
...
},
}) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I've tried to reproduce this, but it seems like everything is working as intended. Can you add some console.logs into your isTypeOf handlers to see that it is being called, and that the check is returning true? |
Beta Was this translation helpful? Give feedback.
-
The answer for anyone looking is that your load function has to return a class instance. So if your fetch call does not do it automatically, you need to return the class -- i.e. |
Beta Was this translation helpful? Give feedback.
The answer for anyone looking is that your load function has to return a class instance. So if your fetch call does not do it automatically, you need to return the class -- i.e.
return new User({ ...properties })