Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ export const listShops = async (
return { shops, totalCount }
}

export const listAllShops = async (
options: Options
): Promise<listShopsResult> => {
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
Expand Down
10 changes: 8 additions & 2 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
}
Expand Down
37 changes: 36 additions & 1 deletion test/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mockApp from './mockApp'
import mockApp, { largeMockApp } from './mockApp'
import { BASE_URL } from '@/app'
import { app } from '@/index'

Expand All @@ -24,6 +24,17 @@ const postRequest = async (data: object): Promise<Response> => {
})
}

const postLargeRequest = async (data: object): Promise<Response> => {
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 = {
Expand Down Expand Up @@ -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: `
Expand Down
8 changes: 8 additions & 0 deletions test/mockApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down