Using mutations on the frontend
As we learned in the previous chapter, mutations in GraphQL are used to change the state of the backend (similar to POST
requests in REST). We are now going to implement mutations for signing up and logging in to our app.
Follow these steps:
- Create a new
src/api/graphql/users.js
file and importgql
:import { gql } from '@apollo/client/core/index.js'
- Then, define a new
SIGNUP_USER
mutation, which takes a username and a password and calls thesignupUser
mutation field:export const SIGNUP_USER = gql` mutation signupUser($username: String!, $password: String!) { signupUser(username: $username, password: $password) { username } } `
- Edit
src/pages/Signup.jsx
and replace the currentuseMutation
hook from TanStack React Query with the one from Apollo Client. As we did before foruseQuery
, we are also going to rename this hook...