Kaynağa Gözat

stringify ids in handler, refactor out injectTypename, duplicate id as characterId

Jason Gorst 4 gün önce
ebeveyn
işleme
9e89eb875d

+ 2 - 2
app/mutations/characters/useDeleteCharacter.js

@@ -4,10 +4,10 @@ export const useDeleteCharacter = defineMutation(() => {
   const { transaction } = useOptimisticUpdate()
 
   const { mutate, ...mutation } = useMutation({
-    onMutate({ id }) {
+    onMutate: ({ id })=> {
       const optionsCache = useOptionsCache()
       const tx = transaction()
-      tx.remove("character", _toString(id))
+      tx.remove("character", id)
       optionsCache.update()
 
       return { ...tx, rollbackOptions: optionsCache.rollback }

+ 1 - 1
app/mutations/characters/useUpdateCharacter.js

@@ -7,7 +7,7 @@ export const useUpdateCharacter = defineMutation(() => {
     onMutate: ({ id, updates }) => {
       const optionsCache = useOptionsCache()
       const tx = transaction()
-      tx.set("character", _toString(id), updates)
+      tx.set("character", id, updates)
       optionsCache.update()
 
       return { ...tx, rollbackOptions: optionsCache.rollback }

+ 11 - 18
server/socketio/handlers/characterHandlers.js

@@ -91,27 +91,20 @@ export function characterHandlers(_io, socket) {
       idValidator: characterSchema.shape.id.parse
     }
 
-    function typenameMutator(rawResult) {
-      if (_isPlainObject(rawResult) && _has(rawResult, "id")) {
-        _set(rawResult, "__typename", "character")
-      } else if (_isArray(rawResult)) {
-        _forEach(rawResult, (entry) =>
-          _set(entry, "__typename", "character")
-        )
-      }
-
-      return rawResult
-    }
-
-    let mutator
+    const mutators = [
+      stringifyIds,
+      injectTypename("character"),
+      injectEntityId("character")
+    ]
 
     if (_has(options, "mutator")) {
-      mutator = _flow([options.mutator, typenameMutator])
-      _unset(options, "mutator")
-    } else {
-      mutator = typenameMutator
+      mutators.unshift(options.mutator)
     }
 
-    return await executeQuery({ ...defaultOptions, ...options, mutator })
+    return await executeQuery({
+      ...defaultOptions,
+      ...options,
+      mutator: _flow(mutators)
+    })
   }
 }

+ 13 - 0
server/utils/injectEntityId.js

@@ -0,0 +1,13 @@
+export default function injectEntityId(entity) {
+  function entityIdMutator(_entity, _rawResult) {
+    if (_isPlainObject(_rawResult) && _has(_rawResult, "id")) {
+      _set(_rawResult, `${_entity}Id`, _rawResult.id)
+    } else if (_isArray(_rawResult)) {
+      _forEach(_rawResult, (entry) => _set(entry, `${_entity}Id`, entry.id))
+    }
+
+    return _rawResult
+  }
+
+  return (rawResult) => entityIdMutator(entity, rawResult)
+}

+ 13 - 0
server/utils/injectTypename.js

@@ -0,0 +1,13 @@
+export default function injectTypename(typename) {
+  function typenameMutator(_typename, _rawResult) {
+    if (_isPlainObject(_rawResult) && _has(_rawResult, "id")) {
+      _set(_rawResult, "__typename", _typename)
+    } else if (_isArray(_rawResult)) {
+      _forEach(_rawResult, (entry) => _set(entry, "__typename", _typename))
+    }
+
+    return _rawResult
+  }
+
+  return (rawResult) => typenameMutator(typename, rawResult)
+}

+ 9 - 0
server/utils/stringifyIds.js

@@ -0,0 +1,9 @@
+export default function stringifyIds(rawResult) {
+  if (_isPlainObject(rawResult) && _has(rawResult, "id")) {
+    _update(rawResult, "id", _toString)
+  } else if (_isArray(rawResult)) {
+    _forEach(rawResult, (entry) => _update(entry, "id", _toString))
+  }
+
+  return rawResult
+}