简体   繁体   English

可以为 GraphQL 解析器转换返回类型吗

[英]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 .我有一个graphene-django GraphQL API 我添加了自定义指令和中间件,它们与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.有几个地方枚举类型作为graphene.Int()字段返回,我希望能够使用指令来转换它, graphene.String()但它自己不会这样做。 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.我正在查看ResolvInfo字段,返回类型有一些内容,但最初的尝试不起作用。

Edit: FWIW I don't think it matters that the API is currently developed using Python and Graphene.编辑:FWIW 我认为 API 目前是使用 Python 和石墨烯开发的并不重要。 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.问题更多的是关于 GraphQL 是否甚至支持这一点,我假设如果不支持,那么没有语言或库将使其成为可能。

It's not really in the spirit of graphQL to have a variable output type.拥有一个变量 output 类型并不真正符合 graphQL 的精神。 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.然后只需在查询中询问您想要的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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