This is the simplest strategy for Data Synchronization that is also quite straightforward to set up.
Start 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 models
@datasync annotation to your model(s) in your GraphQL SDL found in the
@datasync annotation ensures consistency of your data and gives you delta queries.
@datasyncannotation is a shorthand for two annotations
@delta. Refer to the last section for details.
@datasync transforms your model by adding two fields:
@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.
syncquery also accepts a filter argument for filtering
@datasync annotation also adds a
Delta type and a
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.
DeltaList is a container for
Delta type, which also returns a
lastSync timestamp, that can be used in a subsequent
Soft Deletesstrategy can only get you the latest version of changed documents ignoring any in-between states that may have transpired between
Modify the template to support Data Synchronization
src/index.ts file of the template, use
DataSyncPlugin and compliant data sources in
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 be get info about this deletion.
Issuing Delta/Sync queries from client
As an example consider the usecase when your application has stayed offline for a while. You can then use the
syncX query to get only the changed documents rather than having to refetch 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
false otherwise. The response also contains a
lastSync timestamp which can be used in the next
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 has a
@datasync annotation, Graphback will construct a
CommentDelta type as follows:
Notice that there are is no relationship field in the
@datasync annotation provided by
@graphback/datasync is a shorthand for two separate annotations:
@versioned. Vanilla Graphback supports
@versioned out of the box, see this page.
@delta annotation is only provided by the
@graphback/datasync package. However, we do not support using this annotation on it's own without
@datasync in any the above examples with
@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