Skip to content
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
14 changes: 12 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"no-self-compare": 0,
"no-sequences": 2,
"no-throw-literal": 2,
"no-unused-expressions": 2,
"no-unused-expressions": 0,
"no-void": 2,
"no-warning-comments": 0,
"no-with": 2,
Expand Down Expand Up @@ -246,5 +246,15 @@
"wrap-regex": 0,
"no-var": 0,
"max-len": [2, 130, 4]
},
"globals": {
"it": false,
"afterEach": false,
"beforeEach": false,
"before": false,
"after": false,
"describe": false,
"expect": false,
"assert": false
}
}
}
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# graphql-sequelize

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

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

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})`.

#### Default attributes
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']})`.

### Features

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

[Relay documentation](docs/relay.md)

### Examples
### Examples

```js
import {resolver} from 'graphql-sequelize';
Expand Down Expand Up @@ -236,7 +239,7 @@ attributeFields(Model);
### Renaming generated fields

attributeFields accepts a ```map``` option to customize the way the attribute fields are named. The ```map``` option accepts
an object or a function that returns a string.
an object or a function that returns a string.

```js

Expand Down Expand Up @@ -336,12 +339,12 @@ fullName: {

### defaultArgs

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

```js
var Model = sequelize.define('User', {

});

defaultArgs(Model);
Expand Down Expand Up @@ -387,7 +390,7 @@ defaultArgs(Model);
type: GraphQLString
},
where: {
type JSONType
type JSONType
}
}
```
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"dependencies": {
"babel-runtime": "^6.0.8",
"graphql-relay": "^0.3.6",
"invariant": "2.2.1",
"lodash": "^4.0.0"
}
}
15 changes: 15 additions & 0 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import simplifyAST from './simplifyAST';
import generateIncludes from './generateIncludes';
import argsToFindOptions from './argsToFindOptions';
import { isConnection, handleConnection, nodeAST, nodeType } from './relay';
import invariant from 'invariant';

function inList(list, attribute) {
return ~list.indexOf(attribute);
}

function validateOptions(options) {
invariant(
!options.defaultAttributes || Array.isArray(options.defaultAttributes),
'options.defaultAttributes must be an array of field names.'
);
}

function resolverFactory(target, options) {
var resolver
, targetAttributes
Expand All @@ -26,6 +34,8 @@ function resolverFactory(target, options) {
if (options.handleConnection === undefined) options.handleConnection = true;
if (options.filterAttributes === undefined) options.filterAttributes = resolverFactory.filterAttributes;

validateOptions(options);

resolver = function (source, args, info) {
var root = info.rootValue || {}
, ast = info.fieldASTs
Expand Down Expand Up @@ -61,6 +71,11 @@ function resolverFactory(target, options) {
findOptions.attributes = Object.keys(fields)
.map(key => fields[key].key || key)
.filter(inList.bind(null, targetAttributes));

if (options.defaultAttributes) {
findOptions.attributes = findOptions.attributes.concat(options.defaultAttributes);
}

} else {
findOptions.attributes = targetAttributes;
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/relay/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ if (helper.sequelize.dialect.name === 'postgres') {
edges {
...projectOwner
node {
id
id
}
}
}
Expand Down Expand Up @@ -877,4 +877,4 @@ if (helper.sequelize.dialect.name === 'postgres') {
});
});
});
}
}
Loading