Skip to content

Commit

Permalink
feat(ts-client): add raw method
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 15, 2024
1 parent 5de618b commit 420a389
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DocumentNode } from 'graphql'
import type { ExcludeUndefined } from 'type-fest/source/required-deep.js'
import request from '../entrypoints/main.js'
import { type RootTypeName } from '../lib/graphql.js'
Expand All @@ -9,7 +10,7 @@ import { SelectionSet } from './SelectionSet/__.js'
import type { GraphQLDocumentObject } from './SelectionSet/toGraphQLDocumentString.js'

// dprint-ignore
type RootTypeMethods<$Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName> =
type RootTypeMethods<$Index extends Schema.Index, $RootTypeName extends Schema.RootTypeName> =
& {
$batch: <$SelectionSet extends object>(selectionSet: Exact<$SelectionSet, SelectionSet.Root<$Index, $RootTypeName>>) =>
Promise<ResultSet.Root<$SelectionSet, $Index,$RootTypeName>>
Expand All @@ -22,6 +23,9 @@ type RootTypeMethods<$Index extends Schema.Index, $RootTypeName extends Schema.R

// dprint-ignore
export type Client<$Index extends Schema.Index> =
& {
raw: (document: DocumentNode, variables?:object) => Promise<object>
}
& (
$Index['Root']['Query'] extends null
? unknown
Expand All @@ -31,10 +35,10 @@ export type Client<$Index extends Schema.Index> =
)
& (
$Index['Root']['Mutation'] extends null
? unknown
: {
mutation: RootTypeMethods<$Index, 'Mutation'>
}
? unknown
: {
mutation: RootTypeMethods<$Index, 'Mutation'>
}
)
// todo
// & ($SchemaIndex['Root']['Subscription'] extends null ? {
Expand Down Expand Up @@ -86,15 +90,30 @@ export const create = <$SchemaIndex extends Schema.Index>(input: Input): Client<
url: new URL(input.url).href,
requestHeaders: input.headers,
document: documentString,
// todo handle variables
})
const resultDecoded = CustomScalars.decode(rootIndex, result as object)
return resultDecoded
}

// @ts-expect-error ignoreme
const client: Client<$SchemaIndex> = {
query: sendDocumentObject(`Query`),
mutation: sendDocumentObject(`Mutation`),
raw: async (document, variables) => {
return await request({
url: new URL(input.url).href,
requestHeaders: input.headers,
document,
variables,
})
},
query: {
$batch: sendDocumentObject(`Query`),
// todo proxy that allows calling any query field
},
mutation: {
$batch: sendDocumentObject(`Mutation`),
// todo proxy that allows calling any mutation field
},
// todo
// subscription: async () => {},
}
Expand Down

0 comments on commit 420a389

Please sign in to comment.