是否可以将订阅参数映射到变异输出中的数组?

Is it possible to map a subscription parameter to an array at the mutation output?

我有一个理论问题。据我所知,订阅参数必须作为突变返回类型中的一个字段存在。这意味着参数的类型也必须与突变的返回对象中的字段类型相匹配。我对吗?假设我在突变响应中得到一个带有通道 ID 的数组。我只发送一个频道 ID 作为订阅中的参数。是否可以将订阅参数映射到变异输出中的数组?如果频道 ID 存在于数组中(字段 channelsIds),则订阅必须有效。是不是可以把这个逻辑写在scheme本身,或者技术上是不可能的?

GraphQL 架构:

schema {
    mutation: Mutation
    subscription: Subscription
}

type Mutation {
    testMutation(input: TestMutationInput): TestMutationOutput
}

type TestMutationOutput {
    channelsIds: [String!]!
    userId: String!
    userEmail: String
    userPhoneNumber: String
}

type Subscription {
    watchTestMutation(channelId: String!): TestMutationOutput
        @aws_subscribe(mutations: ["testMutation"])
}

如果我对你的理解是正确的,你想根据突变的返回值是否在作为参数传递给订阅的数组中进行过滤。很抱歉,目前无法做到这一点。订阅筛选器仅评估为真或假,不能容纳除此之外的任何逻辑。

2020 年 10 月末,我联系了 AWS 支持部门寻求有关此问题的建议。我认为这个答案可能对某人有用,所以我 post 他们的答案。

Please allow me to inform you that the use-case that you have mentioned in the case is currently not possible via AppSync. I understand that the lack of the feature may be causing inconvenience. There is an internal feature request already with the AppSync team to incorporate this feature and I have added a +1 on your behalf. It is worth noting, that once this feature request is with the team, it will be up to the team as to if/when this potential infrastructure feature is implemented, and because of the limited visibility into the progress of internal development processes, I won’t be able to provide an ETA regarding its release. I would request you to keep an eye on the what's new page or the AWS Blogs as all new feature requests and enhancements are posted there[1-3].

However we can suggest a couple of workarounds in this case:

  1. Filter the required fields on client side itself after receiving the values on the client-side from AppSync.

  2. If the values to be filtered are very limited we can use a fake mutation made with the help of a resolver mapped to “None” Data source. In this flow, we would create a lambda function that uses a DynamoDB stream as the trigger. The Lambda function is triggered whenever there's an update to the DynamoDB table.
    
We can then include logic in the Lambda function to filter the required fields and perform a mutation to AppSync. In AppSync, the mutation which was called by lambda would configured using a resolver mapped to a “None” Data source. The None data source type passes the request mapping template directly to the response mapping template. And when we subscribe to this mutation, we will directly get the filtered data from Lambda that was used to call this mutation. Please refer to [4] for a step-by-step description of this process.

But please note that this workaround is cumbersome and would require a lot of changes if the required field values keep changing. Workaround 1(handling it on the client-side) is usually the preferred way to handle this use-case.

Resources:

[1] https://blogs.amazon.com/

[2] https://aws.amazon.com/new/

[3] https://aws.amazon.com/releasenotes/

[4] https://aws.amazon.com/premiumsupport/knowledge-center/appsync-notify-subscribers-real-time/