Ariadne 中联合类型的解析器函数

Posted

技术标签:

【中文标题】Ariadne 中联合类型的解析器函数【英文标题】:Resolver function for union type in Ariadne 【发布时间】:2019-11-24 21:54:39 【问题描述】:

我正在尝试为 Ariadne 中的联合类型编写查询解析器函数。我怎样才能做到这一点?

正如我在documentation 中所读到的,有一个名为__typename 的字段可以帮助我们解析联合类型。但是我的解析器功能没有得到任何__typename

架构

type User 
  username: String!
  firstname: String
  email: String


type UserDuplicate 
  username: String!
  firstname: String
  email: String


union UnionTest = User | UserDuplicate

type UnionForCustomTypes 
  user: UnionTest
  name: String!


type Query 
  user: String!
  unionForCustomTypes: [UnionForCustomTypes]!


Ariadne 解析器函数

query = QueryType()
mutation = MutationType()
unionTest = UnionType("UnionTest")


@unionTest.type_resolver
def resolve_union_type(obj, *_):
    if obj[0]["__typename"] == "User":
        return "User"
    if obj[0]["__typename"] == "DuplicateUser":
        return "DuplicateUser"

    return None


# Query resolvers
@query.field("unionForCustomTypes")
def resolve_union_for_custom_types(_, info):
    result = [
        "name": "Manisha Bayya", "user": ["__typename": "User", "username": "abcd"]
    ]   
    return result

我正在尝试的查询


  unionForCustomTypes 
    name
    user 
      __typename
      ...on User 
        username
        firstname
      
    
  

当我尝试查询时出现以下错误


  "data": null,
  "errors": [
    
      "message": "Cannot return null for non-nullable field Query.unionForCustomTypes.",
      "locations": [
        [
          2,
          3
        ]
      ],
      "path": [
        "unionForCustomTypes"
      ],
      "extensions": 
        "exception": 
          "stacktrace": [
            "Traceback (most recent call last):",
            "  File \"/root/manisha/prisma/ariadne_envs/lib/python3.6/site-packages/graphql/execution/execute.py\", line 675, in complete_value_catching_error",
            "    return_type, field_nodes, info, path, result",
            "  File \"/root/manisha/prisma/ariadne_envs/lib/python3.6/site-packages/graphql/execution/execute.py\", line 754, in complete_value",
            "    \"Cannot return null for non-nullable field\"",
            "TypeError: Cannot return null for non-nullable field Query.unionForCustomTypes."
          ],
          "context": 
            "completed": "None",
            "result": "None",
            "path": "ResponsePath(...rCustomTypes')",
            "info": "GraphQLResolv...f04e9c1fc50>)",
            "field_nodes": "[FieldNode at 4:135]",
            "return_type": "<GraphQLNonNu...ustomTypes'>>>",
            "self": "<graphql.exec...x7f04e75677f0>"
          
        
      
    
  ]

【问题讨论】:

【参考方案1】:

我们不需要任何联合类型的解析器。我们可以在返回owner 字段的同时发送__typename 字段。在我的代码中,我正在返回错误的 owner 属性列表。我只需要寄一本字典。

以下是我对代码所做的更改以使其正常工作。

# Deleted resolver for UnionType

@query.field("unionForCustomTypes")
def resolve_union_for_custom_types(_, info):
    result = ["name": "Manisha Bayya", "user": "__typename": "User", "username": "abcd", "firstname": "pqrs"] # <-- Line changed

    return result

【讨论】:

以上是关于Ariadne 中联合类型的解析器函数的主要内容,如果未能解决你的问题,请参考以下文章

Swift函数式编程十一(解析器组合算子)

Swift函数式编程十一(解析器组合算子)

如何在 AST 解析器中将类型解析为原语

输入 graphql 解析器函数

Next JS api 路由类型为 graphql 错误(解析器不是函数)

AWS AppSync Lambda 解析器获取查询返回类型