AWS AppSync User relations for FriendsList

0 votes

I have a problem with AWS AppSync and ApolloClient. How can I use an association between users in the Amazon Service named AppSync, that is, a connection as node and edge. What I want to do is when I follow the users, I would like to see the flow of all users with a single request. It is the request that I want to be. How do I build a structure for this?

query {
    getFeeds(id:"myUserId") {
    following {
      userFeed {
        id
        ImageDataUrl
        textData
        date
      }
    }
  }
}

The schema I created is as follows

type Comments {
    id: ID!
    date: Int!
    message: String!
    user: User
}
type Feed {
    id: ID!
    user: User!
    date: Int!
    textData: String
    ImageDataUrl: String
    VideoDataUrl: String
    likes: Like
    comments: [Comments]
}

#Objects
type Like {
    id: ID!
    number: Int!
    likers: [User]
}
}
type Query {
    getAllUsers(limit: Int): [User]
}

type User {
    id: ID!
    name: String!
    email: String!
    imageUrl: String!
    imageThumbUrl: String!
    followers: [User]
    following: [User]
    userFeed: [Feed]
}

schema {
    query: Query
}

Sep 11, 2018 in AWS by bug_seeker
• 15,520 points
1,102 views

1 answer to this question.

0 votes

This is possible in AppSync today.

To accomplish this, you could add a query field to your schema called getUser (getUser makes more sense than getFeeds in this case) and it would have a resolver which retrieves a User object from a data source.

type Query {
    getAllUsers(limit: Int): [User]
    getUser(id:ID!): User
}

Then, you can also add resolvers on the User.following and User.userFeed fields. The User.following resolver would query your data source and retrieve users whom somebody is following. The User.userFeed resolver would query your data source to retrieve a list of user feeds.

Both of these resolvers (User.following and User.userFeed) should utilize $context.source in the resolver's request mapping template. This variable will contain the result of your getUserresolver. The request mapping template's job is to create a query which your data source understands.

An example request mapping template which might be attached to User.following could be similar to the following. It would query a table named "Following", which has a primary partition key of id (the id of the user):

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        ## Provide a query expression. **
        "expression": "id = :id",
        "expressionValues" : {
            ":id" : {
                ## Use the result of getUser to populate the query parameter **
                "S" : "${ctx.source.id}"
            }
        }
    }
}

You would have to do something similar for User.userFeed resolver.

After you're all setup, you can run the below query, and the following will happen:

query {
    getUser(id:"myUserId") {
    following {
      userFeed {
        id
        ImageDataUrl
        textData
        date
      }
    }
  }
}
  1. getUser resolver will run first. It will query your User data source and retrieve the user.
  2. User.following resolver will run. It will use the result of it's parent field resolver (getUser) to query the data source for following.
  3. User.userFeed resolver will run. It will use the result of it's parent field resolver (getUser) to query the user feed data source.
answered Sep 11, 2018 by Priyaj
• 58,090 points

Related Questions In AWS

0 votes
1 answer

Want to use an AWS Cognito User Pool without putting a password(for an easier approach)

Currently, AWS Cognito is not supporting passwordless ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,670 points
5,202 views
0 votes
1 answer

ERROR 1045 (28000): Access denied for user in AWS.

Hi@akhtar, You need to reset the password for this. ...READ MORE

answered Apr 2, 2020 in AWS by MD
• 95,440 points
7,196 views
0 votes
1 answer

How to create an access key for an IAM user in AWS?

Hi@akhtar, You can create a new AWS secret ...READ MORE

answered Nov 3, 2020 in AWS by MD
• 95,440 points
417 views
+3 votes
1 answer

aws cloudformation for atomic stack-update

When you perform a stack update, only ...READ MORE

answered Mar 27, 2018 in AWS by DareDev
• 6,890 points
795 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
+1 vote
3 answers

How to get ARN for s3 Bucket using aws cli .

An ARN is a non-opaque, constructible identifier, ...READ MORE

answered Aug 16, 2018 in AWS by Priyaj
• 58,090 points
5,067 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP