Error GraphQL with Sequelize

2

I'm having an error in the return of a Mutation . Apparently it can not read the id field, but the object I'm sending to GraphQL has this field, and since I'm new to GraphQL I did not understand what the error means.

The error:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field UserVoucher.id.",
      "locations": [
        {
          "line": 48,
          "column": 5
        }
      ],
      "path": [
        "createUserVoucher",
        "id"
      ]
    }
  ],
  "data": {
    "createUserVoucher": null
  }
}

The mutation and where I enter the bank (it correctly inserts the element):

createUserVoucher: (parent, args, { user, models }) => {
  args['userId'] = user.id;
  models.UserVoucher.count({
    where: {utilized: true, pinId: args.pinId, userId: args.userId}
  }).then( c => {
    if (c > 0) {
      return []
    } else {
      models.Voucher.findOne({
        where: {
          id: args.voucherId
        }
      }).then( voucher => {
        models.UserVoucher.count({
          where: { voucherId: args.voucherId }
        }).then( c => {
          if (c < voucher.captured) {
            if (user.name, user.gender, user.username, user.email, user.phoneNumber, user.city, user.state, user.cpf) {
              return models.UserVoucher.create(args) //RETORNA AQUI
            }
          }
        })
      })
    }
  });
  return []
}

The UserVoucher setting:

type UserVoucher {
  id: Int!
  nodeId: Int!
  userId: ID!
  voucherId: ID!
  voucher: Voucher
  pinId: ID!
  capturedAt: DateTime
  utilized: Boolean
}
';

The mutation definition:

createUserVoucher(
  pinId: Int!, 
  voucherId: Int!
): UserVoucher

Any idea what I can do to solve this or at least a light to debugar better?

    
asked by anonymous 28.02.2018 / 22:26

1 answer

0

After asking the OS in English, I realized that the error was in Promises and not in GraphQL.

createUserVoucher: async (parent, args, { user, models }) => {
  args['userId'] = user.id;
  const c = await models.UserVoucher.count({
    where: {utilized: true, pinId: args.pinId, userId: args.userId}
  })
  if (c > 0) return null
  const voucher = await models.Voucher.findOne({
    where: { id: args.voucherId }
  })
  const c2 = await models.UserVoucher.count({ 
    where: { voucherId: args.voucherId }
  })
  return models.UserVoucher.create(args) // still need a return here
}

In addition a refactoring was suggested using Async Await which greatly improved the code reading.

    
14.03.2018 / 18:18