【问题标题】:AWS AppSync User relations for FriendsListFriendsList 的 AWS AppSync 用户关系
【发布时间】:2018-09-06 04:44:09
【问题描述】:

我在使用 AWS AppSync 和 ApolloClient 时遇到问题。 如何在名为 AppSync 的 Amazon 服务中使用用户之间的关联,即作为节点和边缘的连接。我想做的是当我关注用户时,我想通过一个请求查看所有用户的流程。 这是我想成为的要求。如何为此构建结构?

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

我创建的架构如下

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
}

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb graphql apollo aws-appsync


    【解决方案1】:

    这在今天的 AppSync 中是可能的。

    要完成此操作,您可以在架构中添加一个名为 getUser 的查询字段(在这种情况下,getUsergetFeeds 更有意义)并且它会有一个解析器,用于从数据源检索用户对象.

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

    然后,您还可以在 User.followingUser.userFeed 字段上添加解析器。 User.following 解析器将查询您的数据源并检索有人关注的用户。 User.userFeed 解析器将查询您的数据源以检索用户提要列表。

    这两个解析器(User.followingUser.userFeed)都应该在解析器的请求映射模板中使用 $context.source。此变量将包含您的 getUser 解析器的结果。请求映射模板的工作是创建您的数据源可以理解的查询。

    可能附加到User.following 的示例请求映射模板可能类似于以下内容。它将查询一个名为“Following”的表,该表的主分区键为 id(用户的 id):

    {
        "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}"
                }
            }
        }
    }
    

    您必须为 User.userFeed 解析器执行类似的操作。

    所有设置完成后,您可以运行以下查询,将会发生以下情况:

    query {
        getUser(id:"myUserId") {
        following {
          userFeed {
            id
            ImageDataUrl
            textData
            date
          }
        }
      }
    }
    
    1. getUser 解析器将首先运行。它将查询您的用户数据源并检索用户。
    2. User.following 解析器将运行。它将使用其父字段解析器 (getUser) 的结果来查询数据源以供关注。
    3. User.userFeed 解析器将运行。它将使用其父字段解析器 (getUser) 的结果来查询用户提要数据源。

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 2022-11-13
      • 2020-07-23
      • 2018-12-21
      • 2020-05-25
      • 2020-08-30
      • 2019-03-08
      • 2018-11-17
      • 2022-10-16
      相关资源
      最近更新 更多