Authenticate a User with a GraphQL Mutation

Share this video with your friends

Send Tweet

Mutations give you the ability to invoke backend functions from the client. In this lesson, we will use a mutation to authenticate a user with their username and password. Authorized users will receive a token that can be used to identify the current user in future operations.

To follow along with these queries, go to the Pet Library GraphQL Playground.

ganesh arumugam
ganesh arumugam
~ 3 years ago

I am not able to run the mutation and query as part of the same document since login token is refreshed with new value every time it runs. So it makes it invalid for the Me query.

~ 3 years ago

I tried to add variables with my username and password to log in, but it didn't work

"message": "Unknown type "username".",

//Code mutation($myusername: username $mypassword: password) { logIn(username: $myusername password: $mypassword) { customer { name } token } }

//Variables { "myusername": "hahaha", "mypassword": "yes" }

Eve Porcello
Eve Porcello(instructor)
~ 3 years ago

I tried to add variables with my username and password to log in, but it didn't work

"message": "Unknown type "username".",

//Code mutation($myusername: username $mypassword: password) { logIn(username: $myusername password: $mypassword) { customer { name } token } }

//Variables { "myusername": "hahaha", "mypassword": "yes" }

Hello! Make sure that you're using the type correctly in the mutation as the variable. The change you'd need to make is on line 1 of the mutation:

mutation($myusername: ID!, $mypassword: String!) {
  logIn(username: $myusername, password: $mypassword) {
    customer {
      name
    }
    token
  }
}
Jayden
Jayden
~ 3 years ago

Account with that username: ep123 not found.".

What should I do with this error?

Eve Porcello
Eve Porcello(instructor)
~ 3 years ago

Hi Jayden, you just need to create your own account to log in. You can watch the previous video for instructions on how to create an account, and then log in with those details in this video.

Thanks!