Skip to content

Commit ffc8533

Browse files
committed
added: attributeFields map and globalId options, closes #41
1 parent bc9683f commit ffc8533

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ attributeFields(Model, {
148148
// ... options
149149
exclude: [], // array of model attributes to ignore - default: []
150150
only: [], // only generate definitions for these model attributes - default: null
151+
globalId: true, // return an relay global id field - default: false
152+
map: {} // rename fields - default: {}
151153
});
152154

153155
/*

src/attributeFields.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as typeMapper from './typeMapper';
22
import { GraphQLNonNull, GraphQLEnumType } from 'graphql';
3+
import { globalIdField } from 'graphql-relay';
34

45
module.exports = function (Model, options) {
56
options = options || {};
@@ -11,11 +12,15 @@ module.exports = function (Model, options) {
1112
var attribute = Model.rawAttributes[key]
1213
, type = attribute.type;
1314

15+
if (options.map && options.map[key]) {
16+
key = options.map[key];
17+
}
18+
1419
memo[key] = {
1520
type: typeMapper.toGraphQL(type, Model.sequelize.constructor)
1621
};
1722

18-
if ( memo[key].type instanceof GraphQLEnumType ) {
23+
if (memo[key].type instanceof GraphQLEnumType ) {
1924
memo[key].type.name = `${Model.name}${key}EnumType`;
2025
}
2126

@@ -26,5 +31,9 @@ module.exports = function (Model, options) {
2631
return memo;
2732
}, {});
2833

34+
if (options.globalId) {
35+
result.id = globalIdField(Model.name);
36+
}
37+
2938
return result;
3039
};

test/attributeFields.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import {
1717
GraphQLList
1818
} from 'graphql';
1919

20+
import {
21+
toGlobalId
22+
} from 'graphql-relay';
23+
2024
describe('attributeFields', function () {
2125
var Model;
2226
var modelName = Math.random().toString();
@@ -99,6 +103,29 @@ describe('attributeFields', function () {
99103
expect(Object.keys(fields)).to.deep.equal(['id', 'email', 'list']);
100104
});
101105

106+
it('should be possible to rename fields with map', function () {
107+
var fields = attributeFields(Model, {
108+
map: {
109+
id: '_id'
110+
}
111+
});
112+
113+
expect(Object.keys(fields)).to.contain('_id');
114+
expect(Object.keys(fields)).not.to.contain('id');
115+
});
116+
117+
it('should be possible to automatically set a relay globalId', function () {
118+
var fields = attributeFields(Model, {
119+
globalId: true
120+
});
121+
122+
expect(fields.id.resolve).to.be.ok;
123+
expect(fields.id.type.ofType.name).to.equal('ID');
124+
expect(fields.id.resolve({
125+
id: 23
126+
})).to.equal(toGlobalId(Model.name, 23));
127+
});
128+
102129
it('should automatically name enum types', function () {
103130
var fields = attributeFields(Model);
104131

0 commit comments

Comments
 (0)