Skip to content

Commit 3706b82

Browse files
committed
fix(Flowtype): Resolve Flow errors. Cover tests by Flow.
1 parent 5de304d commit 3706b82

12 files changed

+121
-54
lines changed

.flowconfig

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
[ignore]
2-
.*/__tests__/.*
3-
.*/__mocks__/.*
42
.*/coverage/.*
53
.*/resources/.*
64
<PROJECT_ROOT>/lib/.*
75
<PROJECT_ROOT>/dist/.*
8-
.*/node_modules/config-chain/test/broken.json
9-
.*/node_modules/npmconf/test/fixtures/package.json
6+
.*/node_modules/ajv.*
7+
.*/node_modules/acorn.*
8+
.*/node_modules/async.*
9+
.*/node_modules/babel.*
10+
.*/node_modules/bluebird.*
11+
.*/node_modules/caniuse-db.*
12+
.*/node_modules/config-chain.*
13+
.*/node_modules/conventional-changelog.*
14+
.*/node_modules/core-js.*
15+
.*/node_modules/cssstyle.*
16+
.*/node_modules/diff.*
17+
.*/node_modules/es5-ext.*
18+
.*/node_modules/escope.*
19+
.*/node_modules/escodegen.*
20+
.*/node_modules/eslint.*
21+
.*/node_modules/github.*
22+
.*/node_modules/fsevents.*
23+
.*/node_modules/jsdoctypeparser.*
24+
.*/node_modules/jsdom.*
25+
.*/node_modules/iconv.*
26+
.*/node_modules/istanbul.*
27+
.*/node_modules/handlebars.*
28+
.*/node_modules/markdown.*
29+
.*/node_modules/node-notifier.*
30+
.*/node_modules/npmconf.*
31+
.*/node_modules/prettier.*
32+
.*/node_modules/source-map.*
33+
.*/node_modules/travis.*
34+
.*/node_modules/uglify.*
35+
.*/node_modules/yargs.*
1036

