I want to know if having a circular reference in AWS Appsync possible or not? I have searched a lot but couldn't find it. Something like this:
type Post {
title: String!
content: String!
user: User!
}
type Query {
allPosts: [Post!]
singlePost(id: String!): Post!
}
type User {
name: String!
posts: [Post!]
}
Edit (Tried lambda with the logic described here https://youtu.be/bgq7FRSPDpI?list=PL55RiY5tL51rG1x02Yyj93iypUuHYXcB_&t=526 )
Here's the lambda resolver for allPosts (handler function will be called):
import * as sdk from "aws-sdk";
declare var process: {
env: {
TABLE_NAME: string;
};
};
interface Event {
info: {
fieldName: string;
parentTypeName: string;
variables: Record<string, any>;
};
}
const client = new sdk.DynamoDB.DocumentClient();
const getUser = (user_id: string): Record<string, any> | null => {
return client
.query({
TableName: process.env.TABLE_NAME,
KeyConditionExpression: "PK = :pk AND SK = :sk",
ExpressionAttributeValues: {
":pk": user_id,
":sk": "profile",
},
})
.promise()
.then(
(data) =>
data.Items?.map((item) => ({
...item.data,
posts: getPost.bind(null, item.PK),
}))[0]
)
.catch((err) => {
console.log(err);
return null;
});
};
const getPost = (user_id: string): Record<string, any> | null => {
return client
.query({
TableName: process.env.TABLE_NAME,
KeyConditionExpression: "SK = :sk AND pk = :pk",
ExpressionAttributeValues: {
":pk": user_id,
":sk": "profile",
},
})
.promise()
.then((data) =>
data.Items?.map((item) => ({
...item.data,
user: getUser.bind(null, item.PK),
}))
)
.catch((err) => {
console.log(err);
return null;
});
};
export const handler = async (event: Event) => {
if (event.info.fieldName === "allPosts") {
const data = await client
.query({
TableName: process.env.TABLE_NAME,
KeyConditionExpression: "#t = :sk",
IndexName: "GSI",
ProjectionExpression: "#d, PK",
ExpressionAttributeNames: {
"#t": "type",
"#d": "data",
},
ExpressionAttributeValues: {
":sk": "post",
},
})
.promise();
const result = data.Items?.map((item) => ({
...item.data,
user: getUser.bind(null, item.PK),
}));
console.log(data, result);
return result;
}
return;
// else if (event.fieldName === "singlePost") {
// }
};
The user field has a function bounded as in this video: https://youtu.be/bgq7FRSPDpI?list=PL55RiY5tL51rG1x02Yyj93iypUuHYXcB_&t=526
But lambda response is not returning the bounded function.
[
{
"title": "post by user_123",
"content": "\n\nNew to this community. I need some help in designing the Amazon Dynamo DB table for my personal projects.\n\nOverview, this is a simple photo gallery application with following attributes.\n\nUserID\nPostID\nList item\nS3URL\nCaption\nLikes\nReports\nUploadTime\nI wish to perform the following queries:\n\nFor a given user, fetch 'N' most recent posts\nFor a given user, fetch 'N' most liked posts\nGive 'N' most recent posts (Newsfeed)\nGive 'N' most liked posts (Newsfeed)\nMy solution:"
},
{
"title": "another post by user_123",
"content": "\n\nNew to this community. I need some help in designing the Amazon Dynamo DB table for my personal projects.\n\nOverview, this is a simple photo gallery application with following attributes.\n\nUserID\nPostID\nList item\nS3URL\nCaption\nLikes\nReports\nUploadTime\nI wish to perform the following queries:\n\nFor a given user, fetch 'N' most recent posts\nFor a given user, fetch 'N' most liked posts\nGive 'N' most recent posts (Newsfeed)\nGive 'N' most liked posts (Newsfeed)\nMy solution:"
}
]
But I can see the bounded function in the logs:
[
{
title: 'post by user_123',
content: '\n' +
'\n' +
'New to this community. I need some help in designing the Amazon Dynamo DB table for my personal projects.\n' +
'\n' +
'Overview, this is a simple photo gallery application with following attributes.\n' +
'\n' +
'UserID\n' +
'PostID\n' +
'List item\n' +
'S3URL\n' +
'Caption\n' +
'Likes\n' +
'Reports\n' +
'UploadTime\n' +
'I wish to perform the following queries:\n' +
'\n' +
"For a given user, fetch 'N' most recent posts\n" +
"For a given user, fetch 'N' most liked posts\n" +
"Give 'N' most recent posts (Newsfeed)\n" +
"Give 'N' most liked posts (Newsfeed)\n" +
'My solution:',
user: [Function: bound getUser]
},
{
title: 'another post by user_123',
content: '\n' +
'\n' +
'New to this community. I need some help in designing the Amazon Dynamo DB table for my personal projects.\n' +
'\n' +
'Overview, this is a simple photo gallery application with following attributes.\n' +
'\n' +
'UserID\n' +
'PostID\n' +
'List item\n' +
'S3URL\n' +
'Caption\n' +
'Likes\n' +
'Reports\n' +
'UploadTime\n' +
'I wish to perform the following queries:\n' +
'\n' +
"For a given user, fetch 'N' most recent posts\n" +
"For a given user, fetch 'N' most liked posts\n" +
"Give 'N' most recent posts (Newsfeed)\n" +
"Give 'N' most liked posts (Newsfeed)\n" +
'My solution:',
user: [Function: bound getUser]
}
]