Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 35 forks source link

Usage with the latest Objection version: TypeError: Class constructor Model cannot be invoked without 'new' #7

Closed nasushkov closed 7 years ago

nasushkov commented 7 years ago

I'm trying to use this package with the latest Objection js version. Unfortunately, I've got the following error while building my schema:

Usage with the latest Objection version: TypeError: Class constructor Model cannot be invoked without 'new'

Here is my stack trace:

at propertyNameToColumnName (..\node_modules\objection\lib\model\Model.js:1244:15) < at Function.propertyNameToColumnName (..\node_modules\objection\lib\model\Model.js:518:40) < at reducePrimitiveFields (..\node_modules\objection-graphql\lib\argFactories.js:149:55) < at ..\node_modules\objection-graphql\lib\argFactories.js:30:12 < at ..\node_modules\objection-graphql\lib\SchemaBuilder.js:109:27 < at arrayReduce (..\node_modules\lodash\lodash.js:704:21) < at Function.reduce (..\node_modules\lodash\lodash.js:9698:14) < at SchemaBuilder._argsForModel ..\node_modules\objection-graphql\lib\SchemaBuilder.js:108:12) < at ..\node_modules\objection-graphql\lib\SchemaBuilder.js:81:27

It looks like the problem is in this row:

output = func(output, field, propName, modelClass.propertyNameToColumnName(propName));

koskimas commented 7 years ago

https://github.com/Vincit/objection.js/issues/388

nasushkov commented 7 years ago

@koskimas yes I saw this issue. But it's about legacy Node versions (I run 7.2.0) and ES5 inheritance. I use ES6 syntax:

export default class BaseModel extends Model {    
    $formatDatabaseJson(json) {
        json = super.$formatDatabaseJson(json)
        return mapKeys(json, (value, key) => snakeCase(key))
    }

    $parseDatabaseJson(json) {
        json = mapKeys(json, (value, key) => camelCase(key))
        return super.$parseDatabaseJson(json)
    }
}

export default class Company extends BaseModel {
    static tableName = 'company'

    static jsonSchema = {
        type: 'object',
        required: ['id', 'name'],
        properties: {
            id: {type: 'integer'},
            name: {type: 'string', maxLength: 200}
        }
    }

    static relationMappings = {
        residentialComplexes: {
            relation: Model.HasManyRelation,
            modelClass: path.join(MODELS_PATH, 'residentialComplex'),
            join: {
                from: 'company.id',
                to: 'residential_complex.company_id'
            }
        }
    }
}
koskimas commented 7 years ago

You use babel. Please read the whole issue.