Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Created January 30, 2021 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thawkin3/f2458fa32c3317374c2b62e1d43d7fdd to your computer and use it in GitHub Desktop.
Save thawkin3/f2458fa32c3317374c2b62e1d43d7fdd to your computer and use it in GitHub Desktop.
JS snippet to post a new joke rating using the GraphQL API
fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
mutation CreateRating {
rating(jokeId: ${jokeId}, score: ${score}) {
id
score
jokeId
}
}
`,
}),
})
.then(res => res.json())
.then(res => {
const rating = res.data.rating
const jokeToUpdate = jokes.find(joke => joke.id === rating.jokeId)
jokeToUpdate && jokeToUpdate.ratings.push(rating)
})
.finally(() => {
ratingInput.checked = false
displayNextJoke()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment