4

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) }) }) }) }) } 
2
  • 1
    As I understand, your result variable is the JWT ? So it's a string ? Commented Jun 14, 2017 at 8:14
  • Thank you so much for you help. I just had to return the the JWT in an object like so: return { "token": result } Commented Jun 14, 2017 at 11:10

1 Answer 1

4

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)); }); }); }); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for all of your help. I really appreciate it. When you assign the value of userId, it is undefined. I believe that you need to assign this after you insert the entity into datastore.
OK :) I do not know exactly the details of the store you're using. Are you able to modify the code I posted for it to works ? The general idea is simply to return the inserted entity - you should be able to do so ;)
Thats fine. I was able to get it working. Thanks for all the help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.