Skip to content

Commit dd54576

Browse files
authored
remove datalodaer (#567)
* remove datalodaer * Fix typo
1 parent 081d2a9 commit dd54576

File tree

8 files changed

+72
-74
lines changed

8 files changed

+72
-74
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
[![Slack](http://sequelize-slack.herokuapp.com/badge.svg)](http://sequelize-slack.herokuapp.com)
66
[![Coverage](https://codecov.io/gh/mickhansen/graphql-sequelize/branch/master/graph/badge.svg)](https://codecov.io/gh/mickhansen/graphql-sequelize)
77

8+
Should be used with [dataloader-sequelize](https://github.com/mickhansen/dataloader-sequelize) to avoid N+1 queries
9+
810
- [Installation](#installation)
911
- [Resolve helpers](#resolve-helpers)
1012
- [field helpers](#field-helpers)
@@ -32,8 +34,6 @@ Please take a look at [the tests](https://github.com/mickhansen/graphql-sequeliz
3234
- Automatically converts args to where if arg keys matches model attributes
3335
- Automatically converts an arg named 'limit' to a sequelize limit
3436
- Automatically converts an arg named 'order' to a sequelize order
35-
- Only loads the attributes defined in the query (automatically adds primary key and foreign keys)
36-
- Batching of nested associations (see [dataloader-sequelize](https://github.com/mickhansen/dataloader-sequelize))
3737

3838
### Relay & Connections
3939

@@ -78,7 +78,18 @@ resolver(SequelizeModel, {
7878
result.sort(/* Custom sort function */);
7979
return result;
8080
},
81+
82+
/*
83+
* Transfer fields from the graphql context to the options passed to model calls
84+
* Inherits from global resolver.contextToOptions
85+
*/
86+
contextToOptions: {
87+
a: 'a',
88+
b: 'c'
89+
}
8190
});
91+
92+
resolver.contextToOptions = {}; /* Set contextToOptions globally */
8293
```
8394

8495
_The `args` and `context` parameters are provided by GraphQL. More information

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
},
4242
"homepage": "https://github.com/mickhansen/graphql-sequelize",
4343
"dependencies": {
44-
"dataloader-sequelize": "^1.5.4",
45-
"invariant": "2.2.1",
4644
"bluebird": "^3.4.0",
45+
"invariant": "2.2.1",
4746
"lodash": "^4.0.0"
4847
},
4948
"peerDependencies": {

src/resolver.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import argsToFindOptions from './argsToFindOptions';
44
import { isConnection, handleConnection, nodeType } from './relay';
55
import invariant from 'assert';
66
import Promise from 'bluebird';
7-
import dataLoaderSequelize from 'dataloader-sequelize';
87

98
function whereQueryVarsToValues(o, vals) {
109
_.forEach(o, (v, k) => {
@@ -17,16 +16,13 @@ function whereQueryVarsToValues(o, vals) {
1716
}
1817

1918
function resolverFactory(target, options = {}) {
20-
if (options.dataLoader !== false) {
21-
dataLoaderSequelize(target);
22-
}
23-
2419
var resolver
2520
, targetAttributes
2621
, isModel = !!target.getTableName
2722
, isAssociation = !!target.associationType
2823
, association = isAssociation && target
29-
, model = isAssociation && target.target || isModel && target;
24+
, model = isAssociation && target.target || isModel && target
25+
, contextToOptions = _.assign({}, resolverFactory.contextToOptions, options.contextToOptions);
3026

3127
targetAttributes = Object.keys(model.rawAttributes);
3228

@@ -58,6 +54,10 @@ function resolverFactory(target, options = {}) {
5854
findOptions.logging = findOptions.logging || context.logging;
5955
findOptions.graphqlContext = context;
6056

57+
_.each(contextToOptions, (as, key) => {
58+
findOptions[as] = context[key];
59+
});
60+
6161
return Promise.resolve(options.before(findOptions, args, context, info)).then(function (findOptions) {
6262
if (args.where && !_.isEmpty(info.variableValues)) {
6363
whereQueryVarsToValues(args.where, info.variableValues);
@@ -96,4 +96,6 @@ function resolverFactory(target, options = {}) {
9696
return resolver;
9797
}
9898

99+
resolverFactory.contextToOptions = {};
100+
99101
module.exports = resolverFactory;

test/integration/relay.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,7 @@ describe('relay', function () {
567567
}
568568
}
569569
}
570-
`, null, {
571-
logging: sqlSpy
572-
}).then(result => {
570+
`, null).then(result => {
573571
if (result.errors) throw new Error(result.errors[0].stack);
574572

575573
expect(result.data.project.users.edges).to.have.length(2);
@@ -584,8 +582,6 @@ describe('relay', function () {
584582
expect(userB).to.have.property('tasks');
585583
expect(userB.tasks.edges).to.have.length.above(0);
586584
expect(userB.tasks.edges[0].node.name).to.be.ok;
587-
588-
expect(sqlSpy).to.have.been.calledThrice;
589585
});
590586
});
591587

test/integration/relay/connection.test.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,14 +1034,10 @@ describe('relay', function () {
10341034
}
10351035
}
10361036
}
1037-
`, null, {
1038-
logging: sqlSpy
1039-
});
1037+
`, null);
10401038

10411039
if (result.errors) throw new Error(result.errors[0].stack);
10421040

1043-
expect(sqlSpy.callCount).to.equal(3);
1044-
10451041
const nodeNames = result.data.user.projects.edges.map(edge => {
10461042
return edge.node.tasks.edges.map(edge => {
10471043
return edge.node.name;
@@ -1093,9 +1089,7 @@ describe('relay', function () {
10931089
}
10941090
}
10951091
}
1096-
`, null, {
1097-
logging: sqlSpy
1098-
});
1092+
`, null);
10991093

11001094
if (result.errors) throw new Error(result.errors[0].stack);
11011095

@@ -1108,13 +1102,9 @@ describe('relay', function () {
11081102

11091103
expect(projects[0].tasks.edges[0].node.id).to.equal(toGlobalId(this.Task.name, this.userA.tasks[4].get('id')));
11101104
expect(projects[1].tasks.edges[0].node.id).to.equal(toGlobalId(this.Task.name, this.userA.tasks[8].get('id')));
1111-
1112-
expect(sqlSpy.callCount).to.equal(3);
11131105
});
11141106

11151107
it('should support connection fields', async function () {
1116-
let sqlSpy = sinon.spy();
1117-
11181108
let result = await graphql(this.schema, `
11191109
{
11201110
user(id: ${this.userA.id}) {
@@ -1123,9 +1113,7 @@ describe('relay', function () {
11231113
}
11241114
}
11251115
}
1126-
`, null, {
1127-
logging: sqlSpy
1128-
});
1116+
`, null, {});
11291117

11301118
if (result.errors) throw new Error(result.errors[0].stack);
11311119

@@ -1134,8 +1122,6 @@ describe('relay', function () {
11341122
});
11351123

11361124
it('should support connection fields on nested connections', async function () {
1137-
let sqlSpy = sinon.spy();
1138-
11391125
let result = await graphql(this.schema, `
11401126
{
11411127
user(id: ${this.userA.id}) {
@@ -1150,9 +1136,7 @@ describe('relay', function () {
11501136
}
11511137
}
11521138
}
1153-
`, null, {
1154-
logging: sqlSpy
1155-
});
1139+
`, null, {});
11561140

11571141
if (result.errors) throw new Error(result.errors[0].stack);
11581142

@@ -1181,9 +1165,7 @@ describe('relay', function () {
11811165
fragment projectOwner on userProjectEdge {
11821166
isOwner
11831167
}
1184-
`, null, {
1185-
logging: sqlSpy
1186-
});
1168+
`, null);
11871169

11881170
if (result.errors) throw new Error(result.errors[0].stack);
11891171

@@ -1202,9 +1184,7 @@ describe('relay', function () {
12021184
}
12031185
}
12041186
}
1205-
`, null, {
1206-
logging: sqlSpy
1207-
});
1187+
`, null, {});
12081188

12091189
if (result.errors) throw new Error(result.errors[0].stack);
12101190

test/integration/resolver.test.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ describe('resolver', function () {
210210
type: new GraphQLNonNull(GraphQLInt)
211211
}
212212
},
213-
resolve: resolver(User)
213+
resolve: resolver(User, {
214+
contextToOptions: {
215+
a: 'a',
216+
b: 'c'
217+
}
218+
})
214219
},
215220
users: {
216221
type: new GraphQLList(userType),
@@ -325,6 +330,9 @@ describe('resolver', function () {
325330
});
326331
});
327332

333+
beforeEach(function () {
334+
this.sandbox.spy(User, 'findOne');
335+
});
328336
afterEach(function () {
329337
this.sandbox.restore();
330338
})
@@ -351,6 +359,31 @@ describe('resolver', function () {
351359
});
352360
});
353361

362+
it('should map context to find options', function () {
363+
var user = this.userB;
364+
365+
return graphql(schema, `
366+
{
367+
user(id: ${user.id}) {
368+
name
369+
myVirtual
370+
}
371+
}
372+
`, null, {a: 1, b: 2}).then(function (result) {
373+
if (result.errors) throw new Error(result.errors[0].stack);
374+
375+
expect(result.data).to.deep.equal({
376+
user: {
377+
name: user.name,
378+
myVirtual: 'lol'
379+
}
380+
});
381+
382+
expect(User.findOne.firstCall.args[0].a).to.equal(1);
383+
expect(User.findOne.firstCall.args[0].c).to.equal(2);
384+
});
385+
});
386+
354387
it('should resolve a plain result with an aliased field', function () {
355388
var user = this.userB;
356389

@@ -641,17 +674,13 @@ describe('resolver', function () {
641674
}
642675
}
643676
}
644-
`, null, {
645-
logging: spy
646-
}).then(function (result) {
677+
`, null).then(function (result) {
647678
if (result.errors) throw new Error(result.errors[0].stack);
648679

649680
expect(result.data.users).to.have.length(users.length);
650681
result.data.users.forEach(function (user) {
651682
expect(user.tasks).to.have.length.above(0);
652683
});
653-
654-
expect(spy).to.have.been.calledTwice;
655684
});
656685
});
657686

@@ -744,9 +773,7 @@ describe('resolver', function () {
744773
}
745774
}
746775
}
747-
`, null, {
748-
logging: spy
749-
}).then(function (result) {
776+
`, null).then(function (result) {
750777
if (result.errors) throw new Error(result.errors[0].stack);
751778

752779
expect(result.data.users).to.have.length(users.length);
@@ -757,8 +784,6 @@ describe('resolver', function () {
757784
expect(task.id).to.be.ok;
758785
});
759786
});
760-
761-
expect(spy).to.have.been.calledTwice;
762787
});
763788
});
764789

@@ -892,9 +917,7 @@ describe('resolver', function () {
892917
}
893918
}
894919
}
895-
`, null, {
896-
logging: sqlSpy
897-
}).then(function (result) {
920+
`, null).then(function (result) {
898921
if (result.errors) throw new Error(result.errors[0].stack);
899922

900923
expect(result.data.users.length).to.equal(users.length);
@@ -904,8 +927,6 @@ describe('resolver', function () {
904927
expect(task.project.name).to.be.ok;
905928
});
906929
});
907-
908-
expect(sqlSpy.callCount).to.equal(3);
909930
});
910931
});
911932

@@ -924,9 +945,7 @@ describe('resolver', function () {
924945
}
925946
}
926947
}
927-
`, null, {
928-
logging: sqlSpy
929-
}).then(function (result) {
948+
`, null).then(function (result) {
930949
if (result.errors) throw new Error(result.errors[0].stack);
931950

932951
expect(result.data.users.length).to.equal(users.length);
@@ -936,8 +955,6 @@ describe('resolver', function () {
936955
expect(task.project.name).to.be.ok;
937956
});
938957
});
939-
940-
expect(sqlSpy.callCount).to.equal(3);
941958
});
942959
});
943960

@@ -960,9 +977,7 @@ describe('resolver', function () {
960977
}
961978
}
962979
}
963-
`, null, {
964-
logging: sqlSpy
965-
}).then(function (result) {
980+
`, null).then(function (result) {
966981
if (result.errors) throw new Error(result.errors[0].stack);
967982

968983
expect(result.data.users.length).to.equal(users.length);
@@ -977,8 +992,6 @@ describe('resolver', function () {
977992
});
978993
});
979994
});
980-
981-
expect(sqlSpy.callCount).to.equal(4);
982995
});
983996
});
984997

test/support/helper.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import { resetCache } from 'dataloader-sequelize';
21
import Sequelize from 'sequelize';
32

43
export const Promise = Sequelize.Promise;
54
export const sequelize = createSequelize();
65

7-
if (typeof afterEach !== 'undefined') {
8-
afterEach(resetCache);
9-
}
10-
116
export function createSequelize(options = {}) {
127
const env = process.env;
138
const dialect = env.DIALECT || 'sqlite';

test/unit/relay/connection.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ describe('relay', function () {
7878
id: Math.ceil(Math.random() * 999)
7979
});
8080

81-
this.sinon.stub(this.Task, 'findAll').resolves([this.Task.build()]);
81+
const task = this.Task.build();
82+
task.dataValues.full_count = Math.random() * 999;
83+
this.sinon.stub(this.Task, 'findAll').resolves([task]);
8284
this.sinon.stub(this.User, 'findById').resolves(this.User.build());
8385
});
8486

@@ -131,7 +133,7 @@ describe('relay', function () {
131133
}
132134
}),
133135
sinon.match({
134-
ast: sinon.match.any
136+
path: sinon.match.any
135137
})
136138
);
137139

0 commit comments

Comments
 (0)