AWS AppSync与ApolloClient用户关联及Feed查询结构构建咨询
Hey there! Let's break down how to build this follow-based feed system with AWS AppSync and get your query working exactly as you want it. I'll walk you through schema design, data modeling, resolver setup, and tweaks to match your desired query structure.
Step 1: Refine Your GraphQL Schema
First, we need to model the core entities: User, Feed (user posts), and Follow (the connection between followers and followed users). We'll use AppSync's @connection directive to handle relational data, and we'll simplify the structure to align with your desired query format.
Here's a tailored schema:
# Core feed type for user posts type Feed { id: ID! userId: ID! ImageDataUrl: String textData: String! date: AWSDateTime! # Link back to the user who created this feed item author: User! @connection(fields: ["userId"]) } # User type with follow relationships and their own feed type User { id: ID! username: String! # Add other user fields like email, avatarUrl as needed # List of users this user follows (simplified for your query) following: [User]! @connection(keyName: "FollowerToFollowing", fields: ["id"]) # List of feed items created by this user userFeed: [Feed]! @connection(fields: ["id"]) } # Follow entity to track follow relationships (stored in DynamoDB) type Follow { id: ID! followerId: ID! # User who initiated the follow followingId: ID! # User being followed createdAt: AWSDateTime! # Connections back to both users follower: User! @connection(fields: ["followerId"]) followedUser: User! @connection(fields: ["followingId"]) } # Query type matching your desired getFeeds operation type Query { getFeeds(id: ID!): User! # Add other queries like getUser, getFeed if needed } # Mutations to handle follows and feed creation type Mutation { followUser(followerId: ID!, followingId: ID!): Follow! unfollowUser(followId: ID!): Follow! createFeed(userId: ID!, ImageDataUrl: String, textData: String!, date: AWSDateTime!): Feed! }
Key Schema Notes:
- The
@connectiondirective tells AppSync how to link related data. ForUser.following, we use a custom index name (FollowerToFollowing) that we'll set up in DynamoDB. - We simplified the
followingfield to return a direct list ofUserobjects (instead of strict Relay edges) to match your exact query structure. You can add full edge-node pagination later if needed.
Step 2: DynamoDB Table Design
AppSync integrates seamlessly with DynamoDB, so let's set up tables and indexes to support these relationships:
User Table
- Partition Key:
id(String) - Store basic user data (id, username, etc.)
- Partition Key:
Feed Table
- Partition Key:
id(String) - Global Secondary Index (GSI):
- Partition Key:
userId(String) - This lets us quickly fetch all feed items for a specific user.
- Partition Key:
- Partition Key:
Follow Table
- Partition Key:
followerId(String) - Sort Key:
followingId(String) - Global Secondary Index (GSI) named
FollowerToFollowing:- Partition Key:
followerId(String) - Sort Key:
followingId(String) - This index powers the
User.followingconnection by letting us fetch all users a given user follows.
- Partition Key:
- Partition Key:
Step 3: Configure AppSync Resolvers
Link each schema field to a DynamoDB resolver to handle data fetching:
Query.getFeeds
- Data Source: User Table
- Resolver Logic: Fetch the user with the provided
idusing a DynamoDBGetItemoperation.
User.following
- Data Source: Follow Table
- Resolver Logic: Use the
FollowerToFollowingGSI to fetch allFollowitems wherefollowerIdequals the current user'sid. Then batch-fetch the correspondingUserobjects for eachfollowingId.
User.userFeed
- Data Source: Feed Table
- Resolver Logic: Use the Feed table's
userIdGSI to fetch all feed items whereuserIdequals the current user'sid.
Mutations
followUser: Insert a new record into the Follow table withfollowerId,followingId, andcreatedAt.createFeed: Insert a new record into the Feed table with the provided fields.
Step 4: Run Your Desired Query
With the schema and resolvers set up, your exact query will now work perfectly:
query { getFeeds(id:"myUserId") { following { userFeed { id ImageDataUrl textData date } } } }
This query will:
- Fetch your user record using
myUserId - Pull all users you follow via the
followingconnection - For each followed user, fetch their entire feed of posts
Bonus: Permissions & Apollo Client Setup
- Permissions: Use AppSync's authorization rules (like Cognito User Pools) to ensure users can only query their own feed. Add a rule to
Query.getFeedsthat restricts access to when theidmatches the authenticated user's ID. - Apollo Client: Configure your client with your AppSync endpoint and auth credentials (API key, Cognito token, etc.). Use the
aws-appsyncpackage to integrate Apollo with AppSync seamlessly.
内容的提问来源于stack exchange,提问作者Yılmaz Gürsoy




