Unverified Commit a2bea9b4 authored by WeichienHung's avatar WeichienHung Committed by GitHub

Add mutation example to with-typescript-graphql (#16742)

Fixes  https://github.com/vercel/next.js/issues/16550

Add a graphql mutation operation in this example.
The changes are:
1. Define mutation in schema `type-defs.graphqls`
2. Add mutation to `viewer.graphql ` that compiled to typescript code.
3. Add mutation resolver in `resolvers.ts`
4. [UI] add a input box and button to submit mutation request to change name. 

screenshot of change
![mutation_example](https://user-images.githubusercontent.com/1462027/91797123-5ffc4e00-ec54-11ea-910b-3ddb399b42f0.gif)
parent 530b1461
import { QueryResolvers } from './type-defs.graphqls'
import { QueryResolvers, MutationResolvers } from './type-defs.graphqls'
import { ResolverContext } from './apollo'
const userProfile = {
id: String(1),
name: 'John Smith',
status: 'cached',
}
const Query: Required<QueryResolvers<ResolverContext>> = {
viewer(_parent, _args, _context, _info) {
return { id: String(1), name: 'John Smith', status: 'cached' }
return userProfile
},
}
const Mutation: Required<MutationResolvers<ResolverContext>> = {
updateName(_parent, _args, _context, _info) {
userProfile.name = _args.name
return userProfile
},
}
export default { Query }
export default { Query, Mutation }
type Query {
viewer: User!
}
type Mutation {
updateName(name: String!): User!
}
......@@ -6,3 +6,11 @@ query Viewer {
status
}
}
mutation UpdateName($name: String!) {
updateName(name: $name) {
id
name
status
}
}
import Link from 'next/link'
import { useViewerQuery, ViewerDocument } from '../lib/viewer.graphql'
import { useState } from 'react'
import {
useViewerQuery,
useUpdateNameMutation,
ViewerDocument,
} from '../lib/viewer.graphql'
import { initializeApollo } from '../lib/apollo'
const Index = () => {
const { viewer } = useViewerQuery().data!
const [newName, setNewName] = useState('')
const [updateNameMutation] = useUpdateNameMutation()
const onChangeName = () => {
updateNameMutation({
variables: {
name: newName,
},
//Follow apollo suggestion to update cache
//https://www.apollographql.com/docs/angular/features/cache-updates/#update
update: (
store,
{
data: {
updateName: { name },
},
}
) => {
// Read the data from our cache for this query.
const { viewer } = store.readQuery({ query: ViewerDocument })
const newViewer = { ...viewer }
// Add our comment from the mutation to the end.
newViewer.name = name
// Write our data back to the cache.
store.writeQuery({ query: ViewerDocument, data: { viewer: newViewer } })
},
})
}
return (
<div>
You're signed in as {viewer.name} and you're {viewer.status} go to the{' '}
You're signed in as {viewer.name} and you're {viewer.status}. Go to the{' '}
<Link href="/about">
<a>about</a>
</Link>{' '}
page.
<div>
<input
type="text"
placeholder="your new name..."
onChange={(e) => setNewName(e.target.value)}
/>
<input type="button" value="change" onClick={onChangeName} />
</div>
</div>
)
}
......
......@@ -6,7 +6,7 @@ exports[`Index renders the html we want 1`] = `
Baa
and you're
Healthy
go to the
. Go to the
<a
href="/about"
......@@ -17,5 +17,17 @@ exports[`Index renders the html we want 1`] = `
</a>
page.
<div>
<input
onChange={[Function]}
placeholder="your new name..."
type="text"
/>
<input
onClick={[Function]}
type="button"
value="change"
/>
</div>
</div>
`;
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