javascript - Return data to GraphQL from Promise - Stack Overflow

I'm having some trouble returning data to a GraphQL mutation. In the mutation, you provide an emai

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

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)
          })
        })
    })
  })
}
Share edited Jul 5, 2017 at 9:15 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Jun 14, 2017 at 7:01 ProfessorNudelzProfessorNudelz 431 silver badge6 bronze badges 2
  • 1 As I understand, your result variable is the JWT ? So it's a string ? – yachaka 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 } – ProfessorNudelz Commented Jun 14, 2017 at 11:10
Add a ment  | 

1 Answer 1

Reset to default 4

Following your ment:

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

  });

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744883471a4598980.html

相关推荐

  • javascript - Return data to GraphQL from Promise - Stack Overflow

    I'm having some trouble returning data to a GraphQL mutation. In the mutation, you provide an emai

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信