Soft Deletes with delta queries
This is the simplest strategy for Data Synchronization that is also quite straightforward to set up.
#
SetupStart off with the official Graphback template for MongoDB here to follow along. Adding this strategy is relatively simple, consisting of the following two steps:
#
Annotate the required modelsAdd the @datasync
annotation to your model(s) in your GraphQL SDL found in the model
folder:
The @datasync
annotation ensures consistency of your data and gives you delta queries.
note
The @datasync
annotation is a shorthand for two annotations @versioned
and @delta
. Refer to the last section for details.
@datasync
transforms your model by adding two fields: updatedAt
and createdAt
The @datasync
annotation adds a sync
query or a delta query:
This allows you to get all the changed documents (updated and deleted) in a collection since the lastSync
timestamp. Internally this uses the updatedAt
database field to check if any documents in the database have been modified, by comparing client provided lastSync
timestamp value.
note
The sync
query also accepts a filter argument for filtering
The @datasync
annotation also adds a Delta
type and a DeltaList
type:
The Delta
type for a model consists of all of the model's transformed properties, as well as a _deleted
field which is used internally to mark documents as deleted in the database. Thus delete
mutations only mark documents with _deleted: true
instead of actually removing them.
The DeltaList
is a container for Delta
type, which also returns a lastSync
timestamp, that can be used in a subsequent sync
query.
note
Soft Deletes
strategy can only get you the latest version of changed documents ignoring any in-between states that may have transpired between lastSync
and now.
#
Modify the template to support Data SynchronizationIn the src/index.ts
file of the template, use DataSyncPlugin
and compliant data sources in buildGraphbackAPI
:
The data sources provided by @graphback/datasync
ensure that the documents are only marked with _deleted: true
instead of being removed from the database, meaning that a compatible client can issue a sync
query to obtain information about this deletion.
#
Issuing Delta/Sync queries from clientAs an example consider the usecase when your application has stayed offline for a while. You can then use the sync<Type>s
query to retrieve only the changed documents rather than having to re-fetch all of the documents.
As an example response you might get:
The response is a list of the latest versions of the changed documents along with a _deleted
flag that is set to true
if the document has been deleted since lastSync
and false
otherwise. The response also contains a lastSync
timestamp which can be used in the next syncX
query.
In the example response, we get that the "First!" comment has been deleted, while a new comment "Second!" has been created.
note
There is no support for querying relationships through a delta query, all relationship fields are removed when constructing a delta Type.
Taking an example model definition with a oneToMany
relationship from the Note
type to the Comment
type:
Since the Comment
type has a @datasync
annotation, Graphback will construct a CommentDelta
type as follows:
Notice that there are is no relationship field in the CommentDelta
type.
#
Advanced topics@delta
annotation#
The @datasync
annotation provided by @graphback/datasync
is a shorthand for two separate annotations: @delta
and @versioned
. Vanilla Graphback supports @versioned
out of the box, see this page.
The @delta
annotation is only provided by the @graphback/datasync
package. However, we do not support using this annotation on it's own without @versioned
.
#
ExampleReplacing @datasync
in any the above examples with @versioned
and @delta
yields the exact same results, for example:
This model exactly yields all of the type-definitions outlined above:
Similarly, this will also yield a Delta
type, a DeltaList
type and a sync
query.