Skip to content

Commit

Permalink
refactor(model): change graffiti-model format
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Oct 9, 2015
1 parent 26ddd10 commit e5b005e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 132 deletions.
4 changes: 4 additions & 0 deletions src/field/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function getField(graffitiModel, type) {
[pluralName]: {
type: new GraphQLList(type),
args: reduce(type._typeConfig.fields(), (args, field) => {
if (field.type instanceof GraphQLNonNull && field.name !== 'id') {
field.type = field.type.ofType;
}

if (field.type instanceof GraphQLScalarType) {
args[field.name] = field;
}
Expand Down
27 changes: 9 additions & 18 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@ function getField(schemaPath) {

const field = {
name: name,
path: schemaPath.path,
instance: schemaPath.instance,
indexed: options.index ? true : false
type: schemaPath.instance,
nonNull: options.index ? true : false
};

// Field options
if (schemaPath.options) {
// ObjectID ref
if (schemaPath.options.ref) {
field.ref = schemaPath.options.ref;
field.reference = schemaPath.options.ref;
}
}

// Caster
if (schemaPath.caster) {
field.caster = {
path: schemaPath.caster.path,
instance: schemaPath.caster.instance
};
field.subtype = schemaPath.caster.instance;

// Caster options
if (schemaPath.caster.options) {
// ObjectID ref
if (schemaPath.caster.options.ref) {
field.caster.ref = schemaPath.caster.options.ref;
field.reference = schemaPath.caster.options.ref;
}
}
}
Expand All @@ -50,26 +46,21 @@ function getField(schemaPath) {
* @param {Object} model
* @return {Object} field
*/
function extractPath(schemaPath, model) {
function extractPath(schemaPath) {
const subs = schemaPath.path.split('.');
const subNames = schemaPath.path.split('.');

return reduceRight(subs, (field, sub, key) => {
const path = subNames.join('.');
const obj = {};

if (key === (subs.length - 1)) {
obj[sub] = getField(schemaPath);
} else {
obj[sub] = {
name: sub,
path: path,
fullPath: `${model.name}.${path}`,
indexed: false,
instance: 'Object',
caster: {
fields: field
}
nonNull: false,
type: 'Object',
fields: field
};
}

Expand Down
102 changes: 38 additions & 64 deletions src/model/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,18 @@ describe('model', () => {
expect(treeChunk).to.be.eql({
foo: {
name: 'foo',
path: 'foo',
fullPath: 'User.foo',
indexed: false,
instance: 'Object',
caster: {
fields: {
bar: {
name: 'bar',
path: 'foo.bar',
fullPath: 'User.foo.bar',
indexed: false,
instance: 'Object',
caster: {
fields: {
so: {
name: 'so',
path: 'foo.bar.so',
instance: 'String',
indexed: false
}
}
nonNull: false,
type: 'Object',
fields: {
bar: {
name: 'bar',
nonNull: false,
type: 'Object',
fields: {
so: {
name: 'so',
type: 'String',
nonNull: false
}
}
}
Expand Down Expand Up @@ -66,59 +57,42 @@ describe('model', () => {
expect(tree).to.containSubset({
foo: {
name: 'foo',
path: 'foo',
fullPath: 'User.foo',
indexed: false,
instance: 'Object',
caster: {
fields: {
bar: {
name: 'bar',
path: 'foo.bar',
fullPath: 'User.foo.bar',
indexed: false,
instance: 'Object',
caster: {
fields: {
so: {
name: 'so',
path: 'foo.bar.so',
indexed: false
},
very: {
name: 'very',
path: 'foo.bar.very',
indexed: false
}
}
nonNull: false,
type: 'Object',
fields: {
bar: {
name: 'bar',
nonNull: false,
type: 'Object',
fields: {
so: {
name: 'so',
nonNull: false
},
very: {
name: 'very',
nonNull: false
}
},
grr: {
name: 'grr',
path: 'foo.grr',
indexed: false
}
},
grr: {
name: 'grr',
nonNull: false
}
}
},
simple: {
name: 'simple',
path: 'simple',
indexed: false
nonNull: false
},
sub: {
name: 'sub',
path: 'sub',
fullPath: 'User.sub',
indexed: false,
instance: 'Object',
caster: {
fields: {
sub: {
name: 'sub',
path: 'sub.sub',
indexed: false
}
nonNull: false,
type: 'Object',
fields: {
sub: {
name: 'sub',
nonNull: false
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/query/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ function getOne(collection, args, info) {
}

function getList(collection, selector, options = {}, info = null) {
if (selector && Array.isArray(selector.id)) {
if (selector && (Array.isArray(selector.id) || Array.isArray(selector._id))) {
const {id, _id = id} = selector;
delete selector._id;
delete selector.id;
selector._id = {
$in: selector.id
$in: _id.map((id) => processId({id}))
};
delete selector.id;
}

const projection = getFieldList(info);
Expand Down
24 changes: 14 additions & 10 deletions src/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export default function getType(graffitiModels, {name, description, fields}, roo

// these references has to be resolved when all type definitions are avaiable
resolveReference[graphQLType.name] = resolveReference[graphQLType.name] || {};
const graphQLTypeFields = reduce(fields, (result, {name, description, instance, caster, ref}, key) => {
const graphQLTypeFields = reduce(fields, (result, {name, description, type, subtype, reference, nonNull, fields: subfields}, key) => {
name = name || key;
const graphQLField = {name, description};

if (instance === 'Array') {
graphQLField.type = new GraphQLList(stringToGraphQLType(caster.instance));
if (caster.ref) {
const typeName = caster.ref;
if (type === 'Array') {
graphQLField.type = new GraphQLList(stringToGraphQLType(subtype));
if (reference) {
const typeName = reference;
resolveReference[graphQLType.name][name] = {
name: name,
type: typeName,
Expand All @@ -82,15 +82,15 @@ export default function getType(graffitiModels, {name, description, fields}, roo
}
};
}
} else if (instance === 'Object') {
const fields = caster.fields;
} else if (type === 'Object') {
const fields = subfields;
graphQLField.type = getType(graffitiModels, {name, description, fields}, false);
} else {
graphQLField.type = stringToGraphQLType(instance);
graphQLField.type = stringToGraphQLType(type);
}

if (ref && (graphQLField.type === GraphQLID || graphQLField.type === new GraphQLNonNull(GraphQLID))) {
const typeName = ref;
if (reference && (graphQLField.type === GraphQLID || graphQLField.type === new GraphQLNonNull(GraphQLID))) {
const typeName = reference;
resolveReference[graphQLType.name][name] = {
name: name,
type: typeName,
Expand All @@ -101,6 +101,10 @@ export default function getType(graffitiModels, {name, description, fields}, roo
};
}

if (nonNull && graphQLField.type) {
graphQLField.type = new GraphQLNonNull(graphQLField.type);
}

result[name] = graphQLField;
return result;
}, {});
Expand Down
64 changes: 27 additions & 37 deletions src/type/type.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,51 @@ describe('type', () => {
name: 'User',
fields: {
name: {
instance: 'String'
type: 'String'
},
age: {
instance: 'Number'
type: 'Number'
},
mother: {
instance: 'ObjectID',
ref: 'User'
type: 'ObjectID',
reference: 'User'
},
friends: {
instance: 'Array',
caster: {
instance: 'ObjectID',
ref: 'User'
}
type: 'Array',
subtype: 'ObjectID',
reference: 'User'
},
weight: {
instance: 'Number'
type: 'Number'
},
createdAt: {
instance: 'Date'
type: 'Date'
},
removed: {
instance: 'Boolean'
type: 'Boolean'
},
nums: {
instance: 'Array',
caster: {
instance: 'Number'
}
type: 'Array',
subtype: 'Number'
},
unknownType: {
instance: 'Unknown'
type: 'Unknown'
},
sub: {
instance: 'Object',
caster: {
fields: {
foo: {
instance: 'String'
},
nums: {
instance: 'Array',
caster: {
instance: 'Number'
}
},
subsub: {
instance: 'Object',
caster: {
fields: {
bar: {
instance: 'Number'
}
}
type: 'Object',
fields: {
foo: {
type: 'String'
},
nums: {
type: 'Array',
subtype: 'Number'
},
subsub: {
type: 'Object',
fields: {
bar: {
type: 'Number'
}
}
}
Expand Down

0 comments on commit e5b005e

Please sign in to comment.