Tallyb / loopback-graphql

Adding graphql to loopback, based on Apollo Server
MIT License
201 stars 18 forks source link

Unprototyped objects coming in from GraphQL to Loopback #90

Open cdokolas opened 6 years ago

cdokolas commented 6 years ago

I'm trying to use the "where" parameter in a query like this:

query getAllStatus {
  allVStatus(where: {name : {like: "abc%"}}) {
    VStatus {
      name
      field1
      field2
    }
  }
}

I get an error Cannot convert object to primitive value. Full stack trace:

Uncaught TypeError: Cannot convert object to primitive value
    at Object.String [as DataType] (<anonymous>)
    at eval (eval at DataAccessObject._coerce (E:\dev\pm-web\node_modules\loopback\node_modules\loopback-datasource-juggler\lib\dao.js:1791:11), <anonymous>:1:1)
    at Function.DataAccessObject._coerce (E:\dev\pm-web\node_modules\loopback\node_modules\loopback-datasource-juggler\lib\dao.js:1791:11)
    at Function.DataAccessObject.count (E:\dev\pm-web\node_modules\loopback\node_modules\loopback-datasource-juggler\lib\dao.js:2431:18)
    at getCount (E:\dev\pm-web\node_modules\loopback-graphql\dist\index.js:187:18)
    at Object.findAll (E:\dev\pm-web\node_modules\loopback-graphql\dist\index.js:208:12)
    at Query._a.(anonymous function) (E:\dev\pm-web\node_modules\loopback-graphql\dist\index.js:709:34)
    at resolveOrError (E:\dev\pm-web\node_modules\graphql\execution\execute.js:474:12)
    at resolveField (E:\dev\pm-web\node_modules\graphql\execution\execute.js:460:16)
    at E:\dev\pm-web\node_modules\graphql\execution\execute.js:275:18
    at Array.reduce (<anonymous>)
    at executeFields (E:\dev\pm-web\node_modules\graphql\execution\execute.js:272:42)
    at executeOperation (E:\dev\pm-web\node_modules\graphql\execution\execute.js:212:10)
    at E:\dev\pm-web\node_modules\graphql\execution\execute.js:120:13
    at Promise (<anonymous>)
    at Object.execute (E:\dev\pm-web\node_modules\graphql\execution\execute.js:119:10)

All I've managed to find out is that the where object that reaches DataAccessObject._coerce (loopback-datasource-juggler\lib\dao.js) is an unprototyped object and the line with the statement val = DataType(val); (currently 1791) causes the exception.

I've not determined which function is called, but it belongs to the loopback model of the table. It may be ModelBuilder.prototype.resolveType but I've been unable to step into the function.

The unprototyped object probably comes from GraphQL's values.js function getVariableValues (instantiates stuff with Object.create(null)).

Note: I'm using the GraphiQL interface to test all this. It is worth noting that using a variable works much better:

query getAllStatus($where: JSON!) {
  allVStatus(where: $where) {
...

and the variable is declared with {"where": {"name": {"like": "abc%"}}}