Something I have been trying to figure out:
Is it possible to do multiple dynamodb or ES operations in the same request template?
For example:
Use Case 1: I want to fetch the key from dynamodb table A and then get value on this key from dynamodb table B.
Use Case 2: I want to first GET data from ES, parse the output and use it to PUT item.
Not sure if appsync supports such use cases
HI,
What does your schema look like?
Its a sample twitter schema:
type Mutation {
# Create a tweet for a user
createTweet(
handle: String!,
tweet: String!,
name: String!,
location: String!,
description: String!,
followers: [String!]!,
followers_count: Int!,
friends_count: Int!,
favourites_count: Int!
): Tweet!
# Delete User Tweet
deleteTweet(tweet_id: String!, handle: String!): Tweet!
# Retweet existing Tweet
reTweet(tweet_id: String!, handle: String!): Tweet!
# Update existing Tweet
updateTweet(tweet_id: String!, handle: String!, tweet: String!): Tweet!
}
type Query {
getUserTwitterFeed(handle: String!): User!
}
type Subscription {
subscribeToTweeterUser(handle: String!): Tweet
@aws_subscribe(mutations: ["createTweet","deleteTweet", "reTweet", "updateTweet"])
}
type Tweet {
tweet_id: String!
tweet: String!
retweeted: Boolean
retweet_count: Int
favorited: Boolean
}
type TweetConnection {
items: [Tweet!]!
nextToken: String
}
type User {
name: String!
handle: String!
location: String!
description: String!
followers_count: Int!
friends_count: Int!
favourites_count: Int!
followers: [String!]!
topTweet: Tweet
tweets(limit: Int, nextToken: String, keyword: String): TweetConnection
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
Also you can refer to the schema here https://github.com/serverless/serverless-graphql/tree/master/app-backend/appsync in the dynamodb, ES and lambda integrations
In the above schema you can have a resolver on query getUserTwitterFeed that gets a data from table A, as it returns a user which in turn returns a list of tweets and say list of tweets is in another table you can set a resolver on Type user and field tweets which can do a scan on table B.
Does that cover your case?
@karthiksaligrama thats interesting! how would the mapping table look like for Type user and field tweets which can do a scan on table B.. just trying to understand :) thanks
Using a partial of your schema
type Tweet {
tweet_id: String!
tweet: String!
retweeted: Boolean
retweet_count: Int
favorited: Boolean
handle: String!
}
type User {
name: String!
handle: String!
location: String!
description: String!
followers_count: Int!
friends_count: Int!
favourites_count: Int!
followers: [String!]!
topTweet: Tweet
tweets: Tweet
}
First, I added "handle" to Tweet so that there is a reference between user and tweet.
In your "Data Types" list on the right side of your schema, scroll to your User data type. Click on "Attach" next to the field "tweets" to attach a new resolver.
The example I have uses a DynamoDB resolver, but it would work for any resolver.
Select your data source and add the request/response:
Request:
{
"version" : "2017-02-28",
"operation" : "Scan",
"filter" : {
"expression" : "handle= :handle",
"expressionValues" : {
":handle" : { "S" : "${ctx.source.handle}" }
},
}
}
The secret here is ctx.source.handle where in a regular resolver you might use ctx.arguments.handle for your expression
Alternatively, you could use a Lambda function to run everything at one time.
In the User type, the tweets field would then resolve to a list, correct?
tweets: [Tweet]
@teddarling Thanks for the awesome example. Do you have any suggestions on how to handle mapping the User type for an "update" operation? e.g. if we wanted to update the location of an existing user who has tweets? For adding a new user it's easy enough because I can just return an empty array in the response mapping template, but for updating an existing user I'm not sure how I could link the existing tweets with the response from my PutItem/UpdateItem request.
@teddarling I have a similar scenario. If I map it to the @sid88in 's example, how can delete all the tweets of a user from tweets table when a user is deleted. How would I write a resolver for dynamoDB and attach it to deleteUser mutation?
@skhizar13 The way we are handling situations like yours is by leveraging dynamodb's stream and attaching a lambda to it. The lambda can have custom logic that checks if each change is a deletion of a user, and if it is, can then query dynamodb for all tweets from that user and finally issue a batch delete (or more, depending on how many tweets) for each of those tweets.
This pattern could be extended as the application grows. E.g. your lambda attached to the stream can act as a sort of dispatcher for different change events that you want to react to (the deleted user being just one of them). You could also implement functionality that keeps track of deleted users but only deletes the tweets at certain times of day, etc.
Most helpful comment
Using a partial of your schema
First, I added "handle" to Tweet so that there is a reference between user and tweet.
In your "Data Types" list on the right side of your schema, scroll to your User data type. Click on "Attach" next to the field "tweets" to attach a new resolver.
The example I have uses a DynamoDB resolver, but it would work for any resolver.
Select your data source and add the request/response:
Request:
The secret here is
ctx.source.handlewhere in a regular resolver you might usectx.arguments.handlefor your expressionAlternatively, you could use a Lambda function to run everything at one time.