rsnext/examples/with-graphql-hooks/components/post-upvoter.js
Ludovico Fischer b3a631305d
fix(examples): make with-graphql-hooks run correctly again (#20929)
Fixes #20474

* Switch API url to working URL used in apollo example
* Update deps
* Move styled jsx out of the way to avoid demonstrating too many
  unfamiliar concepts at once

dev, build and start work now.
2021-01-11 13:55:18 +00:00

37 lines
693 B
JavaScript

import React from 'react'
import { useMutation } from 'graphql-hooks'
const UPDATE_POST = `
mutation votePost($id: String!) {
votePost(id: $id) {
id
votes
__typename
}
}
`
export default function PostUpvoter({ votes, id, onUpdate }) {
const [updatePost] = useMutation(UPDATE_POST)
return (
<button
className="upvote"
onClick={async () => {
try {
const result = await updatePost({
variables: {
id,
},
})
onUpdate && onUpdate(result)
} catch (e) {
console.error('error upvoting post', e)
}
}}
>
{votes}
</button>
)
}