Kiba Gateaux
Kiba Gateaux

Reputation: 31

Relay nodeDefinition throws "Right-hand side of 'instanceof' is not callable"

I am learning GraphQL and have just started implementing graphql-relay on my node server in the past couple days. When declaring my Relay node definitions I am getting the error "Right-hand side of 'instanceof' is not callable", where the right-hand side is an object with a constructor function. As far as I can tell this is not due to improper usage, also considering this is copied from the docs. I am not sure what the expected outcome is when it is properly working, I assume it returns the GraphQL type to be put through the works and have the requested data returned.

var {nodeInterface, nodeField} = nodeDefinitions(
  (globalId) => {
    var {type, id} = fromGlobalId(globalId);
     console.log(type);
    if (type === 'User') {
      return db.models.user.findById(id)
   } else if (type === 'Video') {
       return db.models.video.findById(id)
    }
    else if (type === 'Producer') {
      return db.models.user.findById(id)
    }
    else if (type === 'Viewer') {
      return db.models.user.findById(id)
    }else {
      return null;
    }
  },
  (obj) => {

  console.log(obj);               // Sequelize object
  console.log(User);              // user
  console.log(User.constructor);  // valid constructor

  // This is where the error occurs
  if (obj instanceof User) {
    return UserType;
  } else if (obj instanceof Video)  {
    return VideoType;
  } else {
    return null;
  }
});

Notes:

Upvotes: 0

Views: 523

Answers (2)

zhi cheng
zhi cheng

Reputation: 91

You need to change the code from:

...

if (obj instanceof User) {

...

} else if (obj instanceof Video)  {

....

to:

...

if (obj instanceof User.Instance) {

...

} else if (obj instanceof Video.Instance)  {

....

Upvotes: 0

dmnd
dmnd

Reputation: 2476

An "object with a constructor function" isn't callable. Probably you need to make that object a class with a constructor like this:

class User {
  constructor(id, name, email) {
    this.id = id;
    // etc
  }
}

Upvotes: 0

Related Questions