Skip to content

Commit 5589d29

Browse files
committed
Prepared for graphql 0.6.2
1 parent 78f4062 commit 5589d29

File tree

6 files changed

+73
-81
lines changed

6 files changed

+73
-81
lines changed

src/__mocks__/userTypeComposer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-param-reassign */
2+
13
import { TypeComposer, Resolver } from 'graphql-compose';
24
import {
35
GraphQLString,
@@ -182,3 +184,38 @@ export const countResolver = new Resolver(userTypeComposer, {
182184
);
183185
},
184186
});
187+
188+
189+
export const sortOptions = {
190+
ID_ASC: {
191+
uniqueFields: ['id'],
192+
sortValue: { id: 1 },
193+
directionFilter: (filter, cursorData, isBefore) => {
194+
filter._operators = filter._operators || {};
195+
filter._operators.id = filter._operators.id || {};
196+
if (isBefore) {
197+
filter._operators.id.lt = cursorData.id;
198+
} else {
199+
filter._operators.id.gt = cursorData.id;
200+
}
201+
return filter;
202+
},
203+
},
204+
AGE_ID_DESC: {
205+
uniqueFields: ['age', 'id'],
206+
sortValue: { age: -1, id: -1 },
207+
directionFilter: (filter, cursorData, isBefore) => {
208+
filter._operators = filter._operators || {};
209+
filter._operators.id = filter._operators.id || {};
210+
filter._operators.age = filter._operators.age || {};
211+
if (isBefore) {
212+
filter._operators.age.gt = cursorData.age;
213+
filter._operators.id.gt = cursorData.id;
214+
} else {
215+
filter._operators.age.lt = cursorData.age;
216+
filter._operators.id.lt = cursorData.id;
217+
}
218+
return filter;
219+
},
220+
},
221+
};

