-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): Allow query reponse types to be overridden through San…
…ityQueries (#858)
- Loading branch information
Showing
3 changed files
with
134 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {createClient, type RawQueryResponse} from '@sanity/client' | ||
import {describe, expectTypeOf, test} from 'vitest' | ||
|
||
type FooResult = { | ||
bar: number | ||
} | ||
|
||
// This would normally be generated by @sanity/codegen | ||
declare module '@sanity/client' { | ||
export interface SanityQueries { | ||
"*[_type == 'foo']": FooResult | ||
} | ||
} | ||
|
||
describe('client.fetch', () => { | ||
const client = createClient({}) | ||
|
||
describe('without params', () => { | ||
test('known query type', async () => { | ||
const resp = await client.fetch("*[_type == 'foo']") | ||
expectTypeOf(resp).toMatchTypeOf<FooResult>() | ||
}) | ||
|
||
test('ad-hoc query type', async () => { | ||
const resp = await client.fetch("*[_type == 'bar']") | ||
expectTypeOf(resp).toMatchTypeOf<any>() | ||
}) | ||
|
||
test('ad-hoc query with a custom type', async () => { | ||
type Result = {bar: string} | ||
const resp = await client.fetch<Result>("*[_type == 'bar']") | ||
expectTypeOf(resp).toMatchTypeOf<Result>() | ||
}) | ||
|
||
test('known query type, but overriden with ad-hoc type', async () => { | ||
type Result = {bar: string} | ||
const resp = await client.fetch<Result>("*[_type == 'foo']") | ||
expectTypeOf(resp).toMatchTypeOf<Result>() | ||
}) | ||
}) | ||
|
||
describe('unfiltered response', () => { | ||
test('known query type', async () => { | ||
const resp = await client.fetch("*[_type == 'foo']", {}, {filterResponse: false}) | ||
expectTypeOf(resp).toMatchTypeOf<RawQueryResponse<FooResult>>() | ||
}) | ||
|
||
test('ad-hoc query type', async () => { | ||
const resp = await client.fetch("*[_type == 'bar']", {}, {filterResponse: false}) | ||
expectTypeOf(resp).toMatchTypeOf<RawQueryResponse<any>>() | ||
}) | ||
|
||
test('ad-hoc query with a custom type', async () => { | ||
type Result = {bar: string} | ||
const resp = await client.fetch<Result>("*[_type == 'bar']", {}, {filterResponse: false}) | ||
expectTypeOf(resp).toMatchTypeOf<RawQueryResponse<Result>>() | ||
}) | ||
|
||
test('known query type, but overriden with ad-hoc type', async () => { | ||
type Result = {bar: string} | ||
const resp = await client.fetch<Result>("*[_type == 'foo']", {}, {filterResponse: false}) | ||
expectTypeOf(resp).toMatchTypeOf<RawQueryResponse<Result>>() | ||
}) | ||
}) | ||
}) |