Skip to content

Commit 2a39e81

Browse files
committedApr 28, 2024·
feat(ts-client): rawOrThrow
1 parent b1f18c1 commit 2a39e81

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed
 
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect, test } from 'vitest'
2+
import { $Index } from '../../../tests/_/schema/generated/SchemaRuntime.js'
3+
import { schema } from '../../../tests/_/schema/schema.js'
4+
import { create } from './client.js'
5+
6+
// todo test with custom scalars
7+
8+
const client = create({ schema, schemaIndex: $Index })
9+
10+
test(`.rawOrThrow() throws if errors array non-empty`, async () => {
11+
await expect(client.rawOrThrow(`query {}`)).rejects.toMatchInlineSnapshot(
12+
`[ContextualAggregateError: One or more errors in the execution result.]`,
13+
)
14+
})
15+
16+
test(`.raw() returns errors in array`, async () => {
17+
await expect(client.raw(`query {}`)).resolves.toMatchInlineSnapshot(`
18+
{
19+
"errors": [
20+
[GraphQLError: Syntax Error: Expected Name, found "}".],
21+
],
22+
}
23+
`)
24+
})

‎src/layers/5_client/client.returnMode.test.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('default (data)', () => {
1616
await expect(client.document({ main: { query: { id: true } } }).runOrThrow()).resolves.toEqual({ id: db.id })
1717
})
1818
test(`document.runOrThrow error`, async () => {
19-
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.error)
19+
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.errorAggregate)
2020
})
2121
test('raw', async () => {
2222
await expect(client.raw('query main {\nid\n}', {}, 'main')).resolves.toEqual({ data: { id: db.id } })
@@ -25,16 +25,16 @@ describe('default (data)', () => {
2525
await expect(client.query.__typename()).resolves.toEqual('Query')
2626
})
2727
test('query.<fieldMethod> error', async () => {
28-
await expect(client.query.error()).rejects.toMatchObject(db.error)
28+
await expect(client.query.error()).rejects.toMatchObject(db.errorAggregate)
2929
})
3030
test('query.<fieldMethod> error orThrow', async () => {
31-
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
31+
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
3232
})
3333
test('query.$batch', async () => {
3434
await expect(client.query.$batch({ __typename: true, id: true })).resolves.toEqual({ __typename: 'Query', id: db.id })
3535
})
3636
test('query.$batchOrThrow error', async () => {
37-
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
37+
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
3838
})
3939
test('mutation.<fieldMethod>', async () => {
4040
await expect(client.mutation.__typename()).resolves.toEqual('Mutation')
@@ -54,7 +54,7 @@ describe('dataAndErrors', () => {
5454
await expect(client.document({ main: { query: { id: true } } }).runOrThrow()).resolves.toEqual({ id: db.id })
5555
})
5656
test(`document.runOrThrow error`, async () => {
57-
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.error)
57+
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.errorAggregate)
5858
})
5959
test('raw', async () => {
6060
await expect(client.raw('query main {\nid\n}', {}, 'main')).resolves.toEqual({ data: { id: db.id } })
@@ -63,16 +63,16 @@ describe('dataAndErrors', () => {
6363
await expect(client.query.__typename()).resolves.toEqual('Query')
6464
})
6565
test('query.<fieldMethod> error', async () => {
66-
await expect(client.query.error()).resolves.toMatchObject(db.error)
66+
await expect(client.query.error()).resolves.toMatchObject(db.errorAggregate)
6767
})
6868
test('query.<fieldMethod> error orThrow', async () => {
69-
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
69+
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
7070
})
7171
test('query.$batch', async () => {
7272
await expect(client.query.$batch({ __typename: true, id: true })).resolves.toEqual({ __typename: 'Query', id: db.id })
7373
})
7474
test('query.$batchOrThrow error', async () => {
75-
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
75+
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
7676
})
7777
test('mutation.<fieldMethod>', async () => {
7878
await expect(client.mutation.__typename()).resolves.toEqual('Mutation')
@@ -146,7 +146,7 @@ describe('graphql', () => {
146146
await expect(client.document({ main: { query: { id: true } } }).runOrThrow()).resolves.toEqual({data:{ id: db.id }})
147147
})
148148
test(`document.runOrThrow error`, async () => {
149-
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.error)
149+
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.errorAggregate)
150150
})
151151
test('raw', async () => {
152152
await expect(client.raw('query main {\nid\n}', {}, 'main')).resolves.toEqual({ data: { id: db.id } })
@@ -155,16 +155,16 @@ describe('graphql', () => {
155155
await expect(client.query.__typename()).resolves.toEqual({ data: { __typename: 'Query' } })
156156
})
157157
test('query.<fieldMethod> error', async () => {
158-
await expect(client.query.error()).resolves.toMatchObject({ errors:db.error['errors'] })
158+
await expect(client.query.error()).resolves.toMatchObject({ errors:db.errorAggregate['errors'] })
159159
})
160160
test('query.<fieldMethod> orThrow error', async () => {
161-
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
161+
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
162162
})
163163
test('query.$batch', async () => {
164164
await expect(client.query.$batch({ __typename: true, id: true })).resolves.toEqual({ data: { __typename: 'Query', id: db.id } })
165165
})
166166
test('query.$batchOrThrow error', async () => {
167-
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
167+
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
168168
})
169169
test('mutation.<fieldMethod>', async () => {
170170
await expect(client.mutation.__typename()).resolves.toEqual({ data: { __typename: 'Mutation' } })

‎src/layers/5_client/client.rootTypeMethods.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ describe(`query`, () => {
4141
await expect(client.query.objectWithArgsOrThrow({ $: { id: `x` }, id: true })).resolves.toEqual({ id: `x` })
4242
})
4343
test(`with error`, async () => {
44-
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
44+
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
4545
})
4646
})
4747
describe(`$batch`, () => {
4848
test(`success`, async () => {
4949
await expect(client.query.$batch({ id: true })).resolves.toMatchObject({ id:db.id })
5050
})
5151
test(`error`, async () => {
52-
await expect(client.query.$batch({ error: true })).rejects.toMatchObject(db.error)
52+
await expect(client.query.$batch({ error: true })).rejects.toMatchObject(db.errorAggregate)
5353
})
5454
describe(`orThrow`, () => {
5555
test(`success`, async () => {
5656
await expect(client.query.$batchOrThrow({ id: true })).resolves.toMatchObject({ id:db.id })
5757
})
5858
test(`error`, async () => {
59-
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
59+
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
6060
})
6161
})
6262
})

‎src/layers/5_client/client.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ExecutionResult } from 'graphql'
22
import { type DocumentNode, execute, graphql, type GraphQLSchema } from 'graphql'
33
import request from '../../entrypoints/main.js'
44
import { Errors } from '../../lib/errors/__.js'
5+
import type { SomeExecutionResultWithoutErrors } from '../../lib/graphql.js'
56
import { type RootTypeName, rootTypeNameToOperationName, type Variables } from '../../lib/graphql.js'
67
import { isPlainObject } from '../../lib/prelude.js'
78
import type { Object$2 } from '../1_Schema/__.js'
@@ -27,6 +28,7 @@ export type Client<$Index extends Schema.Index, $Config extends Config> =
2728
& {
2829
// todo test raw
2930
raw: (document: string | DocumentNode, variables?:Variables, operationName?:string) => Promise<ExecutionResult>
31+
rawOrThrow: (document: string | DocumentNode, variables?:Variables, operationName?:string) => Promise<SomeExecutionResultWithoutErrors>
3032
document: DocumentFn<$Config, $Index>
3133
}
3234
& GetRootTypeMethods<$Config, $Index>
@@ -295,6 +297,18 @@ export const create = <$Input extends Input>(
295297
raw: async (document: string | DocumentNode, variables?: Variables, operationName?: string) => {
296298
return await executeGraphQLDocument({ document, variables, operationName })
297299
},
300+
rawOrThrow: async (document: string | DocumentNode, variables?: Variables, operationName?: string) => {
301+
const result = await client.raw(document, variables, operationName) as ExecutionResult // eslint-disable-line
302+
// todo consolidate
303+
if (result.errors && result.errors.length > 0) {
304+
throw new Errors.ContextualAggregateError(
305+
`One or more errors in the execution result.`,
306+
{},
307+
result.errors,
308+
)
309+
}
310+
return result
311+
},
298312
document: (documentObject: DocumentObject) => {
299313
const run = async (operationName: string) => {
300314
// 1. if returnMode is successData OR using orThrow

‎src/lib/graphql.ts

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isListType,
1212
isNonNullType,
1313
} from 'graphql'
14+
import type { ObjMap } from 'graphql/jsutils/ObjMap.js'
1415
import type { Errors } from './errors/__.js'
1516

1617
export type TypeMapByKind =
@@ -247,3 +248,12 @@ export type Variables = Record<string, string | number | boolean | null> // todo
247248
export type GraphQLExecutionResultError = Errors.ContextualAggregateError<GraphQLError>
248249

249250
export type OperationName = 'query' | 'mutation'
251+
252+
export interface SomeExecutionResultWithoutErrors<
253+
TData = ObjMap<unknown>,
254+
TExtensions = ObjMap<unknown>,
255+
> {
256+
errors?: readonly []
257+
data?: TData | null
258+
extensions?: TExtensions
259+
}

‎tests/_/db.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Errors } from '../../src/lib/errors/__.js'
44
const date0 = new Date(0)
55

66
// const error = { errors: [{ message: `Something went wrong.` }] }
7-
const error = new Errors.ContextualAggregateError(`One or more errors in the execution result.`, {}, [
7+
const errorAggregate = new Errors.ContextualAggregateError(`One or more errors in the execution result.`, {}, [
88
new GraphQLError(`Something went wrong.`),
99
])
1010

@@ -67,5 +67,5 @@ export const db = {
6767
DateInterface1: {
6868
date1: date0,
6969
},
70-
error,
70+
errorAggregate,
7171
} as const

0 commit comments

Comments
 (0)
Please sign in to comment.