diff --git a/lib/SchemaBuilder.js b/lib/SchemaBuilder.js index 5b50a71..ece9978 100644 --- a/lib/SchemaBuilder.js +++ b/lib/SchemaBuilder.js @@ -131,8 +131,9 @@ class SchemaBuilder { _.forOwn(this.models, (modelData) => { const defaultFieldName = fieldNameForModel(modelData.modelClass); - const singleFieldName = modelData.opt.fieldName || defaultFieldName; - const listFieldName = modelData.opt.listFieldName || (`${defaultFieldName}s`); + const schema = modelData.modelClass.jsonSchema; + const singleFieldName = schema.singleName || modelData.opt.fieldName || defaultFieldName; + const listFieldName = schema.collectiveName || modelData.opt.listFieldName || (`${defaultFieldName}s`); fields[singleFieldName] = this._rootSingleField(modelData); fields[listFieldName] = this._rootListField(modelData); diff --git a/lib/utils.js b/lib/utils.js index cbc30e8..2798c4b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,7 +8,9 @@ function isExcluded(opt, prop) { } function typeNameForModel(modelClass) { - return _.upperFirst(_.camelCase(modelClass.tableName)); + const schema = modelClass.jsonSchema; + const name = schema.singleName || schema.title || modelClass.tableName; + return _.upperFirst(_.camelCase(name)); } module.exports = { diff --git a/tests/integration.js b/tests/integration.js index b75c2fc..a867a6f 100644 --- a/tests/integration.js +++ b/tests/integration.js @@ -1281,4 +1281,34 @@ describe('integration tests', () => { }); })); }); + + describe('get singular and plural names from jsonSchema', () => { + let schema; + + before(() => { + const { Model } = require('objection'); + class Human extends Model { + static get tableName() { return 'humans' } + static get jsonSchema() { + return { + type: 'object', + singleName: 'person', + collectiveName: 'mankind', + properties: { id: { type: 'integer' }} + } + } + } + schema = mainModule + .builder() + .model(Human) + .build(); + }); + + it('has a correct type name', () => { + const fields = schema._queryType._fields + expect(fields.person.type).to.be.a(GraphQLObjectType); + expect(fields.mankind.type).to.be.a(GraphQLList); + }); + }); + });