r s
r s

Reputation: 79

AWS AppSync GraphQL subscriptions returning null values on event

New to GraphQL, Amplify, AppSync, etc, and running into an issue when attempting to subscribe to an onUpdate event.

I added the 'API' library to my Amplify project with authentication through an API key. I can successfully send a mutation (updatePurchaseOrder) request through Postman, and the subscription listener registers an update, but the data returned is null on everything besides the id of the updated record.

screenshot in the AppSync console

The status field is null here, I would expect to see the new updated value. Is that the expected behavior?

The defined type on this:

type PurchaseOrder @model @auth(rules: [ { allow: public } ] ){
  id: ID!
  name: String!
  description: String!
  user: String
  time: String
  file: String!
  status: String
}

Schema mutations and subscriptions created from the initial type definition haven't been changed:

type PurchaseOrder {
    id: ID!
    name: String!
    description: String!
    user: String
    time: String
    file: String!
    status: String
    createdAt: AWSDateTime!
    updatedAt: AWSDateTime!
}


type Mutation {
    updatePurchaseOrder(input: UpdatePurchaseOrderInput!, condition: ModelPurchaseOrderConditionInput): PurchaseOrder
}



input UpdatePurchaseOrderInput {
    id: ID!
    name: String
    description: String
    user: String
    time: String
    file: String
    status: String
}


type Subscription {
    onUpdatePurchaseOrder: PurchaseOrder
        @aws_subscribe(mutations: ["updatePurchaseOrder"])
}

I tried logging to console in browser and see the same empty object returned. Figured listening through the AppSyncConsole would be less error prone, but I'm still seeing the same result.

Upvotes: 2

Views: 1015

Answers (2)

jbnunn
jbnunn

Reputation: 6355

In a messaging app, I encountered a similar issue where I was using DynamoDB as the datastore, and was getting the same error while trying to retrieve some of the fields after a mutation.

I needed the partition key (group_id) and sort key (message_id), but they weren't being returned. This led to the errors

"Cannot return null for non-nullable type: 'String' within parent 'Message' (/getMessages[0]/group_id)"

and

"Cannot return null for non-nullable type: 'String' within parent 'Message' (/getMessages[0]/message_id)"

In my add_message request mapping template, I had to add those fields as attribute values in the DDB record.

Previously, my mapping template only had the following attribute values:

"attributeValues": {
        "message": $util.dynamodb.toDynamoDBJson($ctx.args.message),   
        "from_user_id": $util.dynamodb.toDynamoDBJson($ctx.args.from_user_id),
        "created_at": $util.dynamodb.toDynamoDBJson($util.time.nowISO8601())
}

To fix, I had to add group_id and message_id:

"attributeValues": {
        "message": $util.dynamodb.toDynamoDBJson($ctx.args.message),   
        "from_user_id": $util.dynamodb.toDynamoDBJson($ctx.args.from_user_id),
        "created_at": $util.dynamodb.toDynamoDBJson($util.time.nowISO8601()),
        "status": $util.dynamodb.toDynamoDBJson("UNREAD"),
        "group_id": $util.dynamodb.toDynamoDBJson($ctx.args.group_id),
        "message_id": $util.dynamodb.toDynamoDBJson($ctx.args.message_id)
}

After this change, I was able to get the values I needed.

Upvotes: 0

r s
r s

Reputation: 79

Figured out where I went wrong... I needed to specify the response fields I wanted in the POST query (eg.:

mutation updatePurchaseOrder {
  updatePurchaseOrder(input: {status: "Pending", id: "53a98236-7b64-428c-93ea-2fae5228c0ef"}) {
    id
    **status**
  }

)

'status' was not in the query response parameters originally. The subscription response will only include fields that are part of the mutation response in the query itself.

Upvotes: 3

Related Questions