简体   繁体   中英

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM