Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get singular and plural model names from jsonSchema #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/SchemaBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const listFieldName = schema.collectiveName || modelData.opt.listFieldName || (`${defaultFieldName}s`);
const listFieldName = schema.collectiveName || modelData.opt.listFieldName || (`${singleFieldName}s`);


fields[singleFieldName] = this._rootSingleField(modelData);
fields[listFieldName] = this._rootListField(modelData);
Expand Down
4 changes: 3 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
30 changes: 30 additions & 0 deletions tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});