Skip to content

Commit

Permalink
feat(ts-client): batch method
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 14, 2024
1 parent b9888e2 commit 9191249
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
53 changes: 31 additions & 22 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,67 @@ const client = () => create<Index>({ url: ctx.url, schemaIndex })

test.todo(`query`, async () => {
const mockRes = ctx.res({ body: { data } }).spec.body!
expect(await client().query({ fooBarUnion: { onBar: { int: true } } })).toEqual(mockRes.data)
expect(await client().query.$batch({ fooBarUnion: { onBar: { int: true } } })).toEqual(mockRes.data)
})

describe(`custom scalar`, () => {
describe(`output`, () => {
test(`query field`, async () => {
ctx.res({ body: { data: { date: 0 } } })
expect(await client().query({ date: true })).toEqual({ date: new Date(0) })
expect(await client().query.$batch({ date: true })).toEqual({ date: new Date(0) })
})
test(`query field in non-null`, async () => {
ctx.res({ body: { data: { dateNonNull: 0 } } })
expect(await client().query({ dateNonNull: true })).toEqual({ dateNonNull: new Date(0) })
expect(await client().query.$batch({ dateNonNull: true })).toEqual({ dateNonNull: new Date(0) })
})
test(`query field in list`, async () => {
ctx.res({ body: { data: { dateList: [0, 1] } } })
expect(await client().query({ dateList: true })).toEqual({ dateList: [new Date(0), new Date(1)] })
expect(await client().query.$batch({ dateList: true })).toEqual({ dateList: [new Date(0), new Date(1)] })
})
test(`query field in list non-null`, async () => {
ctx.res({ body: { data: { dateList: [0, 1] } } })
expect(await client().query({ dateList: true })).toEqual({ dateList: [new Date(0), new Date(1)] })
expect(await client().query.$batch({ dateList: true })).toEqual({ dateList: [new Date(0), new Date(1)] })
})
test(`object field`, async () => {
ctx.res({ body: { data: { dateObject1: { date1: 0 } } } })
expect(await client().query({ dateObject1: { date1: true } })).toEqual({ dateObject1: { date1: new Date(0) } })
expect(await client().query.$batch({ dateObject1: { date1: true } })).toEqual({
dateObject1: { date1: new Date(0) },
})
})
test(`object field in interface`, async () => {
ctx.res({ body: { data: { dateInterface1: { date1: 0 } } } })
expect(await client().query({ dateInterface1: { date1: true } })).toEqual({
expect(await client().query.$batch({ dateInterface1: { date1: true } })).toEqual({
dateInterface1: { date1: new Date(0) },
})
})
describe(`object field in union`, () => {
test(`case 1 with __typename`, async () => {
ctx.res({ body: { data: { dateUnion: { __typename: `DateObject1`, date1: 0 } } } })
expect(await client().query({ dateUnion: { __typename: true, onDateObject1: { date1: true } } })).toEqual({
dateUnion: { __typename: `DateObject1`, date1: new Date(0) },
})
expect(await client().query.$batch({ dateUnion: { __typename: true, onDateObject1: { date1: true } } }))
.toEqual({
dateUnion: { __typename: `DateObject1`, date1: new Date(0) },
})
})
test(`case 1 without __typename`, async () => {
ctx.res({ body: { data: { dateUnion: { date1: 0 } } } })
expect(await client().query({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
expect(await client().query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
dateUnion: { date1: new Date(0) },
})
})
test(`case 2`, async () => {
ctx.res({ body: { data: { dateUnion: { date2: 0 } } } })
expect(await client().query({ dateUnion: { onDateObject1: { date1: true }, onDateObject2: { date2: true } } }))
expect(
await client().query.$batch({
dateUnion: { onDateObject1: { date1: true }, onDateObject2: { date2: true } },
}),
)
.toEqual({ dateUnion: { date2: new Date(0) } })
})
test(`case 2 miss`, async () => {
ctx.res({ body: { data: { dateUnion: null } } })
expect(await client().query({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({ dateUnion: null }) // dprint-ignore
expect(await client().query.$batch({ dateUnion: { onDateObject1: { date1: true } } })).toEqual({
dateUnion: null,
}) // dprint-ignore
})
})
})
Expand All @@ -91,46 +100,46 @@ describe(`custom scalar`, () => {

test(`arg field`, async () => {
const client = clientExpected((doc) => expect(doc.dateArg.$.date).toEqual(new Date(0).getTime()))
await client.query({ dateArg: { $: { date: new Date(0) } } })
await client.query.$batch({ dateArg: { $: { date: new Date(0) } } })
})
test('arg field in non-null', async () => {
const client = clientExpected((doc) => expect(doc.dateArgNonNull.$.date).toEqual(new Date(0).getTime()))
await client.query({ dateArgNonNull: { $: { date: new Date(0) } } })
await client.query.$batch({ dateArgNonNull: { $: { date: new Date(0) } } })
})
test('arg field in list', async () => {
const client = clientExpected((doc) =>
expect(doc.dateArgList.$.date).toEqual([new Date(0).getTime(), new Date(1).getTime()])
)
await client.query({ dateArgList: { $: { date: [new Date(0), new Date(1)] } } })
await client.query.$batch({ dateArgList: { $: { date: [new Date(0), new Date(1)] } } })
})
test('arg field in list (null)', async () => {
const client = clientExpected((doc) => expect(doc.dateArgList.$.date).toEqual(null))
await client.query({ dateArgList: { $: { date: null } } })
await client.query.$batch({ dateArgList: { $: { date: null } } })
})
test('arg field in non-null list (with list)', async () => {
const client = clientExpected((doc) =>
expect(doc.dateArgNonNullList.$.date).toEqual([new Date(0).getTime(), new Date(1).getTime()])
)
await client.query({ dateArgNonNullList: { $: { date: [new Date(0), new Date(1)] } } })
await client.query.$batch({ dateArgNonNullList: { $: { date: [new Date(0), new Date(1)] } } })
})
test('arg field in non-null list (with null)', async () => {
const client = clientExpected((doc) =>
expect(doc.dateArgNonNullList.$.date).toEqual([null, new Date(0).getTime()])
)
await client.query({ dateArgNonNullList: { $: { date: [null, new Date(0)] } } })
await client.query.$batch({ dateArgNonNullList: { $: { date: [null, new Date(0)] } } })
})
test('arg field in non-null list non-null', async () => {
const client = clientExpected((doc) =>
expect(doc.dateArgNonNullListNonNull.$.date).toEqual([new Date(0).getTime(), new Date(1).getTime()])
)
await client.query({ dateArgNonNullListNonNull: { $: { date: [new Date(0), new Date(1)] } } })
await client.query.$batch({ dateArgNonNullListNonNull: { $: { date: [new Date(0), new Date(1)] } } })
})
test(`input object field`, async () => {
const client = clientExpected((doc) => {
expect(doc.dateArgInputObject.$.input.dateRequired).toEqual(new Date(0).getTime())
expect(doc.dateArgInputObject.$.input.date).toEqual(new Date(1).getTime())
})
await client.query({
await client.query.$batch({
dateArgInputObject: { $: { input: { idRequired: '', dateRequired: new Date(0), date: new Date(1) } } },
})
})
Expand All @@ -139,7 +148,7 @@ describe(`custom scalar`, () => {
expect(doc.InputObjectNested.$.input.InputObject.dateRequired).toEqual(new Date(0).getTime())
expect(doc.InputObjectNested.$.input.InputObject.date).toEqual(new Date(1).getTime())
})
await client.query({
await client.query.$batch({
InputObjectNested: {
$: { input: { InputObject: { idRequired: '', dateRequired: new Date(0), date: new Date(1) } } },
},
Expand Down
8 changes: 6 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ export type Client<$SchemaIndex extends Schema.Index> =
$SchemaIndex['Root']['Query'] extends null
? unknown
: {
query: <$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Query<$SchemaIndex>>) => Promise<ResultSet.Query<$SelectionSet, $SchemaIndex>>
query: {
$batch: <$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Query<$SchemaIndex>>) => Promise<ResultSet.Query<$SelectionSet, $SchemaIndex>>
}
}
)
& (
$SchemaIndex['Root']['Mutation'] extends null
? unknown
: {
mutation: <$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Mutation<$SchemaIndex>>) => Promise<ResultSet.Mutation<$SelectionSet,$SchemaIndex>>
mutation: {
$batch: <$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Mutation<$SchemaIndex>>) => Promise<ResultSet.Mutation<$SelectionSet,$SchemaIndex>>
}
}
)
// todo
Expand Down

0 comments on commit 9191249

Please sign in to comment.