Unverified Commit 15133b47 authored by Sahil Rajput's avatar Sahil Rajput Committed by GitHub

Fix typescript types. (#23771)

Fix typescript type for cache and null check in mutation result.

## Bug

- fixes : https://github.com/vercel/next.js/issues/23770

**That issue deals with -**

- Return the codeflow if data is not returned from the mutation, and that fixes this warning.
![Screenshot from 2021-04-07 18-24-44](https://user-images.githubusercontent.com/31458531/113873069-18353a80-97d2-11eb-8c8c-9e9c9d280f95.png)

- Supply typescript typings for the target cache object.
![Screenshot from 2021-04-07 18-52-15](https://user-images.githubusercontent.com/31458531/113873504-80841c00-97d2-11eb-9b14-17d16a524c84.png)

Thanks.!
parent b2ee0a93
import Link from 'next/link'
import { useState } from 'react'
import {
ViewerQuery,
useViewerQuery,
useUpdateNameMutation,
ViewerDocument,
......@@ -19,21 +20,18 @@ const Index = () => {
},
//Follow apollo suggestion to update cache
//https://www.apollographql.com/docs/angular/features/cache-updates/#update
update: (
store,
{
data: {
updateName: { name },
},
}
) => {
update: (cache, mutationResult) => {
const { data } = mutationResult
if (!data) return // Cancel updating name in cache if no data is returned from mutation.
// Read the data from our cache for this query.
const { viewer } = store.readQuery({ query: ViewerDocument })
const { viewer } = cache.readQuery({
query: ViewerDocument,
}) as ViewerQuery
const newViewer = { ...viewer }
// Add our comment from the mutation to the end.
newViewer.name = name
newViewer.name = data.updateName.name
// Write our data back to the cache.
store.writeQuery({ query: ViewerDocument, data: { viewer: newViewer } })
cache.writeQuery({ query: ViewerDocument, data: { viewer: newViewer } })
},
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment