简体   繁体   中英

Can the return type be transformed for a GraphQL resolver

I have a graphene-django GraphQL API that I've added custom directives and middleware in that are very similar to what's offered by graphene-django-extras . There are several places where enum types are returned as a graphene.Int() field, I'd like to be able to use a directive to transform that a graphene.String() but it doesn't do that on its own. eg.

{
  foo {
    bar            # return Int
    # vs
    bar @readable  # return String
  }
}

Is that even possible? I'm looking through the ResolvInfo fields and there is something for the return type, but initial attempts don't work.

Edit: FWIW I don't think it matters that the API is currently developed using Python and Graphene. The question is more about whether or not GraphQL even supports this, I assume that if it does not then no language or library would make it possible.

It's not really in the spirit of graphQL to have a variable output type. So strictly speaking, you shouldn't try to do this. That being said, instead you could easily add another field for the other type, ie

type Foo {
    barStr: String
    barInt: Int
}

and then just ask for the one that you want in your query.

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