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)
})
})
})
})
}