1

Hi i am new in aws AppSync and GraphQl. I have a problem with subscription. I want to get notifiend in real time when new post is posted

Here is my graphql schema ``

type Mutation {
    addPost(
        id: ID!,
        author: String!,
        title: String,
        contennt: String,
        url: String
    ): Post!
    updatePost(
        id: ID!,
        author: String!,
        title: String,
        content: String,
        ups: Int!
    ): Post!
    deletePost(id: ID): Post!
}

type Post {
    id: ID!
    author: String!
    title: String!
    url: String
    content: String
    ups: Int!
}

type Query {
    allPost: [Post]
    getPost(id: ID!): Post
}

type Subscription {
    newPost: Post
        @aws_subscribe(mutations: ["addPost"])
}

schema {
    query: Query
    mutation: Mutation
    subscription: Subscription
}
``

Here is my Query for subs:

subscription NewPostSub{
  newPost{
    __typename
    title
    content
    author
    url
  }
}

Im getting error that provided key element doesnt match a schema, in dynamodb table pk is id and i generated mapping template. Thank you

Kesa101
  • 31
  • 1
  • 4
  • I will need more information to help. Can you please provide your request & response mapping templates for the **Mutation.addPost** mutation. If you have a resolver on the **Subscription.newPost** field also please give those as well. The subscription query should not care about your DynamoDB schema so I suspect this error is actually coming from the mutation. Let me know if you have more information. – mparis Sep 04 '18 at 05:32
  • Hi here is req for addPost mutation: { "version" : "2017-02-28", "operation" : "PutItem", "key" : { $util.dynamodb.toDynamoDBJson($ctx.args.id) "id": $util.dynamodb.toDynamoDBJson($util.autoId()), }, "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args) } and here is response : $util.toJson($ctx.result) Here is for subscription: { "version": "2017-02-28", "operation": "GetItem", "key": { "id": $util.dynamodb.toDynamoDBJson($ctx.args.id), } } and here is response: $util.toJson($ctx.result) Thanks @mparis – Kesa101 Sep 04 '18 at 07:36

1 Answers1

2

Thanks for the info.

First, you do not need a resolver on your subscription field at all. The only reason you need to add a resolver to a subscription field is to do authorization checks based on the calling identity at connect time. You do not need to add a resolver to make subscriptions work. To fix it, remove the resolver off the subscription (Subscription.newPost) field.

To solve your use case and make it so you only subscribe to posts with a certain id, change your newPost subscription field to newPost(id: ID!). When you make the subscription query, provide an id and that subscription will only get pushed posts returned by the subscribed to mutation with that id. This is a feature baked into AppSync and the argument equality check happens automatically when you add the arguments to the subscription field.

Secondly as you pasted it in the comment, you currently have this request mapping template for Mutation.addPost:

{ 
  "version" : "2017-02-28", 
  "operation" : "PutItem", 
  "key" : { 
    $util.dynamodb.toDynamoDBJson($ctx.args.id) 
    "id": $util.dynamodb.toDynamoDBJson($util.autoId()), 
  }, 
  "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args) 
} 

There is a typo and it should be this:

{ 
  "version" : "2017-02-28", 
  "operation" : "PutItem", 
  "key" : { 
    "id": $util.dynamodb.toDynamoDBJson($util.autoId()), 
  }, 
  "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args) 
} 
mparis
  • 3,623
  • 1
  • 17
  • 16