Modify the Resolver

Learn how to modify the resolver in the GraphQL application.

Implement the resolver

Inside the schema.resolvers.go file, we implement all the resolvers.

Register

First, we implement the resolver for the registration.

Press + to interact
func (r *mutationResolver) Register(ctx context.Context, input model.NewUser) (string, error) {
// perform an user registration
var token string = r.userService.Register(input)
// if JWT token is empty, return an error
if token == "" {
return "", errors.New("registration failed")
}
// return JWT token
return token, nil
}

Below is an explanation of the code above:

  • In line 3, the r.userService.Register(input) function performs user registration. This function returns the JWT token.

  • In line 11, if the registration succeeds, the JWT token is returned.

Login

We implement the resolver for the login.

Press + to interact
func (r *mutationResolver) Login(ctx context.Context, input model.LoginInput) (string, error) {
// perform a login
var token string = r.userService.Login(input)
// if JWT token is empty, return an error
if token == "" {
return "", errors.New("login failed, invalid email or password")
}
// return JWT token
return token, nil
}

Below is an explanation of the code above:

  • In line 3, the r.userService.Login(input) function performs a login for the registered user.

  • In line 11, if the login is successful, the JWT token is returned.

Create a new blog

We ...