1

I'm using GraphQL with Sequelize and I have a mutation that returns null.

I have tried changing the resolver for the mutation about in a different few ways, but it always returns null when I test it with GraphiQL.

Heres the resolver written in a few different ways:

1 - Initially i had the idea to add wrap the functionality in a promise.

    resolve(root, args) {
        return new Promise((resolve, reject) => {
            db.record.findOne({ where: { UID: args.UID } })
                .then(record => {
                    let newArr = undefined
                    if (record.watched == null) {
                        newArr = [args.value]
                    } else {
                        let old = record.watched
                        newArr = old.concat([args.value])
                    }
                    db.record.update({ watched: newArr }, { where: { UID: args.UID } })
                        .then(record => {
                            resolve(record)
                        })
                })
        })
    }

2 - Here, I return a promise.

    resolve(root, args) {
        db.record.findOne({ where: { UID: args.UID } })
            .then(record => {
                let newArr = undefined
                if (record.watched == null) {
                    newArr = [args.value]
                } else {
                    let old = record.watched
                    newArr = old.concat([args.value])
                }
                return db.record.update({ watched: newArr }, { where: { UID: args.UID } })
            })
    }

3 - Below I just returned the record directly.

    resolve(root, args) {
        db.record.findOne({ where: { UID: args.UID } })
            .then(record => {
                let newArr = undefined
                if (record.watched == null) {
                    newArr = [args.value]
                } else {
                    let old = record.watched
                    newArr = old.concat([args.value])
                }
                db.record.update({ watched: newArr }, { where: { UID: args.UID } })
                    .then(record => {
                        return record
                    })
            })
    }
John Jones
  • 21
  • 4

1 Answers1

0

the problem is that you aren't returning anything from your resolver, and you aren't using promises correctly. try:

resolve(root, args) {
    return db.record.findOne({ where: { UID: args.UID } })
        .then(record => {
            let newArr = undefined
            if (record.watched == null) {
                newArr = [args.value]
            } else {
                let old = record.watched
                newArr = old.concat([args.value])
            }
            return db.record.update({ watched: newArr }, { where: { UID: args.UID } });
        });
}
jmcneese
  • 91
  • 1
  • 3
  • That's not working for me. – John Jones Aug 14 '17 at 15:30
  • Still returning null or is there some other error? Have you tried dropping console.logs at various stages of the code to see what is going on? – jmcneese Aug 14 '17 at 19:40
  • It's just returning null, I think console logging will be my next approach. Still trying to wrap my head around grapql, my resolves only seem to work if i have one line in them and that one line returns a promise, anything more complex returns null. – John Jones Aug 15 '17 at 11:07
  • Yes, revolvers always need to return a promise if they are asynchronous. – jmcneese Aug 16 '17 at 14:38