diff --git a/src/data/dataMethods.ts b/src/data/dataMethods.ts index c6a8301d..8f38a509 100644 --- a/src/data/dataMethods.ts +++ b/src/data/dataMethods.ts @@ -303,7 +303,10 @@ export function _requestObservable( } // 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} } diff --git a/test/client.test.ts b/test/client.test.ts index 833d2d16..426dfda2 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -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`) @@ -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 = {} + 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})