3
votes

I'm having some trouble returning data to a GraphQL mutation. In the mutation, you provide an email and password to sign up. From there, GraphQL should return a JSON web token containing the usersId.

Even though the password is being hashed and the email and password is being saved to the database and the JWT is made with the users id as a payload, it responds with this

{
  "data": {
    "signUp": {
      "token": null,
      "email": null
    }
  }
}

Here is the GraphQL Query:

mutation {
  signUp(email: "[email protected]", password: "password") {
    token //Should return a JWT
    email // Should return the users email address
  }
}

Here is the mutation: (When the mutation is run, it logs the JWT to the console but doesnt return it to GraphQL)

const mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    signUp: {
      type: UserType,
      args: {
        email: { type:  new GraphQLNonNull(GraphQLString) },
        password: { type: new GraphQLNonNull(GraphQLString) }
      },
      resolve (parentValue, args) {
        return signUp(args) // Calls a function in another file with the args
          .then((result) => {
            console.log(result) // Logs the JWT to the console.
            return result
          })
      }
    }
  }
})

Here is the userType:

const UserType = new GraphQLObjectType({
  name: 'UserType',
  fields: {
    id: { type: GraphQLID },
    email: { type: GraphQLString },
    token: { type: GraphQLString }
  }
})

Here us the signUp function:

function signUp ({ email, password }) {
  return new Promise((resolve, reject) => {
    bcrypt.hash(password, 10, function(err, password) {
      const userKey = datastore.key('User')
      const entity = {
        key: userKey,
        data: {
          email,
          password
        }
      }

      datastore.insert(entity)
        .then(() => {
          let userId = userKey.path[1]
          jwt.sign({userId}, 'secret', function (err, token) {
            resolve(token)
          })
        })
    })
  })
}
1
As I understand, your result variable is the JWT ? So it's a string ?yachaka
Thank you so much for you help. I just had to return the the JWT in an object like so: return { "token": result }ProfessorNudelz

1 Answers

3
votes

Following your comment:

As your signUp mutation is of UserType, you should not resolve it with an object { token: ... } but with an User object. That will allow you to query others fields on the User when executing the mutation.

Following your example, that could be:

function signUp ({ email, password }) {
  return new Promise((resolve, reject) => {

    bcrypt.hash(password, 10, function(err, password) {
      if (err) return reject(err);

      const userKey = datastore.key('User')
      const userId = userKey.path[1];

      jwt.sign({userId}, 'secret', function (err, token) {
        if (err) return reject(err);

        const entity = {
          key: userKey,
          data: {
            email,
            password,
            token,
          },
        };

        datastore.insert(entity)
          .then(inserted => resolve(inserted));
      });
    });

  });

}