khazim
khazim

Reputation: 11

How make a dynamic field with apollo graphql

good I have a problem and I looked but I can't find

Well, the problem is that I would like to have a dynamic field in my GraphQL Type, the value of this field depends on the other fields if one of the other fields becomes true then the value of this field increases

need help with this problem

Upvotes: 0

Views: 605

Answers (2)

Akxe
Akxe

Reputation: 11495

GraphQL is not dynamic. You can either define multiple queries or add a custom scalar that will have a type like any.

It is somewhat a shame, but it will only allow you static types with no conditions. The only dynamic part is that you can decide what you want to download and what not. Trust me, I looked for it too.

Upvotes: 0

hp10
hp10

Reputation: 632

If you're using type-graphql, you could use FieldResolver for something like that, e.g.

UserType.ts

...
@Field(() => String)
firstName: string

@Field(() => String)
lastName: string

UserResolver.ts

...
@FieldResolver(() => String)
public fullName(@Root() userType: UserType): string {
    return `${userType.firstName} ${userType.lastName}`
}

This will create dynamic field fullName based on firstName and lastName, so you can use this analogy to create your own logic.

Upvotes: 1

Related Questions