You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

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 @connection directive tells AppSync how to link related data. For User.following, we use a custom index name (FollowerToFollowing) that we'll set up in DynamoDB.
  • We simplified the following field to return a direct list of User objects (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:

  1. User Table

    • Partition Key: id (String)
    • Store basic user data (id, username, etc.)
  2. 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.
  3. 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.following connection by letting us fetch all users a given user follows.

Step 3: Configure AppSync Resolvers

Link each schema field to a DynamoDB resolver to handle data fetching:

  1. Query.getFeeds

    • Data Source: User Table
    • Resolver Logic: Fetch the user with the provided id using a DynamoDB GetItem operation.
  2. User.following

    • Data Source: Follow Table
    • Resolver Logic: Use the FollowerToFollowing GSI to fetch all Follow items where followerId equals the current user's id. Then batch-fetch the corresponding User objects for each followingId.
  3. User.userFeed

    • Data Source: Feed Table
    • Resolver Logic: Use the Feed table's userId GSI to fetch all feed items where userId equals the current user's id.
  4. Mutations

    • followUser: Insert a new record into the Follow table with followerId, followingId, and createdAt.
    • 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:

  1. Fetch your user record using myUserId
  2. Pull all users you follow via the following connection
  3. 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.getFeeds that restricts access to when the id matches 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-appsync package to integrate Apollo with AppSync seamlessly.

内容的提问来源于stack exchange,提问作者Yılmaz Gürsoy

火山引擎 最新活动