0
votes

I'm following this tutorial by Robin Wieruch

While implementing User Sign Up using jwt with GraphQL, I'm not able to provide the values through GraphQL Mutation

Server.js

const server = new ApolloServer({
  typeDefs: gql(typeDefs),
  resolvers,
  context: { db },
  secret: process.env.SECRET,
});

models/user.js

export default (sequelize, DataTypes) => {
  const User = sequelize.define('user', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    username: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false,
     },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
      notempty: true,
     len: [8],
     },
    },
   email: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false,
      validate:{
      isEmail: true,
    },
  },
  createdAt: {
  },
   createdAt: {
     type: DataTypes.DATE,
     allowNull: true
   },
   updatedAt:  {
     type: DataTypes.DATE,
     allowNull: true
   },
   deleted_at: {
     type: DataTypes.DATE,
     allowNull: true
       }
     },
     {
       freezeTableName: true,
     });
     User.beforeCreate( async user => {
       user.password = await user.generatePasswordHash();
     });
     User.prototype.generatePasswordHash = async function() {
       const saltRounds = 10;
       return await bcrypt.hash(this.password, saltRounds);
     };
     return User;
   }; 

schema

type Mutation {
signUp(username: String!, email: String!, password: String!): Token!

}

  type Token {
    token: String!
  }

resolvers

signUp: async(
        parent,
        { username, password, email},
        {db, secret },) => {
            const user =  await db.user.create({
              username,
              password,
              email,
            });
           return { token: createToken(user, secret, '30m')};
        },


I'm currently stuck with below GraphiQL Mutation:


mutation($username: String!, $password: String!, $email: String!){
  signUp(username: $username, password: $password, email: $email){
  u
  }
}

As you can see, when I type, u, I should be seeing username, password and email getting auto-populated. Or may be my mutation format is not correct.

Please correct, where I'm wrong.

1
signUp(.....): Token! - you should see tokenxadm
@xadm Means, should I provide token also along with the Mutation?88dax
this is a mutation resultxadm
@xadm So, what should I do about it?88dax
'The token, whether its obtained on registration or login, is returned to the client application after a successful GraphQL signIn or signUp mutation. The client application must store the token somewhere like the browser’s session storage.'xadm

1 Answers

0
votes
mutation($email: String!, $username: String!, $password: String!){
    signUp(email: $email, username: $username, password: $password){ 
        token 
    }
}