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

support virtual properties through dedicated schema #55

Merged
merged 1 commit into from
Jun 17, 2018
Merged
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
7 changes: 4 additions & 3 deletions lib/SchemaBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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 @@ -416,17 +417,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