在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):n1ru4l/graphql-live-query开源软件地址(OpenSource Url):https://github.com/n1ru4l/graphql-live-query开源编程语言(OpenSource Language):TypeScript 95.5%开源软件介绍(OpenSource Introduction):Real-Time with any schema or transport. Why Live Queries? - Read the introduction Post - Learn how Live Query Tracking works Packages in this Repository
MotivationThere is no mature live query implementation that is not tied to any specific database or SaaS product. This implementation should serve as an example for showcasing how live queries can be added to any GraphQL.js schema with (almost) any GraphQL transport. GraphQL already has a solution for real-time: Subscriptions. Those are the right tool for responding to events. E.g. triggering a sound or showing a toast message because someone poked you on Facebook. Subscriptions are also often used for updating existing query results on a client that consumes data from the same GraphQL API. Depending on the complexity of that data, cache update code for applying the subscription result can eventually become pretty bloated (!!! especially, for adding and removing list items). Often for developers it is more straight-forward to simply refetch the query once a subscription event is received instead of doing cache voodoo magic. In contrast to manual cache updates using subscriptions, live queries should feel magical and update the UI with the latest data from the server without having to write any cache update wizardry code on the client. ConceptA live query is a query operation that is annotated with a query users @live {
users(first: 10) {
id
login
}
} A live query is sent to the server (via a transport that supports delivering partial execution results) and registered. The client receives a immediate execution result and furthermore receives additional (partial) execution results once the live query operation was invalidated and therefore the client data became stale. The client can inform the server that it is no longer interested in the query (unsubscribe the live query operation). On the server we have a live query invalidation mechanism that is used for determining which queries have become stale, and thus need to be rescheduled for execution. Instead of sending the whole execution result, the server can diff the previous and the current execution result and only send a delta instruction to the client instead of the whole operation, resulting in less network overhead! In a future implementation the server might only need to re-execute partial subtrees of a query operation instead of the whole operation. How does the server know the underlying data of a query operation has changed?The reference A resource (in terms of the reference implementation) is described by a root query field schema coordinate (such as For the following type: type User {
id: ID!
name: String!
} Legitimate resource identifiers could be In case a resource has become stale it can be invalidated using the Practical example: // somewhere inside a userChangeLogin mutation resolver
user.login = "n1ru4l";
user.save();
liveQueryStore.invalidate([
// Invalidate all operations whose latest execution result contains the given user
`User:${user.id}`,
// Invalidate query operations that select the Query,user field with the id argument
`Query.user(id:"${user.id}")`,
// invalidate a list of all users (redundant with previous invalidations)
`Query.users`,
]); Those invalidation calls could be done manually in the mutation resolvers or on more global reactive level e.g. as a listener on a database write log. The possibilities are infinite. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论