Search code examples
graphqlaws-appsync

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


I have a theoretical question. As I know subscription parameters must exist as a field in the returning type of the mutation. This means that the type of parameter must also match the type of the field in the returning object of the mutation. Am I right? Suppose I get an array with channels ids in the mutation response. I only send one channel id as a parameter in the subscription. Is it possible to map a subscription parameter to an array at the mutation output? If the channel id exists in the array (field channelsIds), the subscription must work. Is it possible to write this logic in the scheme itself, or is it technically impossible?

GraphQL schema:

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"])
}

Solution

  • At the end of October 2020, I contacted AWS support for advice on this issue. I think this answer may be useful to someone, so I post their answer.

    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/