Skip to content

Commit

Permalink
Merge pull request #55 from timhuff/original
Browse files Browse the repository at this point in the history
support virtual properties through dedicated schema
  • Loading branch information
timhuff authored Jun 17, 2018
2 parents 6898d68 + ab21f7c commit fd2f42b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/SchemaBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class SchemaBuilder {
exclude: modelData.opt.exclude,
typeNamePrefix: utils.typeNameForModel(modelData.modelClass),
typeCache: this.typeCache,
virtualJsonSchemaProperties: modelData.modelClass.virtualJsonSchemaProperties
});

modelData.args = this._argsForModel(modelData);
Expand Down Expand Up @@ -421,17 +422,17 @@ class SchemaBuilder {
if (!this.enableSelectFiltering) {
return null;
}
const { jsonSchema, virtualJsonSchemaProperties: vProps } = modelClass;
const virtualPropertyFilter = (it) => !(vProps && vProps[it])

const relations = modelClass.getRelations();
const selects = this._collectSelects(astNode, relations, astRoot.fragments, []);
const selects = this._collectSelects(astNode, relations, astRoot.fragments, []).filter(virtualPropertyFilter);

if (selects.length === 0) {
return null;
}

return (builder) => {
const { jsonSchema } = modelClass;

builder.select(selects.map((it) => {
const col = modelClass.propertyNameToColumnName(it);

Expand Down
4 changes: 3 additions & 1 deletion lib/jsonSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ function jsonSchemaToGraphQLFields(jsonSchema, opt) {
typeIndex: 1,
typeNamePrefix: '',
typeCache: {},
virtualJsonSchemaProperties: {},
});

const fields = {};

_.forOwn(jsonSchema.properties, (propSchema, propName) => {
const augmentedJsonSchemaProperties = Object.assign({}, jsonSchema.properties, ctx.virtualJsonSchemaProperties);
_.forOwn(augmentedJsonSchemaProperties, (propSchema, propName) => {
if (utils.isExcluded(ctx, propName)) {
return;
}
Expand Down
53 changes: 53 additions & 0 deletions tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,59 @@ describe('integration tests', () => {
]);
}));

it('`people` field should have all properties defined in the Person model\'s jsonSchema, plus virtual properties', () => graphql(schema, '{ people { age, birthYear, gender, firstName, lastName, parentId, addresses { street, city, zipCode } } }').then((res) => {
console.log(res);
const { data: { people } } = res;
people.sort(sortByFirstName);

expect(people).to.eql([
{
age: 73,
birthYear: 1945,
firstName: 'Arnold',
lastName: 'Schwarzenegger',
gender: 'Male',
parentId: 1,
addresses: [{
street: 'Arnoldlane 12',
city: 'Arnoldova',
zipCode: '123456',
}],
},
{
age: 98,
birthYear: 1920,
firstName: 'Gustav',
lastName: 'Schwarzenegger',
gender: 'Male',
parentId: null,
addresses: [{
street: 'Gustavroad 64',
city: 'Gustavia',
zipCode: '654321',
}],
},
{
age: 45,
birthYear: 1973,
firstName: 'Michael',
lastName: 'Biehn',
gender: 'Male',
parentId: null,
addresses: null,
},
{
age: 20,
birthYear: 1998,
firstName: 'Some',
lastName: 'Random-Dudette',
gender: 'Female',
parentId: null,
addresses: null,
},
]);
}));

it('should work with the meta field `__typename`', () => graphql(schema, '{ reviews { title, __typename } }').then((res) => {
expect(res.data.reviews).to.eql([{
__typename: 'Review',
Expand Down
12 changes: 11 additions & 1 deletion tests/setup/models/Person.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Person extends Model {
};
}

static get virtualJsonSchemaProperties() {
return {
birthYear: { type: ['number', 'null'] },
};
}

static get jsonSchema() {
return {
type: 'object',
Expand Down Expand Up @@ -90,7 +96,11 @@ class Person extends Model {
}

static get virtualAttributes() {
return ['fullName'];
return ['fullName', 'birthYear'];
}

birthYear() {
return this.age ? (2018 - this.age) : null;
}

fullName() {
Expand Down

0 comments on commit fd2f42b

Please sign in to comment.