我了解如何通过附加链接到AppSync
的解析器(如described here)来通过ElasticSesarch
执行地理空间查询,以查找距gps坐标距离范围内的事件。
但是,如果我希望我的客户也订阅在此距离范围内创建的新事件怎么办?
我知道我可以将解析器附加到订阅类型,但是当我只想通过检查gps坐标之间的距离来过滤订阅时,它似乎迫使您提供数据源。
答案 0 :(得分:0)
这是一个很好的问题,我认为有几种方法可以解决此问题。这里的难点在于,您将找到一种方法来问“在此位置的事件中哪些订阅感兴趣”这一问题。这是一条可能的前进之路。
以下内容假设这些架构部分:
// Whatever custom object has a location
type Post {
id: ID!
title: String
location: Location
}
input PublishPostInput {
id: ID!
title: String
location: Location
subscriptionID: ID
}
type PublishPostOutput {
id: ID!
title: String
location: Location
subscriptionID: ID
}
type Location {
lat: Float,
lon: Float
}
input LocationInput {
lat: Float,
lon: Float
}
# A custom type to hold custom tracked subscription information
# for location discover
type OpenSubscription {
subscriptionID: ID!
location: Location
timestamp: String!
}
type OpenSubscriptionConnection {
items: [OpenSubscription]
nextToken: String
}
type Query {
# Query elasticsearch index for relevant subscriptions
openSubscriptionsNear(location: LocationInput, distance: String): OpenSubscriptionConnection
}
type Mutation {
# This mutation uses a local resolver (e.g. a resolver with a None data source) and simply returns the input as is.
publishPostToSubscription(input: PublishPostInput): PublishPostOutput
}
type Subscription {
# Anytime someone passes an object with the same subscriptionID to the "publishPostToSubscription" mutation field, get updated.
listenToSubscription(subscriptionID: ID!): PublishPostOutput
@aws_subscribe(mutations:["publishPostToSubscription"])
}
假设您使用DynamoDB作为主要的事实来源,请设置一个DynamoDB流,该流将调用“ PublishIfInRange” lambda函数。该“ PublishIfInRange”功能看起来像这样
// event - { location: { lat, lon }, id, title, ... }
function lambdaHandler(event) {
const relevantSubscriptions = await callGraphql(`
query GetSubscriptions($location: LocationInput) {
openSubscriptionsNear(location:$location, distance: "10 miles") {
subscriptionID
}
}
`, { variables: { location: event.location }})
for (const subscription of relevantSubscriptions) {
callGraphql(`
mutation PublishToSubscription($subID: ID!, $obj: PublishPostInput) {
publishPostToSubscription(input: $obj) {
id
title
location { lat lon }
subscriptionID
}
}
`, { variables: { input: { ...subscription, ...event }}})
}
}
您将需要维护一个按位置索引的订阅注册表。一种实现方法是让您的客户端应用程序调用一个变异,该变异创建具有位置和subscriptionID的预订对象(例如,mutation { makeSubscription(loc: $loc) { ... } }
假设您正在使用$ util.autoId()在解析器中生成subscriptionID)。拥有subscriptionID后,您可以通过graphql进行订阅调用,并将subscriptionID作为参数传递(例如subscription { listenToSubscription(subscriptionID: "my-id") { id title location { lat lon } } }
)。当您进行上述订阅时,AppSync会创建一个主题并授权当前用户订阅该主题。该主题对于被调用的订阅字段以及传递给订阅字段的参数集是唯一的。换句话说,主题仅接收对象
现在,无论何时创建对象,记录都会通过DynamoDB流进入lambda函数。 lambda函数查询elasticsearch该对象附近的所有打开的订阅,然后将记录发布到每个打开的订阅。
我认为,这应该可以让您走得很远,但是如果您在狭窄的区域中拥有数百万的用户,则可能会遇到扩展问题。希望这会有所帮助