Skip to content

Commit ad63a70

Browse files
janmeiermickhansen
authored andcommitted
fix(sequelizeConnection.before): Remove root and pass info down (#232)
1 parent f4c17df commit ad63a70

File tree

3 files changed

+127
-3
lines changed

3 files changed

+127
-3
lines changed

src/relay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
244244
if (startIndex > 0) options.offset = startIndex + 1;
245245
}
246246
options.attributes = _.uniq(options.attributes);
247-
return before(options, args, root, context, info);
247+
return before(options, args, context, info);
248248
},
249-
after: function (values, args, root, {source}) {
249+
after: function (values, args, context, {source}) {
250250
var cursor = null;
251251

252252
if (args.after || args.before) {

test/integration/relay/connection.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ if (helper.sequelize.dialect.name === 'postgres') {
204204
ID: {value: [this.Task.primaryKeyAttribute, 'ASC']}
205205
}
206206
}),
207-
before: (options, args, context, {viewer}) => {
207+
before: (options, args, {viewer}) => {
208208
options.where = options.where || {};
209209
options.where.userId = viewer.get('id');
210210
return options;

test/unit/relay/connection.test.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import Sequelize from 'sequelize';
2+
import {expect} from 'chai';
3+
import sinon from 'sinon';
4+
import helper from '../helper';
5+
const {
6+
sequelize
7+
} = helper;
8+
import attributeFields from '../../../src/attributeFields';
9+
10+
import {
11+
GraphQLObjectType,
12+
GraphQLSchema,
13+
graphql
14+
} from 'graphql';
15+
16+
import {
17+
globalIdField
18+
} from 'graphql-relay';
19+
20+
import {
21+
sequelizeConnection
22+
} from '../../../src/relay';
23+
24+
25+
describe('relay', function () {
26+
describe('connections', function () {
27+
before(function () {
28+
this.User = sequelize.define('user', {}, {timestamps: false});
29+
this.Task = sequelize.define('task', {title: Sequelize.STRING}, {timestamps: false});
30+
31+
this.User.Tasks = this.User.hasMany(this.Task, {as: 'tasks', foreignKey: 'userId'});
32+
33+
this.taskType = new GraphQLObjectType({
34+
name: this.Task.name,
35+
fields: {
36+
...attributeFields(this.Task),
37+
id: globalIdField(this.Task.name)
38+
}
39+
});
40+
41+
this.spy = sinon.spy(options => options);
42+
43+
this.viewerTaskConnection = sequelizeConnection({
44+
name: 'Viewer' + this.Task.name,
45+
nodeType: this.taskType,
46+
target: this.User.Tasks,
47+
before: this.spy
48+
});
49+
50+
this.viewerType = new GraphQLObjectType({
51+
name: 'Viewer',
52+
fields: {
53+
tasks: {
54+
type: this.viewerTaskConnection.connectionType,
55+
args: this.viewerTaskConnection.connectionArgs,
56+
resolve: this.viewerTaskConnection.resolve
57+
}
58+
}
59+
});
60+
61+
this.schema = new GraphQLSchema({
62+
query: new GraphQLObjectType({
63+
name: 'RootQueryType',
64+
fields: {
65+
viewer: {
66+
type: this.viewerType,
67+
resolve: function (source, args, {viewer}) {
68+
return viewer;
69+
}
70+
}
71+
}
72+
})
73+
});
74+
});
75+
76+
beforeEach(function () {
77+
this.sinon = sinon.sandbox.create();
78+
79+
this.viewer = this.User.build({
80+
id: Math.ceil(Math.random() * 999)
81+
});
82+
83+
this.sinon.stub(this.Task, 'findAll').resolves([this.Task.build()]);
84+
this.sinon.stub(this.User, 'findById').resolves(this.User.build());
85+
});
86+
87+
afterEach(function () {
88+
this.sinon.restore();
89+
});
90+
91+
it('passes context, root and info to before', async function () {
92+
await graphql(this.schema, `
93+
query {
94+
viewer {
95+
tasks {
96+
edges {
97+
node {
98+
id
99+
}
100+
}
101+
}
102+
}
103+
}
104+
`, null, {
105+
viewer: this.viewer
106+
});
107+
108+
expect(this.spy).to.have.been.calledWithMatch(
109+
sinon.match.any,
110+
sinon.match({
111+
orderBy: sinon.match.any
112+
}),
113+
sinon.match({
114+
viewer: {
115+
id: this.viewer.id
116+
}
117+
}),
118+
sinon.match({
119+
ast: sinon.match.any
120+
})
121+
);
122+
});
123+
});
124+
});

0 commit comments

Comments
 (0)