11import type { DocumentTypeDecoration } from "@graphql-typed-document-node/core" ;
2- import { print } from "graphql" ;
2+ import { GraphQLError , print } from "graphql" ;
33import { isNode } from "graphql/language/ast.js" ;
44import {
55 createRequest ,
@@ -16,6 +16,7 @@ import {
1616 mergeHeaders ,
1717 type GqlResponse ,
1818} from "./helpers" ;
19+ import { toe } from "graphql-toe" ;
1920
2021type Options = {
2122 /**
@@ -60,6 +61,36 @@ type RequestOptions = {
6061 headers ?: Headers | Record < string , string > ;
6162} ;
6263
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+
6394export type ClientFetcher = < TResponse , TVariables > (
6495 astNode : DocumentTypeDecoration < TResponse , TVariables > ,
6596 variables ?: TVariables ,
0 commit comments