amazon-web-services - FriendsList 的 AWS AppSync 用户关系

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

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

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

我创建的schema如下

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
}

最佳答案

这在今天的 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) 的结果来查询用户提要数据源。

关于amazon-web-services - FriendsList 的 AWS AppSync 用户关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518557/

相关文章:

amazon-web-services - Amazon ECS 对 docker 中的 --shm-size 的支持

indexing - DynamoDB 中没有分区键的查询

c# - SQL查询: Display only latest Id from each set

node.js - 如何为 socket io 和 graphql 建立连接?

express - graphql中如何获取最后3个元素

amazon-web-services - 监控AWS EC2实例端口

c# - 亚马逊 S3 C# SDK "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."错误

amazon-web-services - aws cloudformation 包 cli 如何获取上次运行的 MD5 校验和?

python - 如何将 AWS 的 Dynamo Db 与 Django 结合使用?

GraphQL 异步查询结果