-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts-client): document method (#774)
- Loading branch information
1 parent
98cb065
commit 73adae5
Showing
40 changed files
with
918 additions
and
95 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,74 @@ | ||
import { describe, expectTypeOf, test } from 'vitest' | ||
import * as Schema from '../../tests/_/schema/schema.js' | ||
import * as SchemaMutationOnly from '../../tests/_/schemaMutationOnly/schema.js' | ||
import * as SchemaQueryOnly from '../../tests/_/schemaQueryOnly/schema.js' | ||
import { create } from './client.js' | ||
|
||
const client = create<Schema.Index>({ schema: Schema.schema, schemaIndex: Schema.$Index }) | ||
|
||
test(`requires input`, () => { | ||
// @ts-expect-error missing input | ||
client.document() | ||
// @ts-expect-error empty object | ||
client.document({}) | ||
}) | ||
|
||
describe(`input`, () => { | ||
test(`document with one query`, () => { | ||
const run = client.document({ foo: { query: { id: true } } }).run | ||
expectTypeOf(run).toMatchTypeOf<(...params: ['foo'] | [] | [undefined]) => Promise<any>>() | ||
}) | ||
|
||
test(`document with two queries`, () => { | ||
const run = client.document({ | ||
foo: { query: { id: true } }, | ||
bar: { query: { id: true } }, | ||
}).run | ||
expectTypeOf(run).toMatchTypeOf<(name: 'foo' | 'bar') => Promise<any>>() | ||
}) | ||
|
||
test(`root operation not available if it is not in schema`, () => { | ||
const clientQueryOnly = create<SchemaQueryOnly.Index>({ | ||
schema: SchemaQueryOnly.schema, | ||
schemaIndex: SchemaQueryOnly.$Index, | ||
}) | ||
clientQueryOnly.document({ | ||
foo: { query: { id: true } }, | ||
// @ts-expect-error mutation not in schema | ||
bar: { mutation: { id: true } }, | ||
}) | ||
const clientMutationOnly = create<SchemaMutationOnly.Index>({ | ||
schema: SchemaMutationOnly.schema, | ||
schemaIndex: SchemaMutationOnly.$Index, | ||
}) | ||
clientMutationOnly.document({ | ||
// @ts-expect-error query not in schema | ||
foo: { query: { id: true } }, | ||
bar: { mutation: { id: true } }, | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`output`, () => { | ||
test(`document with one query`, async () => { | ||
{ | ||
const result = await client.document({ foo: { query: { id: true } } }).run() | ||
expectTypeOf(result).toEqualTypeOf<{ id: string | null }>() | ||
} | ||
{ | ||
const result = await client.document({ foo: { query: { id: true } } }).run(`foo`) | ||
expectTypeOf(result).toEqualTypeOf<{ id: string | null }>() | ||
} | ||
{ | ||
const result = await client.document({ foo: { query: { id: true } } }).run(undefined) | ||
expectTypeOf(result).toEqualTypeOf<{ id: string | null }>() | ||
} | ||
}) | ||
test(`document with two queries`, async () => { | ||
const result = await client.document({ | ||
foo: { query: { id: true } }, | ||
bar: { query: { id: true } }, | ||
}).run(`foo`) | ||
expectTypeOf(result).toEqualTypeOf<{ id: string | null }>() | ||
}) | ||
}) |
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,70 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import type { Index } from '../../tests/_/schema/generated/Index.js' | ||
import { $Index } from '../../tests/_/schema/generated/SchemaRuntime.js' | ||
import { db, schema } from '../../tests/_/schema/schema.js' | ||
import { create } from './client.js' | ||
|
||
const client = create<Index>({ schema, schemaIndex: $Index }) | ||
|
||
describe(`document with two queries`, () => { | ||
const withTwo = client.document({ | ||
foo: { | ||
query: { id: true }, | ||
}, | ||
bar: { | ||
query: { idNonNull: true }, | ||
}, | ||
}) | ||
|
||
test(`works`, async () => { | ||
const { run } = withTwo | ||
await expect(run(`foo`)).resolves.toEqual({ data: { id: db.id1 } }) | ||
await expect(run(`bar`)).resolves.toEqual({ data: { idNonNull: db.id1 } }) | ||
}) | ||
test(`error if no operation name is provided`, async () => { | ||
const { run } = withTwo | ||
// @ts-expect-error | ||
await expect(run()).resolves.toMatchObject({ | ||
errors: [{ message: `Must provide operation name if query contains multiple operations.` }], | ||
}) | ||
}) | ||
test(`error if wrong operation name is provided`, async () => { | ||
const { run } = withTwo | ||
// @ts-expect-error | ||
await expect(run(`boo`)).resolves.toMatchObject({ errors: [{ message: `Unknown operation named "boo".` }] }) | ||
}) | ||
test(`error if invalid name in document`, async () => { | ||
// @ts-expect-error | ||
const { run } = client.document({ foo$: { query: { id: true } } }) | ||
await expect(run(`foo$`)).resolves.toMatchObject({ | ||
errors: [{ message: `Syntax Error: Expected "{", found "$".` }], | ||
}) | ||
}) | ||
}) | ||
|
||
test(`document with one query`, async () => { | ||
const { run } = client.document({ foo: { query: { id: true } } }) | ||
await expect(run(`foo`)).resolves.toEqual({ data: { id: db.id1 } }) | ||
await expect(run()).resolves.toEqual({ data: { id: db.id1 } }) | ||
await expect(run(undefined)).resolves.toEqual({ data: { id: db.id1 } }) | ||
}) | ||
|
||
test(`document with one mutation`, async () => { | ||
const { run } = client.document({ foo: { mutation: { id: true } } }) | ||
await expect(run(`foo`)).resolves.toEqual({ data: { id: db.id1 } }) | ||
await expect(run()).resolves.toEqual({ data: { id: db.id1 } }) | ||
await expect(run(undefined)).resolves.toEqual({ data: { id: db.id1 } }) | ||
}) | ||
|
||
test(`document with one mutation and one query`, async () => { | ||
const { run } = client.document({ | ||
foo: { | ||
mutation: { id: true }, | ||
}, | ||
bar: { | ||
query: { idNonNull: true }, | ||
}, | ||
}) | ||
await expect(run(`foo`)).resolves.toEqual({ data: { id: db.id1 } }) | ||
await expect(run(`bar`)).resolves.toEqual({ data: { idNonNull: db.id1 } }) | ||
}) |
Oops, something went wrong.