返回也具有字段解析器的Appsync GraphQL类型 [英] Returning Appsync GraphQL type that also has field resolvers

查看:25
本文介绍了返回也具有字段解析器的Appsync GraphQL类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从AddImageSet突变返回ImageSet类型。但我似乎无法做到这一点,因为ImageSet类型上的每个字段都有自己的解析程序,这会覆盖解析程序映射模板。

架构:

type ImageSet {
    SetKey: ID
    SetName: String
    SetCreatedAt: AWSTimestamp
    SetDescription: String
    SetTags: [Tag]
}

type Mutation {
    AddImageSet(setName: String!, setDescription: String): ImageSet
}

以下是突变:

{
    "version" : "2017-02-28",
    "operation" : "PutItem",
    "key" : {
        "PartitionKey" : { "S" : "ImageSet" },
        "SortKey" : { "S" : "ImageSet-$util.autoId()" }
    },
    "attributeValues" : {
        "Name": { "S" : "${context.arguments.setName}" },
        "Description": { "S" : "${context.arguments.setDescription}" },
        "CreatedAt": { "N" : $util.time.nowEpochSeconds() },
    }
}

和响应映射模板

{
    "SetKey" : "$context.result.SortKey",
    "SetName" : "$context.result.Name",
    "SetCreatedAt" : $context.result.CreatedAt,
    "SetDescription" : "$context.result.Description"
}

当我运行此操作时,AddImageSet突变返回的对象将每个字段设置为NULL,因为字段解析器正在覆盖AddImageSet突变的响应映射模板。

我已经通过创建重复的"ImageSetResult"类型解决了这个问题,只是没有将解析器附加到该字段。然后,如果AddImageSet变异返回该值,则一切正常。但这些似乎有点多余。

有没有更好的方法?

推荐答案

这是GraphQL中的预期行为。如果ImageSet.SetKey上有解析器,并且Mutation.AddImageSet变异返回ImageSet类型的对象,则将调用选择集中列出的每个字段的解析器。Mutation.AddImageSet返回的值将被子解析程序返回的任何值覆盖。

要添加更多上下文,当您在ImageSet.SetKey/SetName/SetCreatedAt解析程序中时,从Mutation.AddImageSet响应映射模板返回的值将是$ctx.source的值。然后,您可以从这些解析程序执行以下操作:

#if( $ctx.source.SetKey ) "$ctx.source.SetKey" #else .. whatever SetKey does .. #end

是否需要SetKey/SetName/ETC上的解析程序?如果该对象已由Mutation.AddImageSetQuery.AddImageSet等返回,则不需要该类型的解析程序。

另一个注释。是否需要名称ImageSet.SetName,而不只是ImageSet.Name,后者是您在DynamoDB中存储它的方式?在您可能可以避免的情况下,这似乎会增加很多复杂性。

这篇关于返回也具有字段解析器的Appsync GraphQL类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