Yes, but also, sadly, no.
The issue is not related to the application not performing the refetching, but rather to the refetching causing the server to restart (sometimes it is ECONNRESET, sometimes ECONNREFUSED).
I drop the creating and deleting components and services, in case I am doing something wrong:
// DELETE SERVICE
export const deleteItem: MutationResolvers['deleteItem'] = async ({
id,
}) => {
const item = await db.item.findUnique({
where: { id },
select: { folderName: true },
})
if (!item) {
throw new Error('Item not found')
}
const folderName = item.folderName
const folderPath = `api/folders/${folderName}`
logger.info(`Deleting folder ${folderName}`)
try {
// Delete the folder synchronously
fs.rmdirSync(folderPath)
logger.info(`Folder ${folderName} deleted successfully`)
} catch (error) {
logger.error(`Error deleting folder ${folderName}: ${error}`)
throw error
}
// Remove the entry from the database
return db.item.delete({ where: { id } })
}
// Button performing the delete mutation
const ActionButton = ({ icon, variant, action, id }: ActionButtonProps) => {
const [deleteItem] = useMutation(DELETE_ITEM, {
refetchQueries: [{ query: FETCH_ITEMS }],
onCompleted: () => {
toast.success('Item deleted!')
},
onError: (error) => {
toast.error(
'Error trying to delete: ' +
error.message
)
console.error(error)
},
})
// other stuff
const handleClick = (action: string, id: number | string) => {
if (action === 'delete') {
// If alert is confirmed, invoke a mutation to delete the item by id
if (
confirm('Are you sure you wish to delete item with id ' + id + '?')
) {
deleteItem({ variables: { id: id } })
}
} else if (action === 'edit') {
// Invoke a mutation to edit the item by id
} else if (action === 'show') {
// Invoke a query to show the images of the item by id
}
}
const color = buildColor(variant)
return (
<>
<Toaster />
<button className={color} onClick={() => handleClick(action, id)}>
<FontAwesomeIcon icon={icon} className="mr-2 ml-2 mt-2 mb-1" />
</button>
</>
)
}
// Creation service
export const createItem: MutationResolvers['createItem'] = async ({
input,
}) => {
const itemCounter: number = await db.item.count()
// Create the item
const createdItem = await db.item.create({
data: {
...input,
folderName: Number.isNaN(itemCounter) ? 'R0' : `R${itemCounter}`,
},
})
const folderName = createdItem.folderName
// Create the folder using the folderName variable
fs.mkdirSync(`api/folders/${folderName}`, { recursive: true })
logger.info(`Created folder ${folderName}`)
return createdItem
}
// Creation modal
const ItemModal = ({ isModalOpen, setIsModalOpen }: ItemModalProps) => {
const formMethods = useForm({ mode: 'onBlur' })
const [create, { loading, error }] = useMutation(CREATE_ITEM, {
refetchQueries: [{ query: FETCH_ITEMS }],
onCompleted: () => {
formMethods.reset()
closeModal()
toast.success('Item created!')
},
})
const onSubmit = (data: FormValues) => {
create({ variables: { input: data } })
console.log(data)
}
const closeModal = () => {
setIsModalOpen(false)
console.log(isModalOpen)
}
const customModalStyles = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)',
backgroundColor: '#fff',
padding: '2rem',
borderRadius: '0.5rem',
},
overlay: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
zIndex: '50',
},
}
return (
<Modal
isOpen={isModalOpen}
onRequestClose={closeModal}
style={customModalStyles}
>
<div className="p-6 bg-white rounded max-w-lg mx-auto">
<Toaster />
<h2 className="text-xl mb-4">Add item</h2>
<Form onSubmit={onSubmit} error={error} formMethods={formMethods}>
<FormError error={error} wrapperClassName="text-red-500 mb-4" />
// Form to insert data
<div className="flex justify-end mt-4">
<Submit
className="flex items-center justify-center bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded"
disabled={loading}
>
<FontAwesomeIcon icon={faSave} className="mr-2" />
Save
</Submit>
<button
className="flex items-center justify-center bg-gray-300 hover:bg-gray-400 text-gray-700 font-medium py-2 px-4 rounded ml-4"
onClick={closeModal}
>
<FontAwesomeIcon icon={faTimes} className="mr-2" />
Abort
</button>
</div>
</Form>
</div>
</Modal>
)
}
Within the code above, when I create an item, the toaster appears, so the onCompleted()
method gets called; however, that is when the server side restarts. When I try to delete the item, it frequently throws an error about the server not being up.