Created
January 30, 2021 06:16
-
-
Save thawkin3/f2458fa32c3317374c2b62e1d43d7fdd to your computer and use it in GitHub Desktop.
JS snippet to post a new joke rating using the GraphQL API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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