Skip to content

Commit f997e28

Browse files
committed
Merge pull request #184 from jonathanewerner/pr/default-attributes
Enhancement: Resolver option `defaultAttributes`
2 parents 497b6cd + c6b050b commit f997e28

File tree

6 files changed

+164
-70
lines changed

6 files changed

+164
-70
lines changed

.eslintrc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"no-self-compare": 0,
8989
"no-sequences": 2,
9090
"no-throw-literal": 2,
91-
"no-unused-expressions": 2,
91+
"no-unused-expressions": 0,
9292
"no-void": 2,
9393
"no-warning-comments": 0,
9494
"no-with": 2,
@@ -246,5 +246,15 @@
246246
"wrap-regex": 0,
247247
"no-var": 0,
248248
"max-len": [2, 130, 4]
249+
},
250+
"globals": {
251+
"it": false,
252+
"afterEach": false,
253+
"beforeEach": false,
254+
"before": false,
255+
"after": false,
256+
"describe": false,
257+
"expect": false,
258+
"assert": false
249259
}
250-
}
260+
}

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# graphql-sequelize
22

33
[![NPM](https://img.shields.io/npm/v/graphql-sequelize.svg)](https://www.npmjs.com/package/graphql-sequelize)
4-
[![Build Status](https://travis-ci.org/mickhansen/graphql-sequelize.svg?branch=master)](https://travis-ci.org/mickhansen/graphql-sequelize)
4+
[![Build Status](https://travis-ci.org/mickhansen/graphql-sequelize.svg?branch=master)](https://travis-ci.org/mickhansen/graphql-sequelize)
55
[![Slack Status](http://sequelize-slack.herokuapp.com/badge.svg)](http://sequelize-slack.herokuapp.com)
66

77
- [Installation](#installation)
@@ -26,6 +26,9 @@ The graphql-sequelize resolver will by default only select those attributes requ
2626

2727
If you have non-database values that depend on other values you can either solve this by using virtual attributes with dependencies on your model or disable attribute filtering via `resolver.filterAttributes = false` or for specific resolvers with `resolver(target, {filterAttributes: false})`.
2828

29+
#### Default attributes
30+
When filtering attributes you might need some fields every time, regardless of wether they have been requested in the query. You can specify those fields with the `defaultAttributes` resolver option like `resolver(target, {defaultAttributes: ['default', 'field', 'names']})`. A common use case would be the need to fetch a `userId` for every query and every resource of your domain model for permission checking -- in that case you would write your `resolver` functions like `resolver(target, {defaultAttributes: ['userId']})`.
31+
2932
### Features
3033

3134
- Automatically converts args to where if arg keys matches model attributes
@@ -38,7 +41,7 @@ If you have non-database values that depend on other values you can either solve
3841

3942
[Relay documentation](docs/relay.md)
4043

41-
### Examples
44+
### Examples
4245

4346
```js
4447
import {resolver} from 'graphql-sequelize';
@@ -236,7 +239,7 @@ attributeFields(Model);
236239
### Renaming generated fields
237240

238241
attributeFields accepts a ```map``` option to customize the way the attribute fields are named. The ```map``` option accepts
239-
an object or a function that returns a string.
242+
an object or a function that returns a string.
240243

241244
```js
242245

@@ -336,12 +339,12 @@ fullName: {
336339

337340
### defaultArgs
338341

339-
`defaultArgs(Model)` will return an object containing an arg with a key and type matching your models primary key and
342+
`defaultArgs(Model)` will return an object containing an arg with a key and type matching your models primary key and
340343
the "where" argument for passing complex query operations described [here](http://docs.sequelizejs.com/en/latest/docs/querying/)
341344

342345
```js
343346
var Model = sequelize.define('User', {
344-
347+
345348
});
346349

347350
defaultArgs(Model);
@@ -387,7 +390,7 @@ defaultArgs(Model);
387390
type: GraphQLString
388391
},
389392
where: {
390-
type JSONType
393+
type JSONType
391394
}
392395
}
393396
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"dependencies": {
5555
"babel-runtime": "^6.0.8",
5656
"graphql-relay": "^0.3.6",
57+
"invariant": "2.2.1",
5758
"lodash": "^4.0.0"
5859
}
5960
}

src/resolver.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ import simplifyAST from './simplifyAST';
44
import generateIncludes from './generateIncludes';
55
import argsToFindOptions from './argsToFindOptions';
66
import { isConnection, handleConnection, nodeAST, nodeType } from './relay';
7+
import invariant from 'invariant';
78

89
function inList(list, attribute) {
910
return ~list.indexOf(attribute);
1011
}
1112

13+
function validateOptions(options) {
14+
invariant(
15+
!options.defaultAttributes || Array.isArray(options.defaultAttributes),
16+
'options.defaultAttributes must be an array of field names.'
17+
);
18+
}
19+
1220
function resolverFactory(target, options) {
1321
var resolver
1422
, targetAttributes
@@ -26,6 +34,8 @@ function resolverFactory(target, options) {
2634
if (options.handleConnection === undefined) options.handleConnection = true;
2735
if (options.filterAttributes === undefined) options.filterAttributes = resolverFactory.filterAttributes;
2836

37+
validateOptions(options);
38+
2939
resolver = function (source, args, info) {
3040
var root = info.rootValue || {}
3141
, ast = info.fieldASTs
@@ -61,6 +71,11 @@ function resolverFactory(target, options) {
6171
findOptions.attributes = Object.keys(fields)
6272
.map(key => fields[key].key || key)
6373
.filter(inList.bind(null, targetAttributes));
74+
75+
if (options.defaultAttributes) {
76+
findOptions.attributes = findOptions.attributes.concat(options.defaultAttributes);
77+
}
78+
6479
} else {
6580
findOptions.attributes = targetAttributes;
6681
}

test/integration/relay/connection.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ if (helper.sequelize.dialect.name === 'postgres') {
765765
edges {
766766
...projectOwner
767767
node {
768-
id
768+
id
769769
}
770770
}
771771
}
@@ -877,4 +877,4 @@ if (helper.sequelize.dialect.name === 'postgres') {
877877
});
878878
});
879879
});
880-
}
880+
}

0 commit comments

Comments
 (0)