the-road-to-graphql / fullstack-apollo-express-postgresql-boilerplate

💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
https://roadtoreact.com
MIT License
1.2k stars 265 forks source link

DataLoader issue with $in #48

Open micahbowerbank opened 5 years ago

micahbowerbank commented 5 years ago

When I use the following from the example

  const users = await models.User.findAll({
    where: {
      id: {
        $in: keys,
      },
    },
  });

I get the following error

{
  "errors": [
    {
      "message": "Invalid value { '$in': [ 1, 2, 3 ] }",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "accounts",
        0,
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Invalid value { '$in': [ 1, 2, 3 ] }",
            "    at Object.escape (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/sql-string.js:66:11)",
            "    at Object.escape (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:936:22)",
            "    at Object._whereParseSingleValueObject (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2432:41)",
            "    at Object.whereItemQuery (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2136:21)",
            "    at Utils.getComplexKeys.forEach.prop (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1997:25)",
            "    at Array.forEach (<anonymous>)",
            "    at Object.whereItemsQuery (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1995:35)",
            "    at Object.getWhereConditions (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2459:19)",
            "    at Object.selectQuery (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1143:28)",
            "    at QueryInterface.select (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/query-interface.js:1105:27)",
            "    at Promise.try.then.then.then (/Users/***/Documents/code/*** /***-server/node_modules/sequelize/lib/model.js:1598:34)",
            "    at tryCatcher (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/util.js:16:23)",
            "    at Promise._settlePromiseFromHandler (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/promise.js:512:31)",
            "    at Promise._settlePromise (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/promise.js:569:18)",
            "    at Promise._settlePromise0 (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/promise.js:614:10)",
            "    at Promise._settlePromises (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/promise.js:694:18)",
            "    at _drainQueueStep (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/async.js:138:12)",
            "    at _drainQueue (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/async.js:131:9)",
            "    at Async._drainQueues (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/async.js:147:5)",
            "    at Immediate.Async.drainQueues [as _onImmediate] (/Users/***/Documents/code/*** /***-server/node_modules/bluebird/js/release/async.js:17:14)",
            "    at processImmediate (timers.js:632:19)"
          ]
        }
      }
    }
  ],
  "data": {
    "accounts": null
  }
}

if I change it to the following then everything works

  const users = await models.User.findAll({
    where: {
      id: keys,
    },
  });

I don't know if this is because I have a newer package or something. But it took me hours to figure it out, so I figured I would share with everyone else

rwieruch commented 5 years ago

I ran into Sequelize deprecation warnings for all these operators with $. Maybe they deprecated it in the newest version. Which one are you using? Can you try to follow this comment https://github.com/sequelize/sequelize/issues/8417#issuecomment-337884136 as fix?

matthewboman commented 5 years ago

I got the same error as @micahbowerbank with Sequelize 4.41.2. The solution he gave works.

rwieruch commented 5 years ago

Thanks for confirming this @crashspringfield I will take this issue into for the next book edition.

In case someone else runs into this issue, please write a comment here and how you worked around the issue :)

davidwyett commented 4 years ago

https://sequelize.org/master/manual/querying.html#where

imported Op module from sequelize and replaced $in with [Op.in]

JayK19 commented 4 years ago

So, use [Sequelize.Op.in]: keys instead of $in: keys. It works!

Davigetz commented 4 years ago

Same Issue but it works when updating to @micahbowerbank solution.