How to implement field-level resolver with TypeScript? #1185
-
Hello, I faced with an issue which I couldn't resolve on my own. I have a simple graph of objects type Service {
...
serviceGroups: [ServiceGroup!]!
createdBy: User
}
type ServiceGroup {
...
services: [Service!]!
}
type User {
...
} I tried to represent this graph by using the @InterfaceType()
export class Node {
@Field(type => ID)
id: string
}
@ObjectType({ implements: Node })
export class Service extends Node {
@Field()
name: string
@Field()
description: string
@Field()
createdAt: Date
@Field(type => User)
createdBy: User
@Field(type => ServiceGroup)
serviceGroup: ServiceGroup
}
@ObjectType({ implements: Node })
export class ServiceGroup extends Node {
@Field()
name: string
@Field(type => [Service])
services: Service[]
}
@ObjectType({ implements: Node })
export class User extends Node {
@Field()
name: string
@Field()
email: string
}
@Injectable()
@Resolver(of => Service)
export class ServiceResolver implements ResolverInterface<Service> {
@Query(type => Service)
async service (): Promise<Service> {
// Problem: The `ResolverInterface` forces me to return a `Service` (object-type) which has `serviceGroup` and `createdBy` fields.
// How can I return these properties if they should be resolver inside field-level resolver?
// I expect to call a service here which will return a `serviceGroupId` and `creatorId` props which will be accessible via `@Root() service: Service` in field-level resolvers. But. The return type of this query forces me to put already populated objects like `serviceGroups: ServiceGroups` and `createdBy: User`, instead of `creatorId: string`, `serviceGroupId: string`
}
@FieldResolver(type => ServiceGroup)
async serviceGroup (@Root() service: Service): Promise<ServiceGroup> {
// There is no `serviceGroupId` prop in the `service` argument because of interface rules. It expect the `Service` value objects
}
@FieldResolver(type => User)
async createdBy (@Root() service: Service): Promise<User> {
// There is no `creatorId` prop in the `service` argument because of interface rules. It expect the `Service` value objects
}
} How to make it work in TypeScript world? |
Beta Was this translation helpful? Give feedback.
Answered by
MichalLytek
Jan 26, 2022
Replies: 1 comment 6 replies
-
|
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
enheit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ResolverInterface
Service
class signature gonna contain onlyids
not the relations arrays.