Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add GROQ query params when large POST queries #255

Merged
merged 1 commit into from
Jul 4, 2023
Merged
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
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