Vincit / objection-graphql

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

SchemaBuilder does not recognise inherited methods or properties #19

Closed Trellian closed 6 years ago

Trellian commented 6 years ago

Hi,

I have a set of 'base' objection.js models that are generated directly from my SQL schema, in a 'models/base' folder. These are named as follows 'PersonBaseModel', 'MovieBaseModel', etc, and contain their jsonSchema in each.

In the 'models' parent folder, I have the derived classes that extend the 'base' models, which are basically empty, but they extend the 'base' models, and are named as follows 'PersonModel', 'MovieModel' etc. These models are also generated, but only once. If they already exist, they are not overwritten during regeneration of the models. This allows the user to modify the derived model, and provide custom functionality in eg. the 'PersonModel' , without fear of them being overwritten. This is pretty standard methodology for an ORM.

When I try to run the 'objection-graphql' Builder on eg. 'PersonModel', it tries to verify that the jsonSchema exists, and fails, because jsonSchema is in the parent 'PersonBaseModel'.

Is there any way to work around this? Or is there any work on 'objection-graphql' that is heading in this direction?

Trellian commented 6 years ago

Here is are examples of a base model and it's 'extend'ed model: (sorry, Markdown doesn't seem to be working on the below code...)

IwCountryBaseModel.js:

import {Model} from 'objection';

// Parent Relations let IwTimezoneBaseModel = require(__dirname + '/IwTimezoneBaseModel');

// Child relations
let IwRegionBaseModel = require(__dirname + '/IwRegionBaseModel');

/**

export default class IwCountryBaseModel extends Model {

// Table name is the only required property. static get tableName() { return 'IwCountry'; }

constructor () { }

// Optional JSON schema. This is not the database schema! // Nothing is generated based on this. This is only used // for validation. Whenever a model instance is created // it is checked against this schema. // http://json-schema.org/.

static get jsonSchema () { return { type: 'object', required: [

     'name',
     'country_code',
     'dial_code',
     'timezone_id'
  ],

  properties: {

    id: {type: 'integer'},            
    name: {type: 'string'},            
    country_code: {type: 'string'},            
    dial_code: {type: 'string'},            
    timezone_id: {type: 'integer'}
  }
};

}

// This object defines the relations to other models. static get relationMappings() {

return {

    // Parent Relations    

    iwTimezones: {
        relation: Model.BelongsToOneRelation, 
        modelClass: __dirname + IwTimezone,
            join: {
                from: 'IwCountry.timezone_id',
                to: 'IwTimezone.id'
            }
        },

    // Child Relations and Many-toMany Relations    

    iwRegions: {
        relation: Model.HasManyRelations, 
        modelClass: __dirname + IwRegion,
        join: {
            from: 'IwCountry.id',
            to: 'IwRegion.country_id'
        }
    }

};

} }

and IwCountryModel.js:

import IwCountryBaseModel from ('./base/IwCountryBaseModel.js');

/**

export default class IwCountry extends IwCountryBaseModel {

// Add/Override properties here... it won't be overwritten

}

Trellian commented 6 years ago

My bad, sorry... syntax error on my part :o