FriendsList 的 AWS AppSync 用户关系
AWS AppSync User relations for FriendsList
我遇到了 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
的模式中(在这种情况下,getUser
比 getFeeds
更有意义)并且它会有一个解析器从数据源检索用户对象。
type Query {
getAllUsers(limit: Int): [User]
getUser(id:ID!): User
}
然后,您还可以在 User.following
和 User.userFeed
字段上添加解析器。 User.following
解析器将查询您的数据源并检索有人关注的用户。 User.userFeed
解析器将查询您的数据源以检索用户提要列表。
这两个解析器(User.following
和 User.userFeed
)都应在解析器的请求映射模板中使用 $context.source
。此变量将包含 getUser
解析器的结果。请求映射模板的工作是创建您的数据源理解的查询。
可能附加到 User.following
的示例请求映射模板可能类似于以下内容。它将查询名为 "Following" 的 table,它的主分区键为 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
}
}
}
}
getUser
解析器将首先 运行。它将查询您的用户数据源并检索用户。
User.following
解析器将 运行。它将使用其父字段解析器 (getUser
) 的结果来查询以下数据源。
User.userFeed
解析器将 运行。它将使用其父字段解析器 (getUser
) 的结果来查询用户提要数据源。
我遇到了 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
的模式中(在这种情况下,getUser
比 getFeeds
更有意义)并且它会有一个解析器从数据源检索用户对象。
type Query {
getAllUsers(limit: Int): [User]
getUser(id:ID!): User
}
然后,您还可以在 User.following
和 User.userFeed
字段上添加解析器。 User.following
解析器将查询您的数据源并检索有人关注的用户。 User.userFeed
解析器将查询您的数据源以检索用户提要列表。
这两个解析器(User.following
和 User.userFeed
)都应在解析器的请求映射模板中使用 $context.source
。此变量将包含 getUser
解析器的结果。请求映射模板的工作是创建您的数据源理解的查询。
可能附加到 User.following
的示例请求映射模板可能类似于以下内容。它将查询名为 "Following" 的 table,它的主分区键为 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
}
}
}
}
getUser
解析器将首先 运行。它将查询您的用户数据源并检索用户。User.following
解析器将 运行。它将使用其父字段解析器 (getUser
) 的结果来查询以下数据源。User.userFeed
解析器将 运行。它将使用其父字段解析器 (getUser
) 的结果来查询用户提要数据源。