Skip to content

Commit

Permalink
feat(ts-client): rawOrThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 28, 2024
1 parent b1f18c1 commit 2a39e81
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
24 changes: 24 additions & 0 deletions src/layers/5_client/client.raw.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from 'vitest'
import { $Index } from '../../../tests/_/schema/generated/SchemaRuntime.js'
import { schema } from '../../../tests/_/schema/schema.js'
import { create } from './client.js'

// todo test with custom scalars

const client = create({ schema, schemaIndex: $Index })

test(`.rawOrThrow() throws if errors array non-empty`, async () => {
await expect(client.rawOrThrow(`query {}`)).rejects.toMatchInlineSnapshot(
`[ContextualAggregateError: One or more errors in the execution result.]`,
)
})

test(`.raw() returns errors in array`, async () => {
await expect(client.raw(`query {}`)).resolves.toMatchInlineSnapshot(`
{
"errors": [
[GraphQLError: Syntax Error: Expected Name, found "}".],
],
}
`)
})
24 changes: 12 additions & 12 deletions src/layers/5_client/client.returnMode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('default (data)', () => {
await expect(client.document({ main: { query: { id: true } } }).runOrThrow()).resolves.toEqual({ id: db.id })
})
test(`document.runOrThrow error`, async () => {
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.error)
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.errorAggregate)
})
test('raw', async () => {
await expect(client.raw('query main {\nid\n}', {}, 'main')).resolves.toEqual({ data: { id: db.id } })
Expand All @@ -25,16 +25,16 @@ describe('default (data)', () => {
await expect(client.query.__typename()).resolves.toEqual('Query')
})
test('query.<fieldMethod> error', async () => {
await expect(client.query.error()).rejects.toMatchObject(db.error)
await expect(client.query.error()).rejects.toMatchObject(db.errorAggregate)
})
test('query.<fieldMethod> error orThrow', async () => {
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
})
test('query.$batch', async () => {
await expect(client.query.$batch({ __typename: true, id: true })).resolves.toEqual({ __typename: 'Query', id: db.id })
})
test('query.$batchOrThrow error', async () => {
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
})
test('mutation.<fieldMethod>', async () => {
await expect(client.mutation.__typename()).resolves.toEqual('Mutation')
Expand All @@ -54,7 +54,7 @@ describe('dataAndErrors', () => {
await expect(client.document({ main: { query: { id: true } } }).runOrThrow()).resolves.toEqual({ id: db.id })
})
test(`document.runOrThrow error`, async () => {
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.error)
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.errorAggregate)
})
test('raw', async () => {
await expect(client.raw('query main {\nid\n}', {}, 'main')).resolves.toEqual({ data: { id: db.id } })
Expand All @@ -63,16 +63,16 @@ describe('dataAndErrors', () => {
await expect(client.query.__typename()).resolves.toEqual('Query')
})
test('query.<fieldMethod> error', async () => {
await expect(client.query.error()).resolves.toMatchObject(db.error)
await expect(client.query.error()).resolves.toMatchObject(db.errorAggregate)
})
test('query.<fieldMethod> error orThrow', async () => {
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
})
test('query.$batch', async () => {
await expect(client.query.$batch({ __typename: true, id: true })).resolves.toEqual({ __typename: 'Query', id: db.id })
})
test('query.$batchOrThrow error', async () => {
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
})
test('mutation.<fieldMethod>', async () => {
await expect(client.mutation.__typename()).resolves.toEqual('Mutation')
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('graphql', () => {
await expect(client.document({ main: { query: { id: true } } }).runOrThrow()).resolves.toEqual({data:{ id: db.id }})
})
test(`document.runOrThrow error`, async () => {
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.error)
await expect(client.document({ main: { query: { error: true } } }).runOrThrow()).rejects.toEqual(db.errorAggregate)
})
test('raw', async () => {
await expect(client.raw('query main {\nid\n}', {}, 'main')).resolves.toEqual({ data: { id: db.id } })
Expand All @@ -155,16 +155,16 @@ describe('graphql', () => {
await expect(client.query.__typename()).resolves.toEqual({ data: { __typename: 'Query' } })
})
test('query.<fieldMethod> error', async () => {
await expect(client.query.error()).resolves.toMatchObject({ errors:db.error['errors'] })
await expect(client.query.error()).resolves.toMatchObject({ errors:db.errorAggregate['errors'] })
})
test('query.<fieldMethod> orThrow error', async () => {
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
})
test('query.$batch', async () => {
await expect(client.query.$batch({ __typename: true, id: true })).resolves.toEqual({ data: { __typename: 'Query', id: db.id } })
})
test('query.$batchOrThrow error', async () => {
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
})
test('mutation.<fieldMethod>', async () => {
await expect(client.mutation.__typename()).resolves.toEqual({ data: { __typename: 'Mutation' } })
Expand Down
6 changes: 3 additions & 3 deletions src/layers/5_client/client.rootTypeMethods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ describe(`query`, () => {
await expect(client.query.objectWithArgsOrThrow({ $: { id: `x` }, id: true })).resolves.toEqual({ id: `x` })
})
test(`with error`, async () => {
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.error)
await expect(client.query.errorOrThrow()).rejects.toMatchObject(db.errorAggregate)
})
})
describe(`$batch`, () => {
test(`success`, async () => {
await expect(client.query.$batch({ id: true })).resolves.toMatchObject({ id:db.id })
})
test(`error`, async () => {
await expect(client.query.$batch({ error: true })).rejects.toMatchObject(db.error)
await expect(client.query.$batch({ error: true })).rejects.toMatchObject(db.errorAggregate)
})
describe(`orThrow`, () => {
test(`success`, async () => {
await expect(client.query.$batchOrThrow({ id: true })).resolves.toMatchObject({ id:db.id })
})
test(`error`, async () => {
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.error)
await expect(client.query.$batchOrThrow({ error: true })).rejects.toMatchObject(db.errorAggregate)
})
})
})
Expand Down
14 changes: 14 additions & 0 deletions src/layers/5_client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ExecutionResult } from 'graphql'
import { type DocumentNode, execute, graphql, type GraphQLSchema } from 'graphql'
import request from '../../entrypoints/main.js'
import { Errors } from '../../lib/errors/__.js'
import type { SomeExecutionResultWithoutErrors } from '../../lib/graphql.js'
import { type RootTypeName, rootTypeNameToOperationName, type Variables } from '../../lib/graphql.js'
import { isPlainObject } from '../../lib/prelude.js'
import type { Object$2 } from '../1_Schema/__.js'
Expand All @@ -27,6 +28,7 @@ export type Client<$Index extends Schema.Index, $Config extends Config> =
& {
// todo test raw
raw: (document: string | DocumentNode, variables?:Variables, operationName?:string) => Promise<ExecutionResult>
rawOrThrow: (document: string | DocumentNode, variables?:Variables, operationName?:string) => Promise<SomeExecutionResultWithoutErrors>
document: DocumentFn<$Config, $Index>
}
& GetRootTypeMethods<$Config, $Index>
Expand Down Expand Up @@ -295,6 +297,18 @@ export const create = <$Input extends Input>(
raw: async (document: string | DocumentNode, variables?: Variables, operationName?: string) => {
return await executeGraphQLDocument({ document, variables, operationName })
},
rawOrThrow: async (document: string | DocumentNode, variables?: Variables, operationName?: string) => {
const result = await client.raw(document, variables, operationName) as ExecutionResult // eslint-disable-line
// todo consolidate
if (result.errors && result.errors.length > 0) {
throw new Errors.ContextualAggregateError(
`One or more errors in the execution result.`,
{},
result.errors,
)
}
return result
},
document: (documentObject: DocumentObject) => {
const run = async (operationName: string) => {
// 1. if returnMode is successData OR using orThrow
Expand Down
10 changes: 10 additions & 0 deletions src/lib/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isListType,
isNonNullType,
} from 'graphql'
import type { ObjMap } from 'graphql/jsutils/ObjMap.js'
import type { Errors } from './errors/__.js'

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

export type OperationName = 'query' | 'mutation'

export interface SomeExecutionResultWithoutErrors<
TData = ObjMap<unknown>,
TExtensions = ObjMap<unknown>,
> {
errors?: readonly []
data?: TData | null
extensions?: TExtensions
}
4 changes: 2 additions & 2 deletions tests/_/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Errors } from '../../src/lib/errors/__.js'
const date0 = new Date(0)

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

Expand Down Expand Up @@ -67,5 +67,5 @@ export const db = {
DateInterface1: {
date1: date0,
},
error,
errorAggregate,
} as const

0 comments on commit 2a39e81

Please sign in to comment.