Skip to content

Commit

Permalink
fix: add GROQ query params when large POST queries (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwain authored Jul 4, 2023
1 parent b6359fc commit 4e7a5de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ export function _requestObservable<R>(
}

// GROQ query-only parameters
if (['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && uri.indexOf('/data/query/') === 0) {
if (
['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 &&
uri.indexOf('/data/query/') === 0
) {
if (config.resultSourceMap) {
options.query = {resultSourceMap: true, ...options.query}
}
Expand Down
53 changes: 53 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,27 @@ describe('client', async () => {
expect(res[0].rating, 'data should match').toBe(5)
})

test.skipIf(isEdge)(
'can query for documents with resultSourceMap and perspective',
async () => {
nock(projectHost())
.get(`/v1/data/query/foo?query=*&resultSourceMap=true&perspective=previewDrafts`)
.reply(200, {
ms: 123,
query: '*',
result: [{_id: 'njgNkngskjg', rating: 5}],
})

const client = getClient({
resultSourceMap: true,
perspective: 'previewDrafts',
})
const res = await client.fetch('*', {})
expect(res.length, 'length should match').toBe(1)
expect(res[0].rating, 'data should match').toBe(5)
}
)

test.skipIf(isEdge)('throws on invalid request tag on request', () => {
nock(projectHost())
.get(`/v1/data/query/foo?query=*&tag=mycompany.syncjob`)
Expand Down Expand Up @@ -1240,6 +1261,38 @@ describe('client', async () => {
}
)

test.skipIf(isEdge)(
'uses POST for long queries, but puts resultSourceMap and perspective as query params',
async () => {
const clause: string[] = []
const params: Record<string, string> = {}
for (let i = 1766; i <= 2016; i++) {
clause.push(`title == $beerName${i}`)
params[`beerName${i}`] = `some beer ${i}`
}

// Again, just... don't do this.
const query = `*[_type == "beer" && (${clause.join(' || ')})]`

nock(projectHost())
.filteringRequestBody(/.*/, '*')
.post('/v1/data/query/foo?resultSourceMap=true&perspective=previewDrafts', '*')
.reply(200, {
ms: 123,
query: query,
result: [{_id: 'njgNkngskjg', rating: 5}],
})

const client = getClient({
perspective: 'previewDrafts',
resultSourceMap: true,
})
const res = await client.fetch(query, params)
expect(res.length, 'length should match').toEqual(1)
expect(res[0].rating, 'data should match').toEqual(5)
}
)

test.skipIf(isEdge)('uses POST for long queries also towards CDN', async () => {
const client = createClient({projectId: 'abc123', dataset: 'foo', useCdn: true})

Expand Down

0 comments on commit 4e7a5de

Please sign in to comment.