1
1
import type { DocumentTypeDecoration } from "@graphql-typed-document-node/core" ;
2
- import { print } from "graphql" ;
2
+ import { GraphQLError , print } from "graphql" ;
3
3
import { isNode } from "graphql/language/ast.js" ;
4
4
import {
5
5
createRequest ,
@@ -16,6 +16,7 @@ import {
16
16
mergeHeaders ,
17
17
type GqlResponse ,
18
18
} from "./helpers" ;
19
+ import { toe } from "graphql-toe" ;
19
20
20
21
type Options = {
21
22
/**
@@ -60,6 +61,36 @@ type RequestOptions = {
60
61
headers ?: Headers | Record < string , string > ;
61
62
} ;
62
63
64
+ export type StrictClientFetcher = < TResponse extends Record < string , any > , TVariables > (
65
+ astNode : DocumentTypeDecoration < TResponse , TVariables > ,
66
+ variables ?: TVariables ,
67
+ options ?: RequestOptions ,
68
+ ) => Promise < TResponse > ;
69
+
70
+ // Wraps the initServerFetcher function, which returns the result wrapped in the graphql-toe library. This will throw
71
+ // an error if a field is used that had an entry in the error response array
72
+ export const initStrictClientFetcher = ( url : string , options : Options = { } ) : StrictClientFetcher => {
73
+ const fetcher = initClientFetcher ( url , options ) ;
74
+ return async < TResponse extends Record < string , any > , TVariables > (
75
+ astNode : DocumentTypeDecoration < TResponse , TVariables > ,
76
+ variables ?: TVariables ,
77
+ options ?: RequestOptions ,
78
+ ) : Promise < TResponse > => {
79
+ const response = await fetcher (
80
+ astNode ,
81
+ variables ,
82
+ options ,
83
+ ) ;
84
+
85
+ return toe < TResponse > (
86
+ response as unknown as {
87
+ data ?: TResponse | null | undefined ;
88
+ errors ?: readonly GraphQLError [ ] | undefined ;
89
+ } ,
90
+ ) ;
91
+ } ;
92
+ } ;
93
+
63
94
export type ClientFetcher = < TResponse , TVariables > (
64
95
astNode : DocumentTypeDecoration < TResponse , TVariables > ,
65
96
variables ?: TVariables ,
0 commit comments