Skip to content

Commit 542be21

Browse files
committed
refactor: migrate code to [email protected]
1 parent 45cd046 commit 542be21

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

src/__tests__/composeWithPagination-test.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('composeWithRelay', () => {
1616
describe('basic checks', () => {
1717
it('should return ObjectTypeComposer', () => {
1818
expect(userComposer).toBeInstanceOf(ObjectTypeComposer);
19+
expect(userComposer).toBe(UserTC);
1920
});
2021

2122
it('should throw error if first arg is not ObjectTypeComposer', () => {
@@ -165,8 +166,8 @@ describe('composeWithRelay', () => {
165166
});
166167

167168
it('should pass `countResolveParams` to top resolverParams', async () => {
169+
// first build
168170
let topResolveParams: any = {};
169-
170171
schemaComposer.Query.setField(
171172
'userPagination',
172173
UserTC.getResolver('pagination').wrapResolve(next => rp => {
@@ -181,13 +182,41 @@ describe('composeWithRelay', () => {
181182
count
182183
}
183184
}`;
184-
await graphql(schema, query);
185-
185+
const res = await graphql(schema, query);
186+
expect(res).toEqual({ data: { userPagination: { count: 15 } } });
186187
expect(Object.keys(topResolveParams.countResolveParams)).toEqual(
187188
expect.arrayContaining(['source', 'args', 'context', 'info', 'projection'])
188189
);
189190
expect(topResolveParams.countResolveParams.args).toEqual({
190191
filter: { age: 45 },
192+
perPage: 5,
193+
});
194+
195+
// second build
196+
let topResolveParams2: any = {};
197+
schemaComposer.Query.setField(
198+
'userPagination',
199+
UserTC.getResolver('pagination').wrapResolve(next => rp => {
200+
const result = next(rp);
201+
topResolveParams2 = rp;
202+
return result;
203+
})
204+
);
205+
206+
const schema2 = schemaComposer.buildSchema();
207+
const query2 = `{
208+
userPagination(filter: { age: 333 }) {
209+
count
210+
}
211+
}`;
212+
const res2 = await graphql(schema2, query2);
213+
expect(res2).toEqual({ data: { userPagination: { count: 15 } } });
214+
expect(Object.keys(topResolveParams2.countResolveParams)).toEqual(
215+
expect.arrayContaining(['source', 'args', 'context', 'info', 'projection'])
216+
);
217+
expect(topResolveParams2.countResolveParams.args).toEqual({
218+
filter: { age: 333 },
219+
perPage: 5,
191220
});
192221
});
193222

@@ -204,18 +233,19 @@ describe('composeWithRelay', () => {
204233
);
205234
const schema = schemaComposer.buildSchema();
206235
const query = `{
207-
userPagination(filter: { age: 45 }) {
236+
userPagination(filter: { age: 55 }) {
208237
count
209238
}
210239
}`;
211-
await graphql(schema, query);
240+
const res = await graphql(schema, query);
241+
expect(res).toEqual({ data: { userPagination: { count: 15 } } });
212242

213243
expect(Object.keys(topResolveParams.findManyResolveParams)).toEqual(
214244
expect.arrayContaining(['source', 'args', 'context', 'info', 'projection'])
215245
);
216246

217247
expect(topResolveParams.findManyResolveParams.args).toEqual({
218-
filter: { age: 45 },
248+
filter: { age: 55 },
219249
limit: 6,
220250
perPage: 5,
221251
});

src/__tests__/paginationResolver-test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ describe('paginationResolver', () => {
337337
expect(spyCountResolve.mock.calls).toEqual([
338338
[
339339
{
340-
args: { filter: {} },
340+
args: {
341+
filter: {},
342+
sort: {
343+
_id: 1,
344+
},
345+
},
341346
projection: { count: true, items: { name: true } },
342347
rawQuery: undefined,
343348
},

src/paginationResolver.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type {
55
Resolver,
66
ObjectTypeComposer,
7-
ResolveParams, // eslint-disable-line
7+
ResolverResolveParams, // eslint-disable-line
88
ProjectionType,
99
} from 'graphql-compose';
1010
import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
@@ -128,9 +128,6 @@ export function preparePaginationResolver<TSource, TContext>(
128128
let countPromise;
129129
let findManyPromise;
130130
const { projection = {}, args, rawQuery } = rp;
131-
const findManyParams: $Shape<ResolveParams<TSource, TContext, any>> = {
132-
...rp,
133-
};
134131

135132
const page = parseInt(args.page, 10) || 1;
136133
if (page <= 0) {
@@ -141,10 +138,11 @@ export function preparePaginationResolver<TSource, TContext>(
141138
throw new Error('Argument `perPage` should be positive number.');
142139
}
143140

144-
const countParams: $Shape<ResolveParams<TSource, TContext, any>> = {
141+
const countParams: $Shape<ResolverResolveParams<TSource, TContext, any>> = {
145142
...rp,
146143
rawQuery,
147144
args: {
145+
...rp.args,
148146
filter: { ...rp.args.filter },
149147
},
150148
};
@@ -158,6 +156,10 @@ export function preparePaginationResolver<TSource, TContext>(
158156
countPromise = Promise.resolve(0);
159157
}
160158

159+
const findManyParams: $Shape<ResolverResolveParams<TSource, TContext, any>> = {
160+
...rp,
161+
};
162+
161163
if (projection && projection.items) {
162164
// combine top level projection
163165
// (maybe somebody add additional fields via rp.projection)

src/types/__tests__/preparePaginationType-test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ describe('preparePaginationTC()', () => {
5454
expect(items.name).toEqual('User');
5555
});
5656

57-
it('should have `ofType` property (like GraphQLList, GraphQLNonNull)', () => {
58-
// this behavior needed for `graphql-compose` module in `projection` helper
59-
// otherwise it incorrectly construct projectionMapper for tricky fields
60-
const connectionType: any = preparePaginationTC(UserTC).getType();
61-
expect(connectionType.ofType).toEqual(UserTC.getType());
62-
});
63-
6457
it('should return same type for same Type in ObjectTypeComposer', () => {
6558
const t1 = preparePaginationTC(UserTC);
6659
const t2 = preparePaginationTC(UserTC);

src/types/preparePaginationType.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function preparePaginationTC<TSource, TContext>(
4343
): ObjectTypeComposer<TSource, TContext> {
4444
const schemaComposer = tc.schemaComposer;
4545
const name = `${tc.getTypeName()}${upperFirst(resolverName || 'pagination')}`;
46-
const type = tc.getType();
4746

4847
if (schemaComposer.has(name)) {
4948
return schemaComposer.getOTC(name);
@@ -68,10 +67,5 @@ export function preparePaginationTC<TSource, TContext>(
6867
},
6968
});
7069

71-
// This is small HACK for providing to graphql-compose/src/projection.js
72-
// information about required fields in projection and relations
73-
// $FlowFixMe
74-
paginationTC.gqType.ofType = type;
75-
7670
return paginationTC;
7771
}

0 commit comments

Comments
 (0)