1137
[include]
1238

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"homepage": "https://github.com/nodkz/graphql-compose-connection",
2525
"peerDependencies": {
26-
"graphql-compose": ">=1.20.2"
26+
"graphql-compose": ">=1.20.3"
2727
},
2828
"devDependencies": {
2929
"babel-cli": "^6.24.1",
@@ -42,7 +42,7 @@
4242
"eslint-plugin-prettier": "^2.1.1",
4343
"flow-bin": "^0.47.0",
4444
"graphql": "^0.10.1",
45-
"graphql-compose": "^1.20.2",
45+
"graphql-compose": "^1.20.3",
4646
"jest": "^20.0.4",
4747
"prettier": "^1.4.2",
4848
"rimraf": "^2.6.1",
@@ -64,7 +64,7 @@
6464
"lint": "eslint src test *.js",
6565
"test": "jest",
6666
"watch": "jest --watch",
67-
"flow": "./node_modules/.bin/flow",
67+
"flow": "./node_modules/.bin/flow stop && ./node_modules/.bin/flow",
6868
"all": "npm run test && npm run lint && npm run flow",
6969
"link": "yarn build && yarn link graphql-compose && yarn link",
7070
"unlink": "yarn unlink graphql-compose && yarn add graphql-compose",

src/__mocks__/userTypeComposer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import { TypeComposer, Resolver, graphql } from 'graphql-compose';
55

6+
import type { ConnectionSortMapOpts } from '../definition';
7+
68
const {
79
GraphQLString,
810
GraphQLObjectType,
@@ -171,7 +173,7 @@ export const countResolver = new Resolver({
171173
});
172174
userTypeComposer.setResolver('count', countResolver);
173175

174-
export const sortOptions = {
176+
export const sortOptions: ConnectionSortMapOpts = {
175177
ID_ASC: {
176178
value: { id: 1 },
177179
cursorFields: ['id'],

src/__tests__/composeWithConnection-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ describe('composeWithRelay', () => {
2121
});
2222

2323
it('should throw error if first arg is not TypeComposer', () => {
24+
// $FlowFixMe
2425
expect(() => composeWithConnection(123)).toThrowError('should provide TypeComposer instance');
2526
});
2627

2728
it('should throw error if options are empty', () => {
29+
// $FlowFixMe
2830
expect(() => composeWithConnection(userTypeComposer)).toThrowError(
2931
'should provide non-empty options'
3032
);
@@ -52,6 +54,7 @@ describe('composeWithRelay', () => {
5254
describe('check `connection` resolver props', () => {
5355
const rsv = userComposer.getResolver('connection');
5456
const type = rsv.getType();
57+
// $FlowFixMe
5558
const tc = new TypeComposer(type);
5659

5760
it('should exists', () => {
@@ -92,6 +95,8 @@ describe('composeWithRelay', () => {
9295
}
9396
}`;
9497
const result = await graphql.graphql(schema, query);
98+
99+
// $FlowFixMe
95100
expect(result.data.userConnection).toEqual({
96101
count: 15,
97102
pageInfo: {
@@ -145,6 +150,8 @@ describe('composeWithRelay', () => {
145150
}
146151
}`;
147152
const result = await graphql.graphql(schema, query);
153+
154+
// $FlowFixMe
148155
expect(result.data.userConnection).toEqual({
149156
count: 15,
150157
pageInfo: {
@@ -258,9 +265,11 @@ describe('composeWithRelay', () => {
258265
}
259266
}`;
260267
await graphql.graphql(schema, query);
268+
// $FlowFixMe
261269
expect(Object.keys(topResolveParams.countResolveParams)).toEqual(
262270
expect.arrayContaining(['source', 'args', 'context', 'info', 'projection'])
263271
);
272+
// $FlowFixMe
264273
expect(topResolveParams.countResolveParams.args).toEqual({
265274
filter: { age: 45 },
266275
});
@@ -289,9 +298,11 @@ describe('composeWithRelay', () => {
289298
}
290299
}`;
291300
await graphql.graphql(schema, query);
301+
// $FlowFixMe
292302
expect(Object.keys(topResolveParams.findManyResolveParams)).toEqual(
293303
expect.arrayContaining(['source', 'args', 'context', 'info', 'projection'])
294304
);
305+
// $FlowFixMe
295306
expect(topResolveParams.findManyResolveParams.args).toEqual({
296307
filter: { age: 45 },
297308
limit: 2,

src/__tests__/connectionResolver-test.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ describe('connectionResolver', () => {
2222
});
2323

2424
it('should throw error if first arg is not TypeComposer', () => {
25+
// $FlowFixMe
2526
expect(() => prepareConnectionResolver(123)).toThrowError(
2627
'should be instance of TypeComposer'
2728
);
2829
});
2930

3031
it('should throw error if opts.countResolverName are empty', () => {
32+
// $FlowFixMe
3133
expect(() => prepareConnectionResolver(userTypeComposer, {})).toThrowError(
3234
'should have option `opts.countResolverName`'
3335
);
@@ -45,6 +47,7 @@ describe('connectionResolver', () => {
4547

4648
it('should throw error if opts.findResolverName are empty', () => {
4749
expect(() =>
50+
// $FlowFixMe
4851
prepareConnectionResolver(userTypeComposer, {
4952
countResolverName: 'count',
5053
})
@@ -72,28 +75,34 @@ describe('connectionResolver', () => {
7275
});
7376

7477
it('should have type to be ConnectionType', () => {
78+
// $FlowFixMe
7579
expect(connectionResolver.type.name).toBe('UserConnection');
7680
});
7781
});
7882

7983
describe('resolver args', () => {
8084
it('should have `first` arg', () => {
85+
// $FlowFixMe
8186
expect(connectionResolver.getArg('first').type).toBe(GraphQLInt);
8287
});
8388

8489
it('should have `last` arg', () => {
90+
// $FlowFixMe
8591
expect(connectionResolver.getArg('last').type).toBe(GraphQLInt);
8692
});
8793

8894
it('should have `after` arg', () => {
95+
// $FlowFixMe
8996
expect(connectionResolver.getArg('after').type).toBe(Cursor);
9097
});
9198

9299
it('should have `before` arg', () => {
100+
// $FlowFixMe
93101
expect(connectionResolver.getArg('before').type).toBe(Cursor);
94102
});
95103

96104
it('should have `sort` arg', () => {
105+
// $FlowFixMe
97106
expect(connectionResolver.getArg('sort').type.name).toBe('SortConnectionUserEnum');
98107
});
99108
});
@@ -173,8 +182,8 @@ describe('connectionResolver', () => {
173182
count: true,
174183
},
175184
});
176-
expect(countResolverCalled, 'count resolver called').toBe(true);
177-
expect(findManyResolverCalled, 'findMany resolver called').toBe(false);
185+
expect(countResolverCalled).toBe(true);
186+
expect(findManyResolverCalled).toBe(false);
178187
});
179188

180189
it('should call count and findMany resolver when not only count is projected', async () => {
@@ -190,8 +199,8 @@ describe('connectionResolver', () => {
190199
},
191200
},
192201
});
193-
expect(countResolverCalled, 'count resolver called').toBe(true);
194-
expect(findManyResolverCalled, 'findMany resolver called').toBe(true);
202+
expect(countResolverCalled).toBe(true);
203+
expect(findManyResolverCalled).toBe(true);
195204
});
196205

197206
it('should call findMany and not count when arbitrary top level fields are projected without count', async () => {
@@ -202,8 +211,8 @@ describe('connectionResolver', () => {
202211
age: true,
203212
},
204213
});
205-
expect(countResolverCalled, 'count resolver called').toBe(false);
206-
expect(findManyResolverCalled, 'findMany resolver called').toBe(true);
214+
expect(countResolverCalled).toBe(false);
215+
expect(findManyResolverCalled).toBe(true);
207216
});
208217

209218
it('should call findMany and count when arbitrary top level fields are projected with count', async () => {
@@ -215,8 +224,8 @@ describe('connectionResolver', () => {
215224
age: true,
216225
},
217226
});
218-
expect(countResolverCalled, 'count resolver called').toBe(true);
219-
expect(findManyResolverCalled, 'findMany resolver called').toBe(true);
227+
expect(countResolverCalled).toBe(true);
228+
expect(findManyResolverCalled).toBe(true);
220229
});
221230

222231
it('should call count and findMany resolver when last arg is used but not first arg', async () => {
@@ -233,8 +242,8 @@ describe('connectionResolver', () => {
233242
},
234243
},
235244
});
236-
expect(countResolverCalled, 'count resolver called').toBe(true);
237-
expect(findManyResolverCalled, 'findMany resolver called').toBe(true);
245+
expect(countResolverCalled).toBe(true);
246+
expect(findManyResolverCalled).toBe(true);
238247
});
239248

240249
it('should call findMany but not count resolver when first arg is used', async () => {
@@ -249,8 +258,8 @@ describe('connectionResolver', () => {
249258
},
250259
},
251260
});
252-
expect(countResolverCalled, 'count resolver called').toBe(false);
253-
expect(findManyResolverCalled, 'findMany resolver called').toBe(true);
261+
expect(countResolverCalled).toBe(false);
262+
expect(findManyResolverCalled).toBe(true);
254263
});
255264
});
256265

@@ -270,9 +279,10 @@ describe('connectionResolver', () => {
270279

271280
it('should setup in resolveParams.rawQuery', () => {
272281
const rp = {
273-
args: { filter: 123 },
282+
args: { filter: { id: 123 } },
274283
};
275284
prepareRawQuery(rp, sortConfig);
285+
// $FlowFixMe
276286
expect(rp.rawQuery).toEqual({});
277287
});
278288

@@ -286,6 +296,8 @@ describe('connectionResolver', () => {
286296
rawQuery,
287297
};
288298
const dumbSortConfig = {
299+
value: { id: 1 },
300+
cursorFields: ['id'],
289301
beforeCursorQuery: () => {},
290302
afterCursorQuery: () => {},
291303
};
@@ -297,21 +309,23 @@ describe('connectionResolver', () => {
297309
const rp = {
298310
args: {
299311
after: dataToCursor({ id: 123 }),
300-
sort: { id: 1 },
312+
sort: sortConfig,
301313
},
302314
};
303315
prepareRawQuery(rp, sortConfig);
316+
// $FlowFixMe
304317
expect(rp.rawQuery).toEqual({ after: { id: 123 } });
305318
});
306319

307320
it('should call beforeCursorQuery if provided args.before', () => {
308321
const rp = {
309322
args: {
310323
before: dataToCursor({ id: 234 }),
311-
sort: { id: 1 },
324+
sort: sortConfig,
312325
},
313326
};
314327
prepareRawQuery(rp, sortConfig);
328+
// $FlowFixMe
315329
expect(rp.rawQuery).toEqual({ before: { id: 234 } });
316330
});
317331

src/composeWithConnection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* @flow */
22

33
import { TypeComposer } from 'graphql-compose';
4-
import type { composeWithConnectionOpts } from './definition';
4+
import type { ComposeWithConnectionOpts } from './definition';
55
import { prepareConnectionResolver } from './connectionResolver';
66

77
export function composeWithConnection(
88
typeComposer: TypeComposer,
9-
opts: composeWithConnectionOpts
9+
opts: ComposeWithConnectionOpts
1010
): TypeComposer {
1111
if (!(typeComposer instanceof TypeComposer)) {
1212
throw new Error('You should provide TypeComposer instance to composeWithRelay method');

src/connectionResolver.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { Resolver, TypeComposer } from 'graphql-compose';
55
import type {
66
ResolveParams,
77
ConnectionResolveParams,
8-
composeWithConnectionOpts,
9-
connectionSortOpts,
10-
connectionSortMapOpts,
8+
ComposeWithConnectionOpts,
9+
ConnectionSortOpts,
10+
ConnectionSortMapOpts,
1111
GraphQLConnectionType,
1212
} from './definition';
1313
import { prepareConnectionType } from './types/connectionType';
@@ -17,7 +17,7 @@ import { cursorToData, dataToCursor } from './cursor';
1717

1818
export function prepareConnectionResolver<TSource, TContext>(
1919
typeComposer: TypeComposer,
20-
opts: composeWithConnectionOpts
20+
opts: ComposeWithConnectionOpts
2121
): Resolver<TSource, TContext> {
2222
if (!(typeComposer instanceof TypeComposer)) {
2323
throw new Error('First arg for prepareConnectionResolver() should be instance of TypeComposer');
@@ -147,7 +147,7 @@ export function prepareConnectionResolver<TSource, TContext>(
147147
let skip = last > 0 ? first - last : 0;
148148

149149
let prepareCursorData;
150-
const sortConfig: ?connectionSortOpts = findSortConfig(opts.sort, args.sort);
150+
const sortConfig: ?ConnectionSortOpts = findSortConfig(opts.sort, args.sort);
151151
if (sortConfig) {
152152
prepareRawQuery(resolveParams, sortConfig);
153153
findManyParams.rawQuery = resolveParams.rawQuery;
@@ -250,7 +250,7 @@ export function preparePageInfo(
250250

251251
export function prepareRawQuery(
252252
rp: $Shape<ConnectionResolveParams<*, *>>,
253-
sortConfig: connectionSortOpts
253+
sortConfig: ConnectionSortOpts
254254
) {
255255
if (!rp.rawQuery) {
256256
rp.rawQuery = {};
@@ -339,7 +339,7 @@ export function emptyConnection(): GraphQLConnectionType {
339339
};
340340
}
341341

342-
export function findSortConfig(configs: connectionSortMapOpts, val: mixed): ?connectionSortOpts {
342+
export function findSortConfig(configs: ConnectionSortMapOpts, val: mixed): ?ConnectionSortOpts {
343343
// Object.keys(configs).forEach(k => { // return does not works in forEach as I want
344344
for (const k in configs) {
345345
if (configs[k].value === val) {

0 commit comments

Comments
 (0)