Skip to content

Commit 193d25f

Browse files
authored
Merge pull request #541 from abillionveg/feature/allow-fetching-with-zero-edges
fix: allow fetching zero edges
2 parents 31210d7 + 7b43170 commit 193d25f

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

src/builder/ConnectionBuilder.spec.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Cursor } from '../cursor/Cursor';
22
import { Foo, FooConnection, FooConnectionBuilder, FooEdge } from '../../test/FooConnection';
33
import { BarConnectionBuilder, FruitBar, NutBar } from '../../test/BarConnection';
4+
import { ConnectionArgsValidationError } from '../error';
45

56
describe('ConnectionBuilder', () => {
67
test('First page is built correctly', () => {
@@ -283,4 +284,115 @@ describe('ConnectionBuilder', () => {
283284
],
284285
});
285286
});
287+
288+
test('Can fetch zero first edges', () => {
289+
const builder = new FooConnectionBuilder({
290+
first: 0,
291+
});
292+
293+
expect(builder.edgesPerPage).toBe(0);
294+
expect(builder.afterCursor).toBeUndefined();
295+
expect(builder.beforeCursor).toBeUndefined();
296+
297+
const connection = builder.build({
298+
totalEdges: 12,
299+
nodes: [],
300+
});
301+
302+
expect(connection).toMatchObject({
303+
pageInfo: {
304+
totalEdges: 12,
305+
hasNextPage: true,
306+
hasPreviousPage: false,
307+
},
308+
edges: [],
309+
});
310+
});
311+
312+
test('Can fetch zero last edges', () => {
313+
const builder = new FooConnectionBuilder({
314+
last: 0,
315+
});
316+
317+
expect(builder.edgesPerPage).toBe(0);
318+
expect(builder.afterCursor).toBeUndefined();
319+
expect(builder.beforeCursor).toBeUndefined();
320+
321+
const connection = builder.build({
322+
totalEdges: 12,
323+
nodes: [],
324+
});
325+
326+
expect(connection).toMatchObject({
327+
pageInfo: {
328+
totalEdges: 12,
329+
hasNextPage: true,
330+
hasPreviousPage: false,
331+
},
332+
edges: [],
333+
});
334+
});
335+
336+
describe('Connection arguments', () => {
337+
test('Should throw an error if the connection does not support offset pagination', () => {
338+
const createBuilder = () =>
339+
new FooConnectionBuilder({
340+
page: 1,
341+
});
342+
343+
expect(createBuilder).toThrow(
344+
new ConnectionArgsValidationError('This connection does not support the "page" argument for pagination.'),
345+
);
346+
});
347+
348+
test('Should throw an error if first is less than zero', () => {
349+
const createBuilder = () =>
350+
new FooConnectionBuilder({
351+
first: -1,
352+
});
353+
354+
expect(createBuilder).toThrow(
355+
new ConnectionArgsValidationError(`The "first" argument accepts a value between 0 and 100, inclusive.`),
356+
);
357+
});
358+
359+
test('Should throw an error if both "first" and "last" arguments are supplied', () => {
360+
const createBuilder = () =>
361+
new FooConnectionBuilder({
362+
first: 5,
363+
last: 5,
364+
});
365+
366+
expect(createBuilder).toThrow(
367+
new ConnectionArgsValidationError(
368+
'It is not permitted to specify both "first" and "last" arguments simultaneously.',
369+
),
370+
);
371+
});
372+
373+
test('Should throw an error if last is less than zero', () => {
374+
const createBuilder = () =>
375+
new FooConnectionBuilder({
376+
last: -1,
377+
});
378+
379+
expect(createBuilder).toThrow(
380+
new ConnectionArgsValidationError(`The "last" argument accepts a value between 0 and 100, inclusive.`),
381+
);
382+
});
383+
384+
test('Should throw an error if both "after" and "before" arguments are supplied', () => {
385+
const createBuilder = () =>
386+
new FooConnectionBuilder({
387+
after: '...',
388+
before: '...',
389+
});
390+
391+
expect(createBuilder).toThrow(
392+
new ConnectionArgsValidationError(
393+
'It is not permitted to specify both "after" and "before" arguments simultaneously.',
394+
),
395+
);
396+
});
397+
});
286398
});

src/builder/ConnectionBuilder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ export abstract class ConnectionBuilder<
151151
}
152152

153153
if (first != null) {
154-
if (first > maxEdgesPerPage || first < 1) {
154+
if (first > maxEdgesPerPage || first < 0) {
155155
throw new ConnectionArgsValidationError(
156-
`The "first" argument accepts a value between 1 and ${maxEdgesPerPage}, inclusive.`,
156+
`The "first" argument accepts a value between 0 and ${maxEdgesPerPage}, inclusive.`,
157157
);
158158
}
159159

@@ -171,9 +171,9 @@ export abstract class ConnectionBuilder<
171171
throw new ConnectionArgsValidationError('This connection does not support the "last" argument for pagination.');
172172
}
173173

174-
if (last > maxEdgesPerPage || last < 1) {
174+
if (last > maxEdgesPerPage || last < 0) {
175175
throw new ConnectionArgsValidationError(
176-
`The "last" argument accepts a value between 1 and ${maxEdgesPerPage}, inclusive.`,
176+
`The "last" argument accepts a value between 0 and ${maxEdgesPerPage}, inclusive.`,
177177
);
178178
}
179179

0 commit comments

Comments
 (0)