From e320e85a28005e0215e0a138c45fe65f43b7868c Mon Sep 17 00:00:00 2001 From: ryohidaka <39184410+ryohidaka@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:18:05 +0900 Subject: [PATCH 1/2] fix: fetch all shops for GraphQL pagination --- src/app.ts | 9 +++++++++ src/graphql.ts | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index 69442bf..33e9367 100644 --- a/src/app.ts +++ b/src/app.ts @@ -150,6 +150,15 @@ export const listShops = async ( return { shops, totalCount } } +export const listAllShops = async ( + options: Options +): Promise => { + const data = await getShopsData(options.c.env.ASSETS, options.c.req.url) + const shopIds: string[] = data['shopIds'] + const shops = await Promise.all(shopIds.map((id) => getShop(id, options))) + return { shops, totalCount: shopIds.length } +} + export const findIndexFromId = async ( id: string, options: Options diff --git a/src/graphql.ts b/src/graphql.ts index 5fad33a..2cf0e68 100644 --- a/src/graphql.ts +++ b/src/graphql.ts @@ -6,7 +6,13 @@ import { GraphQLList, GraphQLInt, } from 'graphql' -import { getShop, listShops, getAuthor, type Shop, type Options } from '@/app' +import { + getShop, + listAllShops, + getAuthor, + type Shop, + type Options, +} from '@/app' import { Pager } from '@/pager' const authorType = new GraphQLObjectType({ @@ -92,7 +98,7 @@ const getQueryType = (options: Options) => { before, }: { first: number; after: string; last: number; before: string } ) => { - const list = await listShops({}, options) + const list = await listAllShops(options) for (let i = 0; i < list.shops.length; i++) { list.shops[i] = await setShopPhotoAuthor(list.shops[i], options) } From cc1bd5fed6d77d365b4de177580c1e3d16204766 Mon Sep 17 00:00:00 2001 From: ryohidaka <39184410+ryohidaka@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:38:21 +0900 Subject: [PATCH 2/2] test: cover GraphQL pagination beyond default limit --- test/graphql.test.ts | 37 ++++++++++++++++++++++++++++++++++++- test/mockApp.ts | 8 ++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/test/graphql.test.ts b/test/graphql.test.ts index 2b64066..26b240a 100644 --- a/test/graphql.test.ts +++ b/test/graphql.test.ts @@ -1,4 +1,4 @@ -import mockApp from './mockApp' +import mockApp, { largeMockApp } from './mockApp' import { BASE_URL } from '@/app' import { app } from '@/index' @@ -24,6 +24,17 @@ const postRequest = async (data: object): Promise => { }) } +const postLargeRequest = async (data: object): Promise => { + const req = new Request('http://localhost/graphql', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(data), + }) + return await app.request(req, undefined, { + ASSETS: { fetch: largeMockApp.request }, + }) +} + describe('Query shop', () => { it('Should return Shop response', async () => { const query = { @@ -72,6 +83,30 @@ describe('Query shop', () => { }) describe('Query shops', () => { + it('Paginates across more than the listShops default limit', async () => { + const query = { + query: ` + { + shops(first: 1, after: "c2hvcC0xMA==") { + edges { node { id } } + totalCount + pageInfo { hasNextPage hasPreviousPage } + } + } + `, + } + const res = await postLargeRequest(query) + const data = await res.json() + const shops = data['data']['shops'] + + expect(shops.edges).toEqual([{ node: { id: 'shop-11' } }]) + expect(shops.totalCount).toBe(11) + expect(shops.pageInfo).toEqual({ + hasNextPage: false, + hasPreviousPage: true, + }) + }) + it('Return null', async () => { const query = { query: ` diff --git a/test/mockApp.ts b/test/mockApp.ts index c0f094c..343ac94 100644 --- a/test/mockApp.ts +++ b/test/mockApp.ts @@ -2,6 +2,14 @@ import { Hono } from 'hono' const app = new Hono().basePath('/content') +export const largeMockApp = new Hono().basePath('/content') +const largeShopIds = Array.from({ length: 11 }, (_, i) => `shop-${i + 1}`) + +largeMockApp.get('/shops.json', (c) => c.json({ shopIds: largeShopIds })) +largeMockApp.get('/shops/:id/info.json', (c) => + c.json({ id: c.req.param('id'), name: c.req.param('id'), photos: [] }) +) + app.get('/shops.json', (c) => { return c.json({ shopIds: ['yoshimuraya', 'sugitaya', 'takasagoya'],