Skip to content

Instantly share code, notes, and snippets.

@xperiandri
Last active February 13, 2022 22:08
Show Gist options
  • Save xperiandri/4ccf9ad765cb4c5862afb0d4d0ba9b24 to your computer and use it in GitHub Desktop.
Save xperiandri/4ccf9ad765cb4c5862afb0d4d0ba9b24 to your computer and use it in GitHub Desktop.
mutation grupo ($id: GrupoID!, $patchOperations: [JsonPatchOperation!]) {
grupo (id: $id) {
patch (operations: $patchOperations)
}
}
let JSONPatchOperationInputType<'t when 't : not struct> =
let name = sprintf "Input%s_JSON_PatchOperation" (typeof<'t>.Name.Replace ("ViewModel", System.String.Empty))
Define.InputObject<Operation<'t>>
(name = name, description = "JSON patch operation",
fields =
[ Define.Input ("op", String)
Define.Input ("path", String)
Define.Input ("from", String)
Define.Input ("value", String, null) ])
Define.AsyncField(name = "patch",
description = "Patch user group via JSON PATCH",
inputs = [ Inputs.JSONPatchOperationInputType<GrupoViewModel> ],
resolver = asyncPatchGrupo)
let asyncPatchGrupo (ctx : ResolveFieldContext) struct (root : Root, grupoId) = asyncResult {
// Get operations array / Получаем массив операций
let! patchOperations = ctx.ArgJSONPatchOperations<GrupoViewModel>()
let grupoContainer = root.BibliotekoDatabase.GrupojContainer
// Create patch document form operations array / Создаём патч документ из массива операций
let patchDocument = new JsonPatchDocument<GrupoViewModel>(patchOperations, new DefaultContractResolver())
let dto = GrupoViewModel.ToViewModel grupo // Create a view model / Создаём view model
do patchDocument.ApplyTo dto // Patch the view model / Патчим view model
let patchedGrupo = dto.FromViewModelChanges grupo // Apply result to the model / Применяем результат к модели
let! grupoContainer.ReplaceItemAsync patchedGrupo // Save to database / Сохраняем в базу
return! Success patchedGrupo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment