Sync
As mentioned EYWA supports GraphQL mutations through few generic mutations for each entity modeled and deployed in
DATASETS tab. Most basic mutation is _Sync_
. Sync will synchronize entity data in mutation with backend storage
service to exact match.
All nested/related data will also be saved and connected in exact way it was attached. To show how sync works we'll be using Authentication, Authorization & Access module dataset to input users, groups and roles.
#
Authentication, Authorization & Access module
#
Mutationmutation ($user: UserInput) { syncUser(user: $user) { name roles { name } groups { name } }}
#
Variables{ "user": { "name": "john_wayne", "active": true }}
#
Response{ "data": { "syncUser": { "name": "john_wayne", "roles": null, "groups": null } }}
Response shows that we have john_wayne was successfully added to users. Response also shows to is not member either of roles nor groups. Let's change that. We are only going to change variables part. Query will remain same as for initial insert.
#
Variables{ "user": { "name": "john_wayne", "groups": [ { "name": "Legends" }, { "name": "Oldschool" } ], "roles": [ { "name": "Smokers" } ] }}
#
Response{ "data": { "syncUser": { "name": "john_wayne", "roles": [ { "name": "Smokers" } ], "groups": [ { "name": "Legends" }, { "name": "Oldschool" } ] } }}
Cool, now "john_wayne
" is part of some groups. Even if those groups might not be right for him... Thing is sync will
store data just the way it was received. Let's see what will happen when we sync with following variables.
#
Variables{ "data": { "syncUser": { "name": "john_wayne", "roles": [ { "name": "Bad Boys" } ], "groups": [ { "name": "Sharks" } ] } }}
#
Response{ "data": { "syncUser": { "name": "john_wayne", "roles": [ { "name": "Bad Boys" } ], "groups": [ { "name": "Sharks" } ] } }}
As you can see john_wayne
is no longer part of Oldschool,Legends groups and it doesn't have Smokers role. Exact match!
So EYWA deleted connections to User Group and User Role entities and replaced them with new ones.
Although it is tempting to sync nested data, try to avoid deep nesting since that could delete some relations that were not ment to be deleted. Or at least were not intended to be deleted.
On the other hand this is great if you know what you are doing and you don't wan't to manually delete these relation in different query and you are absolutely sure that john doesn't wan't to be associated with those groups.
IMPORTANT
Try to remember that sync will set data in DB to exact match of input variables. Including nested relation data that will be created if it doesn't exist. But will also remove relations that were previously there.