skip to Main Content

I have a schema that looks like this:

type Mutation {
    createEvent(input: CreateEventInput!): CreateEventPayload
}

type Subscription {
    onCreateEvent(filter: EventFilter): CreateEventPayload
        @aws_subscribe(mutations: ["createEvent"])
}

type CreateEventPayload {
    event: Event
    clientMutationId: String
}

type Event implements Node {
    description: String
    eventName: String!
    id: String!
    status: String!
}

interface Node {
    nodeId: ID!
}

input EventFilter {
    description: StringFilter
    eventName: StringFilter
    id: StringFilter
    status: StringFilter
}

What I want to do is subscribe to any event that has the same id when mutation executed.

For example here is my subscription:

subscription MySub {
  onCreateEvent(filter: {id: {equalTo: "xxx"}}) {
    event {
      id
      eventName
    }
  }
}

And here is my Mutation:

mutation {
  createEvent(input: {aiEvent: {id: "xxx", eventName: "USER_ENTRY", status: "1"}}) {
    event {
      id
      eventName
    }
  }
}

After I fired Mutation, it gave me the expected result as below:

{
  "data": {
    "createEvent": {
      "event": {
        "id": "xxx",
        "eventName": "USER_ENTRY",

      }
    }
  }
}

But it is not firing the subscription, is it because my id is eventually nested under type event and the response of Mutation is CreateEventPayload while the type event is nested under it?

Does the filtering accept nested field or is there something else I did wrong?

2

Answers


  1. Your GraphQL schema seems to be correct for AWS AppSync, and your mutation and subscription operations are also correct based on the schema.

    Consider the following:

    • Ensure that the client maintaining the subscription is maintaining an active network connection with the server.

    • Correct filter – It seems like you are correctly setting up your filter to match the id field in your subscription. Make sure that the id value in the mutation matches the id value in the subscription filter.

    • AWS AppSync limitations – AWS AppSync does not support subscriptions on nested fields. Only top-level fields can be used in the subscription filter. However, in your case, you’re filtering on the id field, which should be a top-level field in the Event type. So, this should not be the issue here.

    • Operation order – The subscription needs to be active before the mutation happens, otherwise, it will not get the mutation data.

    • AWS configuration – Check if you have set up your AWS AppSync correctly. Make sure that your resolver is properly configured and that your IAM roles and policies allow the subscription to be triggered by the mutation.

    Let me know!

    Login or Signup to reply.
  2. The filtering in "AppSync subscriptions does not support nested fields" (AWS Dev: "Enhanced subscription filtering", will be evaluated as false). It applies filtering at the root level of the response. In your case, since the filtering is applied at the CreateEventPayload level and not directly on the nested Event object, the subscription is not triggered.

    You can modify the response structure to directly return the Event object from the subscription;

    type Mutation {
      createEvent(input: CreateEventInput!): CreateEventPayload
    }
    
    type Subscription {
      onCreateEvent(filter: EventFilter): Event
        @aws_subscribe(mutations: ["createEvent"])
    }
    
    type CreateEventPayload {
      event: Event
      clientMutationId: String
    }
    
    type Event implements Node {
      description: String
      eventName: String!
      id: String!
      status: String!
    }
    
    interface Node {
      nodeId: ID!
    }
    
    input EventFilter {
      description: StringFilter
      eventName: StringFilter
      id: StringFilter
      status: StringFilter
    }
    

    FYI, code snippet was created using GitHub Copilot as I’m trying to experiment with it more so minor adjustments might need to be made. You can also use lang-typescript for syntax highlighting on StackOverflow (couldn’t find GraphQL in the Language codes currently available on Stack Exchange).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search