Skip to content

Commit

Permalink
feat(model): description and visibility can be specified in the mongo…
Browse files Browse the repository at this point in the history
…ose schema
  • Loading branch information
tothandras committed Oct 9, 2015
1 parent c8801db commit aa81f33
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ import mongoose from 'mongoose';

var UserSchema = new mongoose.Schema({
name: {
type: String
type: String,
// field description
description: 'the full name of the user'
},
hiddenField: {
type: Date,
default: Date.now,
// the field is hidden, not available in GraphQL
hidden: true
},
age: {
type: Number,
Expand Down
2 changes: 1 addition & 1 deletion example/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const UserSchema = new mongoose.Schema({

body: {
eye: String,
hair: Number,
hair: Number
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ function getField(schemaPath) {
const field = {
name: name,
type: schemaPath.instance,
nonNull: options.index ? true : false
nonNull: options.index ? true : false,
description: options.description,
hidden: options.hidden
};

// Field options
Expand Down
2 changes: 1 addition & 1 deletion src/model/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('model', () => {
name: 'User'
});

expect(treeChunk).to.be.eql({
expect(treeChunk).to.containSubset({
foo: {
name: 'foo',
nonNull: false,
Expand Down
20 changes: 11 additions & 9 deletions src/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,24 @@ 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, type, subtype, reference, nonNull, fields: subfields}, key) => {
const graphQLTypeFields = reduce(fields, (graphQLFields, {name, description, type, subtype, reference, nonNull, hidden, fields: subfields}, key) => {
if (hidden) {
return graphQLFields;
}

name = name || key;
const graphQLField = {name, description};

if (type === 'Array') {
graphQLField.type = new GraphQLList(stringToGraphQLType(subtype));
if (reference) {
const typeName = reference;
resolveReference[graphQLType.name][name] = {
name: name,
type: typeName,
type: reference,
args: connectionArgs,
resolve: (rootValue, args, info) => {
args.id = rootValue[name].map((i) => i.toString());
return connectionFromModel(graffitiModels[typeName], args, info);
return connectionFromModel(graffitiModels[reference], args, info);
}
};
}
Expand All @@ -90,12 +93,11 @@ export default function getType(graffitiModels, {name, description, fields}, roo
}

if (reference && (graphQLField.type === GraphQLID || graphQLField.type === new GraphQLNonNull(GraphQLID))) {
const typeName = reference;
resolveReference[graphQLType.name][name] = {
name: name,
type: typeName,
type: reference,
resolve: (rootValue, args, info) => {
const resolver = getOneResolver(graffitiModels[typeName]);
const resolver = getOneResolver(graffitiModels[reference]);
return resolver(rootValue, {id: rootValue[name].toString()}, info);
}
};
Expand All @@ -105,8 +107,8 @@ export default function getType(graffitiModels, {name, description, fields}, roo
graphQLField.type = new GraphQLNonNull(graphQLField.type);
}

result[name] = graphQLField;
return result;
graphQLFields[name] = graphQLField;
return graphQLFields;
}, {});

if (root) {
Expand Down
16 changes: 14 additions & 2 deletions src/type/type.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('type', () => {
},
mother: {
type: 'ObjectID',
reference: 'User'
reference: 'User',
description: 'The user\'s mother'
},
friends: {
type: 'Array',
Expand All @@ -52,6 +53,10 @@ describe('type', () => {
unknownType: {
type: 'Unknown'
},
hidden: {
type: 'String',
hidden: true
},
sub: {
type: 'Object',
fields: {
Expand Down Expand Up @@ -103,7 +108,8 @@ describe('type', () => {
},
mother: {
name: 'mother',
type: GraphQLID
type: GraphQLID,
description: 'The user\'s mother'
},
friends: {
name: 'friends',
Expand Down Expand Up @@ -159,6 +165,12 @@ describe('type', () => {
}
});
});

it('should not include hidden fields', () => {
const result = getType([], user);
const fields = result._typeConfig.fields();
expect(fields.hidden).to.be.eql(undefined);
});
});

describe('getTypes', () => {
Expand Down

0 comments on commit aa81f33

Please sign in to comment.