From bf2c54084b601040c07587a3f82e5698e175cd95 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 21 Sep 2023 17:17:47 +0800 Subject: [PATCH] fix getArticle API failed on iOS --- packages/api/src/resolvers/article/index.ts | 6 +++- packages/api/test/resolvers/article.test.ts | 40 +++++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/api/src/resolvers/article/index.ts b/packages/api/src/resolvers/article/index.ts index 7e4f52aee..5891179fe 100644 --- a/packages/api/src/resolvers/article/index.ts +++ b/packages/api/src/resolvers/article/index.ts @@ -383,10 +383,14 @@ export const getArticleResolver = authorized< selectColumns.splice(selectColumns.indexOf('originalContent'), 1) } // We allow the backend to use the ID instead of a slug to fetch the article + // query against id if slug is a uuid + const where = slug.match(/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i) + ? { id: slug } + : { slug } const libraryItem = await authTrx((tx) => tx.withRepository(libraryItemRepository).findOne({ select: selectColumns, - where: { slug }, + where, relations: { labels: true, highlights: { diff --git a/packages/api/test/resolvers/article.test.ts b/packages/api/test/resolvers/article.test.ts index b264a5bd2..2959a2072 100644 --- a/packages/api/test/resolvers/article.test.ts +++ b/packages/api/test/resolvers/article.test.ts @@ -453,12 +453,12 @@ describe('Article API', () => { query = getArticleQuery(slug) }) - context('when page exists', () => { + context('when item exists', () => { before(() => { slug = realSlug }) - it('should return the page', async () => { + it('should return the item', async () => { const res = await graphqlRequest(query, authToken).expect(200) expect(res.body.data.article.article.slug).to.eql(slug) @@ -470,7 +470,7 @@ describe('Article API', () => { expect(res.body.data.article.article.highlights).to.length(1) }) - context('when page is failed to process', () => { + context('when item is failed to process', () => { before(async () => { await updateLibraryItem( itemId, @@ -492,7 +492,19 @@ describe('Article API', () => { }) }) - context('when page does not exist', () => { + context('query with id instead of slug', () => { + before(() => { + slug = itemId + }) + + it('returns the item', async () => { + const res = await graphqlRequest(query, authToken).expect(200) + + expect(res.body.data.article.article.id).to.eql(slug) + }) + }) + + context('when item does not exist', () => { before(() => { slug = 'not-a-real-slug' }) @@ -515,7 +527,7 @@ describe('Article API', () => { query = savePageQuery(url, title, originalContent) }) - context('when we save a new page', () => { + context('when we save a new item', () => { after(async () => { await deleteLibraryItemByUrl(url, user.id) }) @@ -528,7 +540,7 @@ describe('Article API', () => { }) }) - context('when we save a page that is already archived', () => { + context('when we save a item that is already archived', () => { before(() => { url = 'https://blog.omnivore.app/new-url' }) @@ -537,7 +549,7 @@ describe('Article API', () => { await deleteLibraryItemByUrl(url, user.id) }) - it('it should return that page in the Search Query', async () => { + it('it should return that item in the Search Query', async () => { await graphqlRequest( savePageQuery(url, title, originalContent), authToken @@ -565,12 +577,12 @@ describe('Article API', () => { }) }) - xcontext('when we also want to save labels and archives the page', () => { + xcontext('when we also want to save labels and archives the item', () => { after(async () => { await deleteLibraryItemById(url, user.id) }) - it('saves the labels and archives the page', async () => { + it('saves the labels and archives the item', async () => { url = 'https://blog.omnivore.app/new-url-2' const state = ArticleSavingRequestStatus.Archived const labels = ['test name', 'test name 2'] @@ -616,7 +628,7 @@ describe('Article API', () => { }) xcontext('when we save labels', () => { - it('saves the labels and archives the page', async () => { + it('saves the labels and archives the item', async () => { url = 'https://blog.omnivore.app/new-url-2' const state = ArticleSavingRequestStatus.Archived const labels = ['test name', 'test name 2'] @@ -1247,7 +1259,7 @@ describe('Article API', () => { for (let i = 0; i < 5; i++) { const itemToSave: DeepPartial = { user, - title: 'typeahead search page', + title: 'typeahead search item', readableContent: '

test

', slug: '', originalUrl: `https://blog.omnivore.app/p/typeahead-search-${i}`, @@ -1316,7 +1328,7 @@ describe('Article API', () => { // Create some test items for (let i = 0; i < 5; i++) { const itemToSave: DeepPartial = { - title: 'test page', + title: 'test item', slug: '', readableContent: '

test

', originalUrl: `https://blog.omnivore.app/p/updates-since-${i}`, @@ -1392,7 +1404,7 @@ describe('Article API', () => { { user, itemType: i == 0 ? PageType.Article : PageType.File, - title: 'test page', + title: 'test item', readableContent: '

test

', slug: '', state: @@ -1467,7 +1479,7 @@ describe('Article API', () => { }) after(async () => { - // Delete the page + // Delete the item await deleteLibraryItemById(articleId, user.id) })