src/__tests__/composeWithConnection-test.js

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { expect } from 'chai';
44
import { TypeComposer } from 'graphql-compose';
55
import { composeWithConnection } from '../composeWithConnection';
6-
import { userTypeComposer } from '../__mocks__/userTypeComposer';
6+
import { userTypeComposer, sortOptions } from '../__mocks__/userTypeComposer';
77
import { rootQueryTypeComposer } from '../__mocks__/rootQueryTypeComposer';
88
import {
99
graphql,
@@ -15,39 +15,7 @@ describe('composeWithRelay', () => {
1515
const userComposer = composeWithConnection(userTypeComposer, {
1616
countResolverName: 'count',
1717
findResolverName: 'findMany',
18-
sort: {
19-
ID_ASC: {
20-
uniqueFields: ['id'],
21-
sortValue: { id: 1 },
22-
directionFilter: (filter, cursorData, isBefore) => {
23-
filter._operators = filter._operators || {};
24-
filter._operators.id = filter._operators.id || {};
25-
if (isBefore) {
26-
filter._operators.id.lt = cursorData.id;
27-
} else {
28-
filter._operators.id.gt = cursorData.id;
29-
}
30-
return filter;
31-
},
32-
},
33-
AGE_ID_DESC: {
34-
uniqueFields: ['age', 'id'],
35-
sortValue: { age: -1, id: -1 },
36-
directionFilter: (filter, cursorData, isBefore) => {
37-
filter._operators = filter._operators || {};
38-
filter._operators.id = filter._operators.id || {};
39-
filter._operators.age = filter._operators.age || {};
40-
if (isBefore) {
41-
filter._operators.age.gt = cursorData.age;
42-
filter._operators.id.gt = cursorData.id;
43-
} else {
44-
filter._operators.age.lt = cursorData.age;
45-
filter._operators.id.lt = cursorData.id;
46-
}
47-
return filter;
48-
},
49-
},
50-
},
18+
sort: sortOptions,
5119
});
5220

5321
describe('basic checks', () => {
@@ -170,24 +138,16 @@ describe('composeWithRelay', () => {
170138
});
171139
});
172140

173-
describe('projection()', () => {
141+
describe('fragments fields projection of graphql-compose', () => {
174142
it('should return object', async () => {
175-
// const resolver = userTypeComposer.getResolver('connection');
176-
// const resolve = resolver.resolve;
177-
// resolver.resolve = (resolveParams) => {
178-
// const pr = getProjectionFromAST(resolveParams.info);
179-
// console.log(pr);
180-
// resolve(resolveParams);
181-
// };
182-
183143
rootQueryTypeComposer.addField('userConnection',
184144
userTypeComposer.getResolver('connection').getFieldConfig()
185145
);
186146
const schema = new GraphQLSchema({
187147
query: rootQueryTypeComposer.getType(),
188148
});
189149
const query = `{
190-
userConnection(first: 3) {
150+
userConnection(first: 1) {
191151
count,
192152
pageInfo {
193153
startCursor
@@ -215,7 +175,30 @@ describe('composeWithRelay', () => {
215175
}
216176
`;
217177
const result = await graphql(schema, query);
218-
console.log(result.data.userConnection.edges);
178+
expect(result).to.deep.equal({
179+
data: {
180+
userConnection: {
181+
count: 15,
182+
edges: [
183+
{
184+
cursor: 'eyJpZCI6MX0=',
185+
node: {
186+
age: 11,
187+
gender: 'm',
188+
id: 1,
189+
name: 'user1',
190+
},
191+
},
192+
],
193+
pageInfo: {
194+
endCursor: 'eyJpZCI6MX0=',
195+
hasPreviousPage: false,
196+
hasNextPage: true,
197+
startCursor: 'eyJpZCI6MX0=',
198+
},
199+
},
200+
},
201+
});
219202
});
220203
});
221204
});

src/__tests__/mocks-userTypeComposer-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ describe('mocks/userTypeComposer', () => {
4646
},
4747
});
4848
expect(res).to.be.deep.equal([
49+
{ id: 11, name: 'user11', age: 49, gender: 'm' },
4950
{ id: 10, name: 'user10', age: 49, gender: 'f' },
50-
{ id: 11, name: 'user11', age: 48, gender: 'm' },
5151
{ id: 12, name: 'user12', age: 47, gender: 'f' },
5252
{ id: 15, name: 'user15', age: 45, gender: 'm' },
5353
{ id: 14, name: 'user14', age: 45, gender: 'm' },

src/resolvers/__tests__/connectionResolver-test.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-param-reassign */
22

33
import { expect } from 'chai';
4-
import { userTypeComposer, userList } from '../../__mocks__/userTypeComposer';
4+
import { userTypeComposer, userList, sortOptions } from '../../__mocks__/userTypeComposer';
55
import { Resolver } from 'graphql-compose';
66
import { dataToCursor } from '../../cursor';
77
import {
@@ -14,22 +14,6 @@ import { GraphQLInt } from 'graphql';
1414

1515

1616
describe('connectionResolver', () => {
17-
const sortOptions = {
18-
ID_ASC: {
19-
uniqueFields: ['id'],
20-
sortValue: { id: 1 },
21-
directionFilter: (filter, cursorData, isBefore) => {
22-
filter._operators = filter._operators || {};
23-
filter._operators.id = filter._operators.id || {};
24-
if (isBefore) {
25-
filter._operators.id.lt = cursorData.id;
26-
} else {
27-
filter._operators.id.gt = cursorData.id;
28-
}
29-
return filter;
30-
},
31-
},
32-
};
3317
const connectionResolver = prepareConnectionResolver(userTypeComposer, {
3418
countResolverName: 'count',
3519
findResolverName: 'findMany',

src/resolvers/connectionResolver.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function prepareConnectionResolver(
7878
...additionalArgs,
7979
sort: {
8080
type: sortEnumType,
81-
defaultValue: sortEnumType.getValues()[0].name, // first enum used by default
81+
defaultValue: sortEnumType.getValues()[0].value,
8282
description: 'Sort argument for data ordering',
8383
},
8484
},
@@ -90,28 +90,16 @@ export function prepareConnectionResolver(
9090
resolveParams,
9191
{ args: {} } // clear this params in copy
9292
);
93-
let sortOptions: connectionSortOpts;
94-
if (typeof args.sort === 'string') {
95-
const sortValue = sortEnumType.parseValue(args.sort);
96-
if (sortValue) {
97-
sortOptions = sortValue;
98-
} else {
99-
sortOptions = {
100-
sortValue: {},
101-
uniqueFields: [],
102-
// $FlowFixMe
103-
directionFilter: filter => filter,
104-
};
105-
}
106-
} else {
107-
sortOptions = args.sort;
108-
}
93+
94+
const sortOptions: connectionSortOpts = args.sort;
10995

11096
findManyParams.args.filter = prepareFilter(args);
11197
findManyParams.args.sort = sortOptions.sortValue;
11298

11399
if (projection && projection.edges) {
114100
findManyParams.projection = projection.edges.node || {};
101+
} else {
102+
findManyParams.projection = {};
115103
}
116104
sortOptions.uniqueFields.forEach(fieldName => {
117105
findManyParams.projection[fieldName] = true;

src/types/sortInputType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @flow */
2-
/* eslint-disable no-use-before-define */
2+
/* eslint-disable no-use-before-define, no-param-reassign */
33

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

0 commit comments

Comments
 (0)