FriendsList的AWS AppSync用户关系

Yıl*_*soy 1 amazon-web-services amazon-dynamodb apollo graphql aws-appsync

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

query {
    getFeeds(id:"myUserId") {
    following {
      userFeed {
        id
        ImageDataUrl
        textData
        date
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我创建的架构如下

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
}
Run Code Online (Sandbox Code Playgroud)

小智 5

今天,在AppSync中可以做到这一点。

为此,您可以在架构中添加一个名为getUsergetUsergetFeeds在这种情况下更有意义)的查询字段,并且它将具有一个解析器,该解析器从数据源中检索User对象。

type Query {
    getAllUsers(limit: Int): [User]
    getUser(id:ID!): User
}
Run Code Online (Sandbox Code Playgroud)

然后,您也可以在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}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